Sub topic 8
Array of structs versus struct of arrays
This chapter assumes you know C structs and pointers, and that you have read the earlier chapters on cache lines, spatial locality, and padding. A cache line is the 64-byte block (a common size, not a guaranteed one) that the CPU loads from memory in one transfer.
Two ways to store the same records
Suppose you store many particles. Each has a position, a velocity, and an id. There are two common layouts.
- Array of structs (AoS): one array whose elements are whole records. All fields of one particle sit next to each other.
- Struct of arrays (SoA): one struct that holds a separate array per field. All values of one field sit next to each other.
AoS matches how people think about objects. SoA matches how some loops read data.
Why the layout changes cache misses
The deciding question is which fields a loop reads. A cache line is loaded whole, so every byte in it costs memory bandwidth, even bytes the loop ignores.
In the AoS example below, a particle is 28 bytes. A loop that sums only x uses 4 of those 28 bytes. Each 64-byte line holds about two particles, so roughly 8 useful bytes arrive per line and the rest is wasted. In the SoA version, the x values are contiguous, so a line holds 16 floats and all 16 are used.
#include <stddef.h>
/* Array of structs: summing x also pulls y, z, and the rest into cache. */
struct ParticleAos {
float x, y, z;
float vx, vy, vz;
int id;
};
float sum_x_aos(const struct ParticleAos *p, size_t n) {
float s = 0.0f;
for (size_t i = 0; i < n; i++)
s += p[i].x;
return s;
}
/* Struct of arrays: each field is stored contiguously. */
struct ParticlesSoa {
float *x, *y, *z;
float *vx, *vy, *vz;
int *id;
size_t n;
};
float sum_x_soa(const struct ParticlesSoa *p) {
float s = 0.0f;
for (size_t i = 0; i < p->n; i++)
s += p->x[i];
return s;
}The SoA loop touches about 4 bytes per element instead of streaming 28, so it needs far fewer cache line loads. Contiguous floats are also easy for the compiler to vectorize.
When each layout wins
| Access pattern | Better layout | Reason |
|---|---|---|
| Loop reads one or two fields of many records | SoA | Every loaded byte is used |
| Code reads most fields of one record at a time | AoS | One record fits in one or two lines |
| Random lookup of a single record by id | AoS | One miss brings in the whole record |
| Vectorized math over one field | SoA | Contiguous values suit SIMD |
SoA has costs. Reading all fields of record i touches one line per array, so seven fields can mean seven misses. Adding or removing a record means updating every array. The code is also less natural to write.
Exercise
A loop reads only vx from 1,000,000 ParticleAos records (28 bytes each, 64-byte lines). Roughly how many bytes of cache lines must be loaded in AoS versus SoA, and which layout is better for this loop?
Show answer
AoS streams the whole array, about 28 MB (about 437,500 lines). SoA reads only the vx array, about 4 MB (about 62,500 lines). SoA loads roughly 7 times less data, so it is better for this loop.