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. This is lesson 1 of 4. Any 3 lessons of this learning path are free to read in full — after that, CoddyKit PRO unlocks every lesson, plus hands-on practice with a built-in code editor and a 24/7 AI tutor. It is part of the C++ Academy learning path, and your progress syncs across the web and the CoddyKit app. The C++ Academy course includes 4 lessons in total.
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; }; // smallerFalse 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.
Learn C++ with an AI tutor — free
Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.
- Courses
- 51
- Lessons
- 203
Frequently Asked Questions
Is the “Cache-Friendly Data Layouts” lesson free?
Yes — any 3 lessons of the C++ Academy learning path, including “Cache-Friendly Data Layouts”, are free to read in full here on the web. After that, CoddyKit PRO unlocks every lesson, plus interactive practice with a built-in code editor and a 24/7 AI tutor. The C++ Academy course includes 4 lessons in total.
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, so you can start here or from the beginning and move at your own pace. This is lesson 1 of 4.
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
- Cache-Friendly Data Layouts
- Branch Prediction and Hot Loops
- Profiling with perf vtune and Sanitizers
- Micro-benchmarking with Google Benchmark