Skip to content

Multiple equal flash-strings are not optimized #4863

Open
@Ivan-Perez

Description

@Ivan-Perez

Hello!

After looking at my growing-size sketch, which contains too many string messages, I've realised that if a flash string appears more than once the compiler is not saving space and instead it is allocating flash memory for every repeated flash string.

Here it is a simple test case:

void setup() {
  Serial.begin(115200);
}

void loop() {
  Serial.print("A very long example string... A very long example string... A very long example string... A very long example string...");
  Serial.print("A very long example string... A very long example string... A very long example string... A very long example string...");
  Serial.print("A very long example string... A very long example string... A very long example string... A very long example string...");
  Serial.print("A very long example string... A very long example string... A very long example string... A very long example string...");
  Serial.print("A very long example string... A very long example string... A very long example string... A very long example string...");
  Serial.print("A very long example string... A very long example string... A very long example string... A very long example string...");
  Serial.print("A very long example string... A very long example string... A very long example string... A very long example string...");
  Serial.print("A very long example string... A very long example string... A very long example string... A very long example string...");
  Serial.print("A very long example string... A very long example string... A very long example string... A very long example string...");
  Serial.print("A very long example string... A very long example string... A very long example string... A very long example string...");
  Serial.print("A very long example string... A very long example string... A very long example string... A very long example string...");
  Serial.print("A very long example string... A very long example string... A very long example string... A very long example string...");
  Serial.print("A very long example string... A very long example string... A very long example string... A very long example string...");
  Serial.print("A very long example string... A very long example string... A very long example string... A very long example string...");
  Serial.print("A very long example string... A very long example string... A very long example string... A very long example string...");
  Serial.print("A very long example string... A very long example string... A very long example string... A very long example string...");
}

Compiled using Arduino Mega as target, these are the sketch sizes and global variables usages, respectively:

  • All strings: 2990 / 306
  • Only one string (commenting all prints out except one): 2864 / 306

When strings are stored in RAM the compiler detects correctly that all the strings are the same, so it can optimize them to save only one of them.

But if I convert all strings to flash-strings (using F()), it seems that every string is copied onto the sketch, without optimizing them:

  • All strings: 4814 / 186
  • One string: 2864 / 186

Could this be improved? Thanks!!

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions