What is SSO
Just briefly, SSO stands for Short String Optimization. It’s usually implemented as a small buffer (an array or something similar) occurring in the same storage as the string object. When the string is short, this buffer is used instead of a separate dynamic memory allocation.
See a simplified diagram below:
The diagram illustrates two strings and where they “land” in the string object. If the string is long (longer than N characters), it needs a costly dynamic memory allocation, and the address to that new buffer will be stored in ptr
. On the other hand, if the string is short, we can put it inside the object in the buf[N]
. Usually, buf
and ptr
might be implemented as union
to save space, as we use one or the other, but not both simultaneously.
Let’s start with a basic test and see what’s the stack size of std::string
using sizeof()
:
int main() {
return sizeof(std::string);
}
Run at Compiler Explorer
GCC and MSVC show 32, while the libc++
implementation for Clang returns 24!
And now, it’s time to check the length of that short string; how can we check it? We have several options:
- at runtime
constexpr
since C++20constinit
since C++20- and we can always look into real implementation and check the code :)
Let’s start with the first obvious option:
Checking length at runtime
To check the length of the small buffer, we can write a new()
handler and simply watch when new
is used when creating a string object:
#include
#include
void* operator new(std::size_t size) {
auto ptr = malloc(size);
if (!ptr)
throw std::bad_alloc{};
std::cout