-
Notifications
You must be signed in to change notification settings - Fork 63
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
C++: In std=gnu++2b size-optimized builds std::string methods are inlined #67
Comments
Hi @rojer, I appreciate your efforts in size optimization! I took a look at your examples and that what I found - constexpr can explicitly inline functions and the issue appeared when the method called once in translation unit, otherwise gcc is smart enough to not inline the method and make a call... But yes, it's still an issue if you have lot of such cases. The first thing comes to my head is to redefine And your example could be built with |
ok, it's clear now and adheres to the C++20 standard but the tradeoff is definitely wrong for us - being able to construct std::strings in constexpr functions is not worth the bloat, at all. i tried adding these flags to an IDF project build but the compiler got very angry at me :) probably some issue with quoting or something... can you help me apply this to an IDF project, just to see what effect it would have? also, would you consider patching libstdc++ to add a proper way to override this during build? something like
and then use we'd then be able to add |
@rojer , to apply flags to your projects please update your
I'm going to prepare a patch and open a ticket on GCC bugzilla to discuss the changes |
@rojer , I'm also interested to know if you've observed binary size increases only for string methods. It seems to be a problem for most std functions |
you are most likely right, std::string was just the first thing i found and reported. ok, i tried with the settings you suggested but surprisingly the size of binary went up, and by quite a lot (gcc14_cxx2b_opt):
|
@rojer , that's interesting. What result will be if you change |
much better:
even better than cxx17 |
@rojer , thank you for sharing the results of the real application. Could you please check one more option, change the strings: -idf_build_set_property(CXX_COMPILE_OPTIONS "-D_GLIBCXX20_CONSTEXPR=__attribute__((cold))" APPEND)
-idf_build_set_property(CXX_COMPILE_OPTIONS "-D__cpp_lib_constexpr_string=" APPEND)
-idf_build_set_property(CXX_COMPILE_OPTIONS "-D__cpp_lib_string_resize_and_overwrite=" APPEND)
+idf_build_set_property(CXX_COMPILE_OPTIONS "-D_GLIBCXX20_CONSTEXPR=constexpr __attribute__((cold))" APPEND) |
first, apologies: previous test was done incorrect, std was actually set to 17, so i renamed it to
|
Checklist
How often does this bug occurs?
always
Expected behavior
I expected frequently used functions such as
std::string::append
not to be inlined, at least not in-Os
mode.Actual behavior (suspected bug)
Instead, when switching from gnu++17 to gnu++2b standard std::string functions such as append, resize, instead appear to be inlined. This alone is responsible for significant binary size increase for our application.
Error logs or terminal output
No response
Steps to reproduce the behavior
test file: https://gist.github.com/rojer/81900a923fdfa7e6853afe72fa2ea87b
to change the standard: edit tools/cmake/build.cmake, change the
cxx_std
variable.it's obvious that in c++17 mode std::string methods are invoked while in c++2b mode they are inlined.
Project release version
14.2.0_20240906
System architecture
other (details in Additional context)
Operating system
Linux
Operating system version
Ubuntu 24.04
Shell
Bash
Additional context
IDF 5.2.2
The text was updated successfully, but these errors were encountered: