Explain the Small String Optimization (SSO) used by many STL implementations.

Last updated 21 June 2026

  • Small String Optimization stores short strings (often up to ~15 chars on 64-bit) in an inline buffer inside the std::string object, switching to heap allocation only when the content exceeds that threshold.

  • It avoids heap allocations for common short strings and improves cache locality.

  • Costs: larger sizeof(std::string) (typically 24-32 bytes), implementation-defined thresholds, and moves of short strings copy the buffer rather than stealing a pointer.

  • https://pypi.org/project/stringzilla/3.9.3/
  • https://dev.to/codemee/c-de-string-wu-jian-dao-di-zhan-ji-ge-wei-yuan-zu--42ea
  • https://github.com/ashvardanian/stringzilla
All C++ interview questions