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

size optimization for "V4" firmware (-flto) #4167

Draft
wants to merge 2 commits into
base: 0_15
Choose a base branch
from

Conversation

softhack007
Copy link
Collaborator

@softhack007 softhack007 commented Sep 29, 2024

This is a continuation (and replacement) for

Thanks to the contribution made by @deece to platform-espressif32, link time optimization is now possible in esp32 framework version 6.6.0 and above without hacking build scripts (8266 - no way, sorry).

Overview and background

https://gcc.gnu.org/onlinedocs/gcc/Optimize-Options.html#index-flto

  • LTO generally reduces firmware size by post-optimizing the binary when the linker runs, considering all pieces of the code instead of looking at each ".cpp.o" in isolation.
  • In my first tests, there was no additional speedup, however sometimes the stack usage is more than previously.
  • LTO makes it very hard to read exception decoder dumps, so I've added "-g3 -ggdb" which is recommended to improve the situation
  • esp32-C3: not possible to apply LTO, due to compiler bugs
  • Currently we can only use LTO on "V4" builds. I've enabled it for the 4MB binaries which benefit the most (see table below).

size comparison

firmware 0_15 without -flto 0_15 with -flto
esp32_wrover
83.6kB saved
RAM: 4.6% (used 60156 bytes);
Flash: 92.3% (used 1572241 bytes)
RAM: 4.6% (used 60140 bytes)
Flash: 87.4% (used 1488605 bytes)
esp32dev_8M
80.5kB saved
RAM: 18.3% (used 60084 bytes);
Flash: 73.0% (used 1530305 bytes)
RAM: 18.3% (used 60052 bytes);
Flash: 69.1% (used 1449817 bytes)
esp32s3_4M_qspi
71.7kB saved
RAM: 18.4% (used 60288 bytes);
Flash: 91.4% (used 1437945 bytes)
RAM: 18.4% (used 60264 bytes);
Flash: 86.9% (used 1366221 bytes)
lolin_s2_mini
72.3kB saved
RAM: 20.2% (used 66100 bytes);
Flash: 92.7% (used 1458094 bytes)
RAM: 20.2% (used 66100 bytes);
Flash: 88.1% (used 1385770 bytes)

…-esp32 release)

platform 6.6.0 has build-in support for "-flto" (link time optimization)
@softhack007 softhack007 marked this pull request as draft September 29, 2024 19:30
@softhack007
Copy link
Collaborator Author

softhack007 commented Sep 30, 2024

Update: omitting "-g3 -ggdb" saves another 26 kB on the wrover build.

Flash: 85.8% (used 1461869 bytes)

@blazoncek
Copy link
Collaborator

This is beyond my skill level but IMO if we can resolve flash use increase in latest platforms it would be best if we upgraded ESP32 to use newer platform in general. Platform 3.5.0 is ancient to say the least.

@softhack007
Copy link
Collaborator Author

This is beyond my skill level but IMO if we can resolve flash use increase in latest platforms it would be best if we upgraded ESP32 to use newer platform in general. Platform 3.5.0 is ancient to say the least.

yes, this is what i was thinking, too. 80-100Kb might be enough to recover from the core size increase in the new framework.

The only problem that remains - as explained previously:

in userland most people flash their devices once from install.wled.me, then update by OTA. Unfortunately, OTA cannot update the bootloader. Going to the new framework means that we'll have lots of "V3 bootloader and V4 firmware" setups. In the past this caused very unresponsive devices (suspected case LittleFs). Not sure if this is still happening. We definitely need more testing to safeguard the "OTA to V4" upgrade path on esp32.

So we might go for this change after 0.15.0 is released - to have some more time for testing. We also need to check how development is affected. My first experience with LTO was "WTF" when i got my first exception decoder dump, and it was completely gibberish (all functions renamed to "zaXXXXX").

@softhack007
Copy link
Collaborator Author

softhack007 commented Sep 30, 2024

Another option to decrease flash size is creating a custom arduino-esp32 where unneeded features (rainmaker, insights, bluetooth, etc) are removed. I've never tried it, but @Aircoookie did something similar for the old framework. And @troyhacks has some experiences with this for the new arduino framework. Plan B: use a Tasmota framework, as the tasmota team is already having their own custom arduino framework that saves around 150Kb program size.

@troyhacks
Copy link
Contributor

@softhack007 - yes, absolutely can reliably build the v4 and v5 frameworks now without the overhead (or at least test that out)

My v5.3 for the ESP32-P4 has all of the extras removed. No Rainmaker, Insights, NN, etc.

@softhack007 softhack007 added the optimization re-working an existing feature to be faster, or use less memory label Oct 11, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Awaiting testing enhancement optimization re-working an existing feature to be faster, or use less memory
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants