Small Object Optimization

Understand how dynamic memory allocation can impact performance for containers, and the concept of small object optimization.

Performance implications of dynamic memory for containers

One of the great things about containers such as std::vector is that they automatically allocate dynamic memory when needed. Sometimes, though, using dynamic memory for container objects that only contain a few small elements can hurt performance. It would be more efficient to keep the elements in the container itself and only use stack memory instead of allocating small regions of memory on the heap. Most modern implementations of std::string will take advantage of the fact that a lot of strings in a normal program are short and that short strings are more efficient to handle without the use of heap memory.

One alternative is to keep a small separate buffer in the string class itself, which can be used when the string’s content is short. This would increase the size of the string class, even when the short buffer is not used.

So, a more memory-efficient solution is to use a union, which can hold a short buffer when the string is in short mode and, otherwise, hold the data members it needs to handle a dynamically allocated buffer. The technique for optimizing a container for handling small data is usually referred to as small string optimization for strings, or small object optimization and small buffer optimization for other types. We have many names for the things we love.

Coding example

A short code example will demonstrate how std::string from libc++ from LLVM behaves on our 64-bit platform:

Get hands-on with 1200+ tech skills courses.