Flat containers for small data vector illustration.

For Twenty Elements a Sorted Vector Beats Every Tree

I spent six years in high-frequency trading watching developers treat `std::list` or complex tree structures like they were some kind of architectural masterstroke, only to watch the profiler scream during market volatility. There is this pervasive, misguided belief that abstraction equals quality, but when you’re dealing with tiny datasets, those heavy abstractions are just expensive lies told to your CPU. Most people ignore the reality that pointer chasing is a death sentence for performance; if you aren’t utilizing flat containers for small data, you are effectively paying a massive tax to your L1 cache for no reason at all.

I’m not here to give you another textbook lecture on Big O notation or academic complexity classes. My goal is to show you how the hardware actually reacts when you swap out those bloated, node-based structures for something contiguous. I will walk you through the specific mechanics of why flat containers for small data win in the real world, focusing on the mechanical sympathy required to keep your data close to the execution units. We’ll look at the actual memory layout, the cache line implications, and exactly when the cost of a realloc finally outweighs the benefits of a simple vector.

Table of Contents

Cache Locality Optimization Respecting the Hardwares Intent

Cache Locality Optimization Respecting the Hardwares Intent

The hardware doesn’t care about your elegant class hierarchies; it cares about where the bytes live. When you use a linked list for a handful of small objects, you aren’t just wasting a few bytes of pointer overhead—you are actively sabotaging the prefetcher. Every time you follow a pointer to a new node, you risk a cache miss that stalls the pipeline for hundreds of cycles. In my experience, the performance gap between a pointer-chasing structure and contiguous memory allocation for small data is often the difference between a system that feels snappy and one that’s constantly waiting on the memory controller.

To get real performance, you have to embrace cache locality optimization. By keeping your data in a single, dense block, you ensure that when the CPU fetches one element, it inadvertently pulls the next several elements into the L1 cache along with it. This isn’t just a micro-optimization; it’s about respecting the hardware’s intent. If your dataset fits within a few cache lines, a simple array or a specialized flat map will outperform a “sophisticated” tree-based structure every single time. Stop fighting the cache.

Vector vs Linked List Performance the Pointer Tax

Vector vs Linked List Performance the Pointer Tax

The classic debate of vector vs linked list performance usually centers on Big O notation, but in a production environment, Big O is a lie told by academics. If you are working with small datasets, the constant factors matter more than the asymptotic complexity. A `std::list` is a collection of scattered nodes, each wrapped in its own heap allocation. This is the “pointer tax”: every time you traverse a link, you aren’t just moving to the next element; you are gambling that the next node is already sitting in your L1 cache. Most of the time, it isn’t.

When you opt for a `std::vector`, you are committing to contiguous memory allocation for small data. This isn’t just about being tidy; it’s about predictability. The CPU prefetcher sees a linear access pattern and starts pulling the next chunk of data into the cache before you even ask for it. By using a flat structure, you eliminate the overhead of chasing pointers across the heap, turning a series of potential cache misses into a single, streamlined stream of instructions. In low-latency systems, predictability is the only metric that actually keeps the lights on.

The Rules That Bite: How to Actually Implement Flatness

  • Stop using `std::list` for everything. If your data set fits in a few cache lines, the pointer chasing in a linked list isn’t just slow; it’s actively fighting your CPU’s prefetcher. Use `std::vector` or a fixed-size `std::array` and let the hardware do its job.
  • Watch your alignment. A flat container is only as good as its memory layout. If you’re packing small structs into a vector but they’re poorly aligned, you’re just trading pointer indirection for unnecessary cache line splits.
  • Beware the “Small Vector” trap. Using something like `boost::small_vector` is great for avoiding heap allocations, but if you don’t know your upper bound, you’ll end up triggering a move to the heap anyway—at which point you’ve paid the cost of both an abstraction and a reallocation.
  • Prefer `std::array` when the size is known at compile time. If you don’t need the heap, don’t touch it. A `std::array` keeps everything on the stack, keeping your data contiguous and your memory management predictable.
  • Don’t over-engineer the abstraction. I’ve seen people build complex, template-heavy “smart” containers to manage small data sets, only to realize they’ve added layers of indirection that the compiler can’t optimize away. Keep the container simple; let the compiler’s optimizer see the loop, not your wrapper.

The Cost of Indirection

Stop defaulting to node-based containers for small collections; the pointer chasing required to traverse them is a death sentence for your instruction pipeline.

Respect the cache hierarchy by keeping your data contiguous; if your data isn’t sitting in a single cache line, you’re essentially paying a tax on every single access.

Choose flat containers when your data fits in a predictable footprint; the abstraction of a linked list is rarely worth the latency penalty once you’re in a production environment.

The Bottom Line

At the end of the day, the choice between a linked structure and a flat container isn’t about elegance or following textbook design patterns; it’s about whether you want to pay the pointer tax every time your CPU tries to fetch a single byte. We’ve seen how the overhead of node-based allocation destroys your cache locality and how the “flexibility” of a linked list is often just a euphemism for stalling the pipeline. If your data set is small, the abstraction cost of a heavy, heap-allocated container is a luxury you simply cannot afford if you care about predictable latency.

Stop treating the hardware as an abstract black box that magically handles your inefficiencies. C++ gives you the tools to map your data structures directly to the way memory actually works, but that requires you to stop thinking in terms of high-level metaphors and start thinking in terms of cache lines and contiguous blocks. When you align your code with the physical reality of the machine, you aren’t just optimizing—you’re finally writing software that respects the silicon it runs on.

About Ruaridh Kensington-Oyelaran

C++ rewards people who know what the compiler is allowed to do. I write about the rules that bite, the ones nobody mentions until you have already shipped the bug.

More From Author

Documenting C++ interfaces through code signatures.

Document the Precondition, the Signature Says the Rest