size optimization for "V4" firmware (-flto)
This is a continuation (and replacement) for
- #3824 by @deece
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) |
Update: omitting "-g3 -ggdb" saves another 26 kB on the wrover build.
Flash: 85.8% (used 1461869 bytes)
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.
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").
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.
@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.
What's happening with this PR? Are we confident enough that it can go into a bugfix release or should it be moved to 0.16 @softhack007 ?
changing the core version is by no means a bugfix... I like the idea of using tasmota arduino build, if license allows it. maintining our own would needs someone to commit to that but it has the advantage that it may be even more tailored to WLED, IIRC compiling it allows to use IDF optimization flags, like disabling core debugging or support for sprintf to print double floats and such to save flash.
@DedeHai Everything made from Tasmota keeps the license from the original source. So there is no license issue for the adopted Tasmota framework. If you can/want to use, just do.
0.16 is already moved over to Tasmota
Walkthrough
The changes update ESP32 platform versions in the build configuration, add explicit control over link-time optimization (LTO) and debugging flags for several ESP32 environments, and include clarifying comments. No source code or exported entities are altered; all modifications are confined to the configuration file.
Changes
| File(s) | Change Summary |
|---|---|
| platformio.ini | Updated ESP32 platform version to ~6.6.0; added and adjusted LTO and debugging flags for specific environments; added explanatory comments. |
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.
🪧 Tips
Chat
There are 3 ways to chat with CodeRabbit:
- Review comments: Directly reply to a review comment made by CodeRabbit. Example:
-
I pushed a fix in commit <commit_id>, please review it. -
Explain this complex logic. -
Open a follow-up GitHub issue for this discussion.
-
- Files and specific lines of code (under the "Files changed" tab): Tag
@coderabbitaiin a new review comment at the desired location with your query. Examples:-
@coderabbitai explain this code block. -
@coderabbitai modularize this function.
-
- PR comments: Tag
@coderabbitaiin a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:-
@coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase. -
@coderabbitai read src/utils.ts and explain its main purpose. -
@coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format. -
@coderabbitai help me debug CodeRabbit configuration file.
-
Support
Need help? Create a ticket on our support page for assistance with any issues or questions.
Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.
CodeRabbit Commands (Invoked using PR comments)
-
@coderabbitai pauseto pause the reviews on a PR. -
@coderabbitai resumeto resume the paused reviews. -
@coderabbitai reviewto trigger an incremental review. This is useful when automatic reviews are disabled for the repository. -
@coderabbitai full reviewto do a full review from scratch and review all the files again. -
@coderabbitai summaryto regenerate the summary of the PR. -
@coderabbitai generate docstringsto generate docstrings for this PR. -
@coderabbitai generate sequence diagramto generate a sequence diagram of the changes in this PR. -
@coderabbitai resolveresolve all the CodeRabbit review comments. -
@coderabbitai configurationto show the current CodeRabbit configuration for the repository. -
@coderabbitai helpto get help.
Other keywords and placeholders
- Add
@coderabbitai ignoreanywhere in the PR description to prevent this PR from being reviewed. - Add
@coderabbitai summaryto generate the high-level summary at a specific location in the PR description. - Add
@coderabbitaianywhere in the PR title to generate the title automatically.
CodeRabbit Configuration File (.coderabbit.yaml)
- You can programmatically configure CodeRabbit by adding a
.coderabbit.yamlfile to the root of your repository. - Please see the configuration documentation for more information.
- If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation:
# yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json
Documentation and Community
- Visit our Documentation for detailed information on how to use CodeRabbit.
- Join our Discord Community to get help, request features, and share feedback.
- Follow us on X/Twitter for updates and announcements.