What is “small string optimization”?
Standard C++ string stores its data on the heap. But that is only true if the string grows over an implementation-dependent size. That predefined size for std::string is/was 15 for MSVC and GCC and 23 for Clang. That is: the C++ string stays “small” if you have not asked for bigger than 15/23 sized strings. The string will not attempt to grow its storage on the heap if it can stay small.
Heap memory allocations/de-allocations are taking a lot of time when compared to most standard C run time calls.
Thus if you avoid them your program will run faster and will consume less memory. Continue reading