What is the small-string optimization in C++ std::string, and how does it change copying and moving short strings?
answer
- the buffer inside the object
- capacity() of an empty string
- no buffer to steal
- swap may invalidate
- not required by the standard
basics
~20 sThe small-string optimization stores short text inside the std::string object, so no heap allocation is needed. Short copies allocate nothing, but a short move must copy the characters, and pointers into it do not follow. The capacity is each library's choice.
solid answer
~40 sMost standard libraries store a short string's characters in a buffer inside the `std::string` object, overlaid on fields a heap-backed string needs (libc++ overlays its whole representation, libstdc++ the capacity field plus spare bytes). With libc++ on a 64-bit target, `sizeof(std::string)` is 24 and an empty string reports `capacity()` 22; libstdc++ declares a 15-character local buffer. The standard does not require SSO, it only permits it: C++11 loosened the invalidation rules partly so SSO would conform. The consequences: copying a short string allocates nothing; **moving** a short string copies its characters, because there is no heap buffer to steal, so pointers taken into the source do not follow the value; and `swap` may invalidate pointers into a string, unlike `std::vector::swap`. Code must never depend on a particular SSO capacity.
code
cpp · 21 lines#include <iostream>
#include <string>
#include <utility>
int main() {
std::string empty;
std::cout << "sizeof(std::string): " << sizeof(std::string) << '\n';
std::cout << "empty capacity: " << empty.capacity() << '\n';
std::string shortName = "api/v1"; // 6 characters
std::string longName(40, 'x'); // 40 characters
const char* shortBuf = shortName.data();
const char* longBuf = longName.data();
std::string movedShort = std::move(shortName);
std::string movedLong = std::move(longName);
std::cout << std::boolalpha;
std::cout << "short buffer moved with it: " << (movedShort.data() == shortBuf) << '\n';
std::cout << "long buffer moved with it: " << (movedLong.data() == longBuf) << '\n';
}go deeper
Recall that short strings live inside the std::string object without a heap allocation, and that the cut-off length depends on the library.
Explain why a short-string move copies characters, why pointers do not follow a moved short string, and how to observe SSO with capacity().
Use SSO knowledge in profiling: allocation counts that jump past a length threshold, swaps that invalidate pointers, and benchmarks that only test short keys.
Keep SSO an implementation detail in team guidance: no length limits tuned to one library, and measurements taken on the real string lengths.
## The idea A heap-backed `std::string` needs three pieces of bookkeeping: a pointer to the buffer, the size, and the capacity. On a 64-bit target that is already 24 bytes or more. The **small-string optimization (SSO)** reuses some or all of those bytes, sometimes plus a few extra: when the text is short enough, the characters are stored **inside the string object itself**, and no heap allocation happens at all. The string records or detects which mode it is in: libc++ keeps a bit for it, while libstdc++ checks whether its data pointer points at its own local buffer. Most strings in real programs are short (keys, names, path segments, numbers turned into text), so SSO removes a large share of allocations. ## What the standard says, and what libraries choose The standard **does not require** SSO; it only makes it legal. The C++11 compatibility notes record that the invalidation rules for `basic_string` were loosened with the rationale "allow small-string optimization". Everything numeric is an implementation choice: | library | `sizeof(std::string)` on 64-bit | inline characters | |---|---|---| | libc++ (LLVM) | 24 | 22 (an empty string's `capacity()` is 22) | | libstdc++ (GCC, C++11 ABI) | 32 by its member layout | 15 (`_S_local_capacity = 15 / sizeof(_CharT)`) | The libc++ figures come from running the snippet below; the libstdc++ figure is read from its `basic_string.h`. Another library, or another version, may differ, so no code should depend on these numbers. You can observe SSO with standard calls: an empty string's `capacity()` is nonzero even though nothing was allocated, and a short string's `data()` points into the object's own address range. ## What changes for copies - **Copying a short string allocates nothing.** The characters are copied into the destination object's inline buffer. A copy is still linear in the length, but with SSO the length is small and there is no allocator call. - **Copying a long string** behaves as without SSO: allocate, copy, and later free. ## What changes for moves Moving a heap-backed string is cheap: the destination takes the pointer and the source is left valid but unspecified. Moving a **short** string cannot do that, because the characters live inside the source object, which is about to be reused or destroyed. So: 1. a move of a short string **copies the characters** into the destination's own inline buffer, so for short strings a move costs about the same as a copy, still with no allocation; 2. a pointer taken from `source.data()` before the move **keeps pointing at the source object**, not at the moved-to value; with a long string the buffer, and the pointer's target, travel with the value; 3. code that "moves a string to keep pointers stable" is therefore correct only by accident, for long strings, on implementations that transfer the buffer. ## swap and invalidation The standard lists `swap` among the operations that may invalidate pointers, references and iterators into a `basic_string`. That differs from `std::vector`, where `swap` exchanges buffers and existing iterators stay valid, now referring into the other vector. SSO is the reason: swapping two short strings exchanges characters between two inline buffers, so a pointer into one object cannot follow its text. ## Watching the threshold A counting replacement for the global `operator new` shows the cut-off directly: append characters one at a time to an empty `std::string` and note when the first allocation happens. With libc++ on a 64-bit target it is the 23rd character, matching the `capacity()` of 22 reported for an empty string. The inline capacity counts **characters**, not bytes, so it shrinks for wider character types: libc++ reports 10 for an empty `std::u16string`, and libstdc++'s formula divides its 15 by `sizeof` of the character type. ## Practical consequences - **Do not tune to the threshold.** "Keep names under 16 characters" is a guess about one library on one platform. - **Benchmarks lie at the edges.** A string that crosses the inline capacity starts allocating, so a microbenchmark on 10-character keys says little about 40-character URLs. - **`sizeof` grows.** A container of a million empty strings still pays 24 or 32 bytes each. - **Never keep pointers across a move or swap** of a string; take them again from the destination. SSO is a design trade: a bigger object and a branch on every access in exchange for no allocation for the most common strings.
- Why can moving a std::string be no faster than copying it?When the text fits in the inline buffer there is no heap pointer to transfer, so the move constructor copies the characters into the destination's own buffer, exactly what a copy does. The move avoids no allocation because none was needed. For strings past the inline capacity the move steals the heap buffer in constant time, which is where `std::move` pays off.
- How would you find a library's inline capacity without reading its source?Construct an empty `std::string` and print `capacity()`: with SSO it is the inline capacity even though nothing was allocated. You can confirm with a counting `operator new` that appending up to that many characters allocates nothing and the next character allocates. Treat the number as a diagnostic, never as a constant to code against.
- The C++ standard guarantees 15 characters of small-string storage.
- Moving a std::string always transfers its buffer pointer in constant time.
- A pointer into a string stays valid after std::move moves the value elsewhere.
- swap on std::string keeps every pointer valid, exactly as vector's swap does.
- An empty std::string always has capacity() of zero.