4 ms·
Sure, 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
by tialaramex 2y ago
Sure, 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.