Lesson 5 of 9 · 23m
Arrays, Structs, and the Shape of Data
Why contiguous arrays usually beat object graphs, when struct-of-arrays wins over array-of-structs, and how to choose by access pattern.
Most application performance is decided before the algorithm starts, at the moment you choose a data shape. Programs built as webs of tiny objects force the CPU to discover memory one pointer at a time. Programs built around dense arrays let the prefetcher see the future. This is not a niche game-engine trick; it is the difference between memory serving the computation and memory governing it.
Array of structs: convenient but sometimes wasteful
struct particle {
float x, y, z;
float vx, vy, vz;
int state;
float mass;
};
struct particle particles[4096];This array-of-structs layout is excellent when a task needs most fields of one particle. Iterating particles reads compact sequential memory. But suppose the physics step only updates positions from velocities. Every cache line still brings state and mass along, wasting bandwidth.
Struct of arrays: bring only what the loop uses
struct particles {
float x[4096], y[4096], z[4096];
float vx[4096], vy[4096], vz[4096];
float mass[4096];
int state[4096];
};Now a loop over x reads a dense stream of x values. This layout also vectorizes better because adjacent lanes correspond to adjacent elements. The cost is ergonomics and sometimes cache behavior when one logical entity truly needs many fields at once.
for (size_t i = 0; i < n; i++) {
particles.x[i] += particles.vx[i] * dt;
particles.y[i] += particles.vy[i] * dt;
}Choose by access pattern, not ideology
Choose by asking which fields are hot together, whether the hot loop scans many entities or one entity deeply, whether bandwidth or random latency is the bottleneck, and whether the layout helps vectorization.
If a request handler needs a whole user object, array-of-structs or a row-oriented representation may be right. If analytics scans one column across millions of rows, columnar layout wins. This is why analytical databases use columnar storage while transactional systems often use rows.
Pointer-heavy structures are latency machines
Linked lists, binary trees, and object graphs can be appropriate when mutation patterns genuinely require them, but each node is a dependent memory access:
node = node->next; // next address unknown until current node arrivesThe CPU cannot prefetch far ahead because the addresses are data-dependent. A dynamic array often wins even when the textbook says a list has better asymptotic insertion cost. Moving bytes in a contiguous array can be cheaper than chasing cache misses.
Field notes
- “Cache-friendly” means the bytes consumed are close to the bytes recently consumed.
- Do not split every struct mechanically; split hot and cold fields or redesign around real loops.
- Structure-of-arrays complicates ownership and resizing. Encapsulate it rather than exposing parallel arrays everywhere.
- Small vectors inside objects often recreate pointer chasing. Consider flattening nested variable-length data.
- Measure both memory traffic and elapsed time. A layout can reduce bytes while complicating the loop enough to lose elsewhere.
Was this lesson helpful?