0Pricing
C++ Academy · Lesson

Cache-Friendly Data Layouts

Design structures of arrays and pack data for cache locality.

Cache-Friendly Data Layouts is a free C++ Academy lesson on CoddyKit — lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the C++ Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

The Memory Hierarchy

CPUs have multiple cache levels (L1, L2, L3) much faster than main memory. Cache-friendly code keeps hot data close to the CPU.

Cache Lines

Memory is fetched in cache lines — typically 64 bytes. Reading one byte loads the whole line. Use this to your advantage.

Locality of Reference

Two important properties:

  • Spatial locality — using nearby memory soon
  • Temporal locality — re-using the same memory soon

Contiguous vs Linked

Vectors store data contiguously — iterating them is cache friendly. Linked lists scatter memory, blowing the cache on every step.

// Cache friendly
std::vector<int> v(1000);
for (auto& x : v) ++x;

// Cache UNfriendly
std::list<int> l(1000);
for (auto& x : l) ++x;

AoS vs SoA

Two layouts for arrays of records:

  • AoS (Array of Structs) — natural, but iterating one field touches all fields
  • SoA (Struct of Arrays) — better when most loops use only some fields
// AoS
struct Particle { float x, y, z, vx, vy, vz; };
std::vector<Particle> particles;

// SoA
struct Particles {
    std::vector<float> x, y, z, vx, vy, vz;
};

Struct Packing

Order members from largest to smallest to minimize padding. Tools like pahole show actual layout.

struct Bad  { char c; double d; char c2; };  // padded
struct Good { double d; char c; char c2; };  // smaller

False Sharing

Two threads writing to different variables on the same cache line invalidate each other s caches. Catastrophic for performance. Pad to 64 bytes.

struct alignas(64) Counter {
    std::atomic<int> value;
};

Hot/Cold Data Splitting

Separate hot data (frequently accessed) from cold data (rarely accessed) into different structures. The CPU caches only the hot part.

Pre-Allocation

Pre-allocate vectors with reserve to avoid repeated reallocations. Each reallocation copies all elements — expensive and cache-blowing.

Sequential Access Wins

Linear scans through arrays are fastest. The hardware prefetcher predicts and loads the next cache lines automatically.

Avoid Indirection

Pointers force the CPU to chase dependencies. std::vector<T*> is slower than std::vector<T> for traversal. Use indirection only when necessary.

Profile Before Optimizing

"Cache-friendly" is a guideline, not a rule. Measure with tools like perf or VTune to see where cache misses hurt — then optimize.

Quick Check

Why is iterating a std::vector typically much faster than iterating a std::list of the same size?

Recap

Modern CPUs depend on caches. Prefer contiguous containers, use SoA for selective field access, pack structs, avoid false sharing, and profile cache misses with perf or VTune to find hot spots.

Frequently asked questions

Is the “Cache-Friendly Data Layouts” lesson free?

Yes — the full text of “Cache-Friendly Data Layouts” is free to read here on the web, and the C++ Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the C++ Academy course, upgrade to CoddyKit PRO.

What will I learn in “Cache-Friendly Data Layouts”?

Design structures of arrays and pack data for cache locality. You practise C++ Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start C++ Academy?

No prior experience is required. C++ Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Cache-Friendly Data Layouts” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this C++ Academy lesson?

Yes. Every C++ Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Cache-Friendly Data Layouts
  2. Branch Prediction and Hot Loops
  3. Profiling with perf vtune and Sanitizers
  4. Micro-benchmarking with Google Benchmark
← Back to C++ Academy