I disagree with this conclusions. I recently worked for a number of years on an embedded Linux system for a ZigBee lighting/HVAC/... control system. So reasonably limited resources compared to a server farm. The std::string SSO implementation never caused us a problem.I find that SSO implementation short sighted as it is really tuned for systems with endless memory such as Facebook's servers. Having this making it as the default string implementation in GCC is astonishing. It really shows the influence the bigger players have on the standards implementation.
The nice thing about the SSO is that std::string objects that are created on the stack and have a string that fits in the short buffer within the object are the on the stack, no heap memory used at all. The objects are then trivially copy-able. It can also help reduce heap fragmentation.
The users of the Raspberry Pi Pico, Ardunio and other micro-controllers aimed at hobbyists don't make up the bulk of the users of GCC or G++ in particular. Developers that use micro-controllers for a living are either aware of the SSO choice or don't even use C++. It makes sense that SSO is enabled by default and is able to be changed to copy-on-write if required.
I don't believe the bigger players had anything to do with the decision to use the SSO for std::string.
You may find it interesting that G++ used reference counted strings with copy-on-write before the C++ 11 standard.
https://stackoverflow.com/questions/125 ... cc-4-x-c11
The following is a short piece of C++ code that keeps adding to a character to a std::string until a heap allocation occurs.
Code:
#include <cstdlib>#include <iostream>#include <string>bool allocated{false};void* operator new(std::size_t n){ std::cout << "[Allocating " << n << " bytes]"; allocated = true; return ::malloc(n);}void operator delete(void* p) noexcept{ ::free(p);}int main(){ for (auto i = 0; not allocated ; ++i) { std::cout << i << ": " << std::string(i, '=') << '\n'; }}
Code:
0:1: =2: ==3: ===4: ====5: =====6: ======7: =======8: ========9: =========10: ==========11: ===========12: ============13: =============14: ==============15: ===============16: [Allocating 17 bytes]================
edit:
I didn't realise that Andrei Alexandrescu thought up the SSO and he was working at Facebook at the time. That still doesn't mean that Facebook forced this upon everyone. In fact they could have kept using folly::fbstring and not shared SSO this with anyone. The GCC compiler is not the only compiler to implement SSO. It is available in Clang and MSVC
There is an interesting CppCon talk from 2016: The strange details of std::string at Facebook
Statistics: Posted by AndyD — Mon Jan 27, 2025 12:26 am