3 ms·
Why do you think the SSO is linked to the zero terminator? I always thought it was due to the cost of allocations (and deallocations!) combined with the fact th
by SkiFire13 2y ago
Why do you think the SSO is linked to the zero terminator? I always thought it was due to the cost of allocations (and deallocations!) combined with the fact that most strings are actually pretty small.
- tialaramex 2y agoSure, but in a language without the zero terminator the empty string doesn't need an allocation. With a zero terminator, only SSO allows us to avoid that. There are a lot of empty strings. [On a typical modern 64-bit computer] An empty Rust std::String is 24 bytes, exactly the same as the Clang libc++ std::string as somewhat smaller than an MSVC or GCC std::string Most strings are "pretty small" but that's relative. SSO often gives you 15 characters, so the string "http://news.ycombinator.com/ http://news.ycombinator.com/" is far too big, so is my full name, my work email address, a credit card number, and so on. The biggest pay off seems to be that empty string, in Rust's model it's free, in C++ it could not be free without SSO.
- gpderetta 2y agoYou can have allocation-less empty std::string even without SSO. The empty string simply uses a statically allocated shared buffer. That's what libstdc++ did in its old refcounted variant, for example. And no, you don't need refcounting.