Memory Layout of Strings
Short-string optimization keeps short string values inside the string object itself and places only longer strings on the free store.
Why a String Has to Decide Where Its Text Lives
Imagine you are writing a string class yourself. Your first version will probably work correctly, but it will often lose to the standard library's string on convenience and speed. One of the main reasons is a design choice the standard implementation makes for you: every time a value is stored, it decides whether the text should live inside the string object or somewhere outside it.
This technique is called short-string optimization. In this article you will follow it through a small example so you can see exactly where each piece of text ends up. By the end you should be able to look at a string value and reason about where its characters are kept, and why that matters for performance.
Inline Storage Versus the Free Store
Short-string optimization keeps short string values inside the string object itself and places only longer strings on the free store.
The core rule is simple to state. A short string value is kept in the string object itself. Only a longer string value is placed on the free store. This means the string object is not always the same kind of thing. For a short value, the object holds the characters directly. For a long value, the object works with storage that lives elsewhere. The layout of the object depends on which of these two situations applies.
Read the diagram as a sketch of structure, not a precise picture. The source gives the layout only in general terms, so it does not show exact sizes or byte counts. What it does show is the key distinction: one object keeps its short text inline, while the other object refers to text stored on the free store.
Tracing the Two Declarations
The source example uses two values: Annemarie, which is shorter, and Annemarie Stroustrup, which is longer. The code below is an original illustration that declares one string object for each value, so you can trace them with the rule from the previous section.
shortNameholds a value that is short enough to be kept inside the string objectlongNameholds a longer value, so its characters are placed on the free store
What do you think happens?
For shortName and longName above, which object keeps its characters inside the object, and which places them on the free store?
Reveal answer
shortName keeps 'Annemarie' inside the object. longName places 'Annemarie Stroustrup' on the free store.
The rule follows the length of the value. The shorter value stays inline, and the longer value is the one placed on the free store, which is exactly the direction the source example shows.
Deciding where each value is kept
Two string objects are created: one holds 'Annemarie' and the other holds 'Annemarie Stroustrup'. For each object, decide where its characters are kept.
- 1Step 1: Identify the value of each object
The first object holds 'Annemarie'. The second holds 'Annemarie Stroustrup'.
- 2Step 2: Compare the lengths
'Annemarie Stroustrup' is the longer of the two values. 'Annemarie' is the shorter one.
- 3Step 3: Apply the storage rule
A short value is kept in the string object itself. Only a longer value is placed on the free store.
- 4Step 4: Assign each object its location
The object holding 'Annemarie' keeps its characters inside itself. The object holding 'Annemarie Stroustrup' has its text placed on the free store.
The shorter value stays inside its object. The longer value goes to the free store.
When a Value Grows or Shrinks
A string does not keep one layout forever. When a string's value changes from short to long, its representation adjusts to match. When the value changes from long back to short, the representation adjusts again. The storage decision follows the current value, not the value the string had when it was first created.
Follow the diagram from left to right. A string that starts with a short value keeps its text inside the object. When its value grows into the long range, the text moves to the free store. If the value later shrinks back to a short length, the representation adjusts again, and the text is kept inside the object once more. The same object can therefore pass through both layouts over its life.
Where Real Performance Comes From
The source makes an important point: the real speed of string operations depends heavily on where the program runs. Short-string optimization is a design answer to that environment, and it is a large part of why it is used so widely. Two environment factors are named in the source as able to change real performance: multi-threaded allocation cost and memory fragmentation from strings of differing lengths.
Multi-threaded allocation cost is about the work needed when storage is requested on the free store while more than one thread of the program is running. Whenever a long value needs free-store storage, that request is an allocation, so the cost of allocating matters more in an environment where several threads make such requests. Keeping short values inside the object avoids that request entirely for those values.
Memory fragmentation is the second factor. The source ties it to strings of differing lengths. When storage of varying sizes is repeatedly requested and released, the free store can end up with gaps between the pieces still in use. Those gaps can make later allocations less efficient, so the same string operation can cost more in a program that has been running a long time than it did at the start.
Mistakes Beginners Make About String Storage
Common mistake Assuming every string object stores its characters in the same place
What it looks likeTreating shortName and longName from the example as if both keep their text inside the object.
Why it failsThe layout depends on the value. The shorter value is kept inside, and the longer value is placed on the free store.
Do this insteadFor each value, apply the rule: short values stay inside the object, and only longer values go to the free store.
Common mistake Assuming the layout is fixed at creation
What it looks likeReasoning that a string created with a short value will keep its text inline even after it is assigned a much longer value.
Why it failsThe representation adjusts when the value changes from short to long, or from long back to short.
Do this insteadAlways reason from the current value of the string, not from the value it had when it was first created.
Common mistake Looking for an exact length cutoff in the source
What it looks likeTrying to memorize a specific number of characters that separates short from long.
Why it failsThe source does not give a numeric cutoff. It only states the direction of the decision: the longer text is placed on the free store.
Do this insteadUse the comparison between the two values to reason about direction, and do not invent a threshold the source does not state.
Common mistake Assuming performance is the same in every environment
What it looks likeExpecting a string operation to cost the same in a single-threaded run and in a program with many threads making allocations.
Why it failsThe source says real performance depends on the run-time environment, especially allocation cost under multiple threads and fragmentation.
Do this insteadTreat the environment as part of the performance picture, and do not assume a fixed cost per string operation.
Checking Your Understanding of Storage Decisions
A program creates a string object with a short value. Later, the same object is assigned a value much longer than the original. Then the value is shortened again. Describe where the characters are kept at each of the three moments, and explain which rule decides each answer.
Hints
- Ask first whether the value is short or long at each moment, because the layout follows the current value.
- Short values are kept inside the string object. Longer values are placed on the free store.
- The representation adjusts when the value changes from short to long, and again when it changes back.
Key Takeaways
- Short-string optimization keeps short string values inside the string object itself and places only longer strings on the free store.
- The layout of a string follows its current value, and it adjusts when the value changes from short to long or back again.
- In the source example, Annemarie is kept inside its object while Annemarie Stroustrup is placed on the free store.
- Real string performance depends on the run-time environment, especially multi-threaded allocation cost and memory fragmentation from strings of differing lengths.