arduino / arduino/Arduino

Multiple equal flash-strings are not optimized

Open
#4,863 4 comments 0 reactions 0 assignees View on GitHub
Architecture: AVR Component: Compilation Type: Bug
Dominant language
Java
Stars
14.6k
Forks
7k
PR merge metrics
No merged PRs in 30d

Description

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:

``` cpp
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!!

Contributor guide

Open the contributing guide

Research direction

Start by compiling the provided Arduino Mega sketch with repeated RAM strings and repeated F() strings, then compare the reported sketch sizes. No source files or tests are identified in the issue, so trace the compiler/toolchain path responsible for flash-string storage. Done means identical flash strings are stored once without changing program behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp
Domain
embedded-iot
Issue type
Bug
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.