What is small-string optimization, which properties are implementation-defined, and when can it affect performance?
Show answer and explanation
Small-string optimization stores sufficiently short character sequences inside the string object instead of a separate allocation. The Standard does not require SSO or specify its threshold, object layout, capacity growth, move behavior, or whether a particular value allocates, so code must not use those observations as a contract. It can reduce allocation and improve locality for short strings, but a larger string object increases copy traffic and crossing the inline threshold changes costs. Use ordinary string semantics, respect iterator and pointer invalidation rules, and benchmark representative lengths on the target library.
Small-string optimization stores sufficiently short character sequences inside the string object instead of a separate allocation. The Standard does not require SSO or specify its threshold, object layout, capacity growth, move behavior, or whether a particular value allocates, so code must not use those observations as a contract. It can reduce allocation and improve locality for short strings, but a larger string object increases copy traffic and crossing the inline threshold changes costs. Use ordinary string semantics, respect iterator and pointer invalidation rules, and benchmark representative lengths on the target library.
Connect the conclusion directly to the type, lifetime, ownership, overload-resolution rule, or library contract present in the question. State implementation dependence or a violated precondition explicitly instead of merely guessing output.