1. Problem It Solves
Cache locality rewards nearby, predictable memory access; data-oriented design arranges data around the hot operation rather than an object hierarchy. It makes an important constraint visible instead of leaving readers to guess. This lesson keeps only the C++11 core that fits one focused day.
2. Prerequisites
The ideas from Day 51, plus basic variables, functions, and output already introduced.
3. Core Idea
Mental model: Cache locality rewards nearby, predictable memory access; data-oriented design arranges data around the hot operation rather than an object hierarchy. Identify the relevant value or state, who owns it, and whether the rule acts during compilation or execution.
4. Minimal Syntax
for (std::size_t i=0; i<x.size(); ++i) x[i] += vx[i];5. How It Works
The example creates a tiny fixed state with no keyboard input.
C++11 or the standard-library contract applies today's rule.
The program prints the important result so it can be checked against the source.
6. Common Mistakes
Splitting every field into separate arrays can hurt operations that always need all fields, so layout must follow measured access patterns.
7. When to Use It
Use it when a hot loop repeatedly touches only a subset of many records.
Avoid it when it hides ownership, lifetime, type, ordering, or cost.
8. Simple Example
Separate contiguous position and velocity arrays update only the fields needed by one hot loop and print a deterministic checksum. The .cpp keeps the data fixed and avoids unrelated abstraction.
Complete sample code
Source file
cpp11/52_cache_locality_data_oriented_design/main.cpp
#include <cstddef>
#include <iostream>
#include <vector>
int main() {
// Structure of arrays: the hot loop touches contiguous fields only.
std::vector<double> x{0.0, 10.0, 20.0};
std::vector<double> y{1.0, 11.0, 21.0};
std::vector<double> velocity_x{2.0, 2.0, 2.0};
std::vector<double> velocity_y{3.0, 3.0, 3.0};
double checksum = 0.0;
for (std::size_t i = 0; i < x.size(); ++i) {
x[i] += velocity_x[i];
y[i] += velocity_y[i];
checksum += x[i] + y[i];
}
std::cout << "checksum=" << checksum << '\n';
}
9. Key Takeaways
The feature is part of the C++11 scope used in this course.
Understand its lifetime, ownership, type, and ordering consequences.
Compile with warnings and prefer the smallest form that makes the rule obvious.
10. Self-Check Questions
Easy — What is cache locality? For a loop that updates positions from velocities, why might contiguous arrays of the needed fields help compared with visiting scattered objects?
Medium — Read the small example described above. What value or state should it print, and which rule produces that result?
Hard — Find and explain the subtle bug in this situation: Splitting every field into separate arrays can hurt operations that always need all fields, so layout must follow measured access patterns. What is the smallest C++11-safe correction?