← Sun, Sep 27 digest

Writing Efficient C++ Code (2013)

read article ↗ hn thread ↗

Achieving peak C++ performance requires shifting from pointer-heavy object-oriented abstractions to data-oriented design centered on cache locality and contiguous memory layouts.

read it Provides a clear, foundational breakdown of hardware memory hierarchies and practical data-oriented design principles that remain valuable for systems programmers.

summary

The article explores how to write high-performance C++ by aligning code with modern hardware memory hierarchies. It contrasts scattered, pointer-heavy object graphs with contiguous layouts like vectors, detailing how cache misses heavily degrade performance. Practical strategies include using Structure-of-Arrays (SOA) over Array-of-Structures (AOS), minimizing dynamic allocations, and bypassing unnecessary abstractions. Ultimately, it argues that data layout must dictate design rather than treating object-oriented patterns as dogma.

key points

  1. Main memory latency costs hundreds of processor cycles, making cache misses a primary performance bottleneck compared to raw instruction counts.
  2. Contiguous containers like std::vector vastly outperform pointer-linked structures like trees or lists because hardware prefetches entire cache lines.
  3. Structure-of-Arrays (SOA) layouts group frequently accessed data together, optimizing throughput for workloads like particle systems.

who should read it

C++ developers and systems engineers interested in hardware-conscious design and memory optimization.

discussion positive

consensus

Commenters widely agree that replacing pointer-heavy data structures with flat arrays for cache locality is timeless, highly practical advice.

pushback

A minority point out that modern C++ core guidelines can discourage these low-level patterns, while others argue that standard code is already fast enough for most everyday use cases.

best comment by glouwbug ↗

True, but moving from a list of unique polymorphic pointers to a std::variant gains you at least a 2-3x speed up in terms of TLB and cacheline locality. From there, swapping to SOA will net you another 4-8x, so you're looking at nearly 25x improvement by going data first. That may not matter in the unique case of say, games, where rendering a million entities will dwarf the cost of SIMD processing a million entities, but in something like numerical simulations (fluids) or quant it will be warmly welcomed

It quantifies the performance gains of moving from polymorphic pointers to modern data-oriented constructs like std::variant and SOA.

Generated by an LLM from the article and thread. Check the sources before relying on it.