- Notifications
You must be signed in to change notification settings - Fork13.3k
[WString] Reduce build size by implementing flash string calls in .cpp#8106
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Uh oh!
There was an error while loading.Please reload this page.
Conversation
A function called with a flash string, which only has an implementation with `const String&` as argument will be compiled as if it is called with a `String` constructor wrapped around it.For example this implementation in the .h file:```c++bool startsWith(const __FlashStringHelper *prefix) const { return this->startsWith(String(prefix));}```This is completely useless as the compiler will generate exactly the same code with or without this function implementation in the .h file.However if we move the implementation to the .cpp file, this conversion to `String` is only added once in the compiled binary.In my own project I already managed to shrink the largest (ESP32) build by more than 70k in size (!!) by just adding extra function calls with the conversion in the .cpp file.This PR is just a simple optimisation which already shrinks a very small build of my project by almost 3k in build size. (custom_beta_ESP8266_4M1M PIO env of ESPEasy)```Flash: [======== ] 82.5% (used 862137 bytes from 1044464 bytes)Flash: [======== ] 82.3% (used 859545 bytes from 1044464 bytes)```Larger builds may benefit even more.mcspr commentedJun 8, 2021
I think it's also worth to mention |
TD-er commentedJun 8, 2021
Yep for those that can accept Especially the webserver part would benefit from a similar approach as I have seen that sending flash strings directly to the webserver would make it incredible fast, but also highly unstable when running low on free memory. So if it doesn't need to copy flash strings to memory first but serve it directly from flash string (without the need to copy internally). |
earlephilhower commentedJun 10, 2021
Very nice idea! I think your app might be the single largest user of |
TD-er commentedJun 10, 2021
:) |
A function called with a flash string, which only has an implementation with
const String&as argument will be compiled as if it is called with aStringconstructor wrapped around it.For example this implementation in the .h file:
This is completely useless as the compiler will generate exactly the same code with or without this function implementation in the .h file.
However if we move the implementation to the .cpp file, this conversion to
Stringis only added once in the compiled binary.In my own project I already managed to shrink the largest (ESP32) build by more than 70k in size (!!) by just adding extra function calls with the conversion in the .cpp file.
This PR is just a simple optimisation which already shrinks a very small build of my project by almost 3k in build size. (custom_beta_ESP8266_4M1M PIO env of ESPEasy)
Larger builds may benefit even more.