Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Save ~3KB of RAM by moving constant strings out of RODATA #46

Merged
merged 1 commit into from
Jun 20, 2017
Merged

Save ~3KB of RAM by moving constant strings out of RODATA #46

merged 1 commit into from
Jun 20, 2017

Conversation

earlephilhower
Copy link
Contributor

This complements the work you've been doing on moving the constant arrays in the encryption routines to PMEM.

Constant text strings actually take up SRAM space on the ESP8266 because the .RODATA segment must be copied to the top of RAM at startup since FLASH isn't byte-accessible but all string(char*) functions (like ets_printf, Serial.*, etc.) need this. There are a lot of string literals in the axtls code for certificate debugging, and that eats up nearly 10% of the free RAM in any project w/SSL.

This patch moves the constant format strings in axtls' printf completely into FLASH and add a wrapper to copy it into a local stack-allocated space when needed, freeing up about 3100 bytes of RAM for use. This doesn't make FLASH usage any higher, either, since those strings were already being stored there (but never used after the power-on startup code).

Minor edits required in some of the output/debug/tracing functions, but no logic changed.

Constant strings do NOT show up in the symbol table, so they aren't easily visible by many tools like READELF. One easy way to check what string literals are eating up RAM in RODATA is to use the command:
objdump -s -j .rodata /tmp/arduino_build_89991/*.elf
where the number will vary depending on the Arduino PID, etc.

Constant text strings actually take up SRAM space on the ESP8266
because the .RODATA segment must be copied to RAM at startup since
FLASH isn't byte-accessible.

Move the constant format strings in a printf completely into FLASH
and add a wrapper to copy it into a local stack-allocated space
when needed, freeing up about 3100 bytes of RAM for use.  This doesn't
make FLASH usage any higher, either, since those strings were already
being stored there (but never used after the power-on startup code).

Minor edits required in some of the output/debug/tracing functions,
but no logic changed.
@igrr igrr merged commit 27f4a6c into igrr:master Jun 20, 2017
@igrr
Copy link
Owner

igrr commented Jun 20, 2017

Wow, those string do add up to a lot of RAM. Thanks for the PR!

igrr added a commit to esp8266/Arduino that referenced this pull request Jul 23, 2017
Includes two PRs:

- igrr/axtls-8266#46 by @earlephilhower:
  Move debug strings from RAM to Flash

- igrr/axtls-8266#50:
  Fix memory leak in ssl_ext_host_name
d-a-v pushed a commit to d-a-v/Arduino that referenced this pull request Sep 29, 2017
Includes two PRs:

- igrr/axtls-8266#46 by @earlephilhower:
  Move debug strings from RAM to Flash

- igrr/axtls-8266#50:
  Fix memory leak in ssl_ext_host_name
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants