SSO (Small String Optimization) Small String Optimization
Storing short strings directly inside the string object instead of allocating on the heap. The reason sizeof(std::string) is 32 bytes and not 8.
Since most strings in real programs are short — keys, identifiers, labels, file extensions — an allocation per string would be an enormous waste. Implementations therefore put the contents in the object's own bytes up to a certain length, fifteen characters in libstdc++ and twenty-two in libc++, and only switch to the heap beyond that. The gain is both fewer allocations and having the data in the same cache line as the object. Three consequences are worth knowing: the object is larger than you think, moving a short string actually copies the contents rather than stealing a pointer and so isn't free, and the branch between short and long shows up in profiles of string-heavy code. None of this is required by the standard.