Aircoookie / WLED

Control WS2812B and many more types of digital RGB LEDs with an ESP8266 or ESP32 over WiFi!
https://kno.wled.ge
MIT License
15.01k stars 3.24k forks source link

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

Open softhack007 opened 1 month ago

softhack007 commented 1 month ago

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

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)
softhack007 commented 1 month ago

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

Flash: 85.8% (used 1461869 bytes)

blazoncek commented 1 month ago

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 commented 1 month ago

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 commented 1 month ago

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 commented 1 month ago

@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.