What is the difference between array-of-structures and structure-of-arrays layouts, and why does SoA speed up a movement system over fifty thousand units?
answer
- what one cache line brings in
- bytes used per line fetched
- one array per field
- lanes for vector instructions
- blocks of eight: AoSoA
basics
~20 sArray-of-structures stores each unit's fields together; structure-of-arrays stores each field in its own array. A movement system reads only positions and velocities, so SoA streams just those bytes and fills each fetched cache line with useful data.
solid answer
~50 sIn **AoS** each element is a full record (position, velocity, health, AI state), so a loop that only updates positions drags every other field through the cache with it. In **SoA** each field lives in its own contiguous array, so a movement system over fifty thousand units reads exactly the position and velocity arrays in order: almost every byte of every cache line it pulls in is useful, the hardware prefetcher sees simple linear streams, and consecutive values line up for SIMD, so one instruction updates four or eight units. AoS still wins when code touches most fields of one element at a time or accesses single records at random. A hybrid, **AoSoA**, stores small fixed-size blocks in SoA form to keep SIMD width while keeping one element's fields close together. An ECS's per-component storage is SoA at component granularity.
code
pseudocode · 9 lines// AoS: one array of complete unit records
for i in 0 .. count - 1:
units[i].position = units[i].position + units[i].velocity * dt
// SoA: one array per field
for i in 0 .. count - 1:
pos_x[i] = pos_x[i] + vel_x[i] * dt
pos_y[i] = pos_y[i] + vel_y[i] * dt
pos_z[i] = pos_z[i] + vel_z[i] * dtgo deeper
Recall what each layout puts next to what, and that the processor fetches whole cache lines rather than single values.
Work the bytes-used-per-line arithmetic for a concrete record, explain prefetching on linear streams, and say why SoA lines up values for SIMD lanes.
Choose the layout from the access pattern a profile shows, including when AoS or AoSoA wins, and admit the gain is bounded by how memory-bound the loop really is.
Decide where a codebase pays the readability cost of SoA or AoSoA: only for the loops that dominate frame time, with measurements before and after.
## Two ways to lay out the same data Take a swarm of fifty thousand units, each with a position, a velocity and some other state. There are two basic ways to put them in memory. - **Array of structures (AoS):** one array whose elements are complete records. Unit 0's position, velocity, health and AI state sit together, then unit 1's, and so on. - **Structure of arrays (SoA):** one array per field. All x positions sit together, then all y positions, and so on for every field. Both hold the same values and, apart from padding inside records, the same number of bytes. What differs is which bytes sit next to each other, and that decides how much of what the processor fetches is actually used. ## Why adjacency matters: cache lines A processor does not read single values from main memory. It moves fixed-size **cache lines**, typically 64 bytes on current desktop and console processors, into its caches, and a load that misses waits for the whole line. A loop that walks memory linearly also lets the **hardware prefetcher** request the next lines before they are needed. So the useful question for a hot loop is: of every line it pulls in, how many bytes does it use? ## Working the swarm example Assume, as a scenario, a 64-byte unit record: position 12 bytes (three 4-byte floats), velocity 12, health 4, team 4, target id 8, AI state 16 and a render handle 8. A movement system computes `position = position + velocity * dt` and reads nothing else. 1. **AoS:** each record fills one line. The loop uses 24 of its 64 bytes, which is 37.5 percent, and drags the other 40 bytes through the cache for nothing. Over fifty thousand units it moves 50,000 × 64 bytes = 3.2 MB. 2. **SoA:** the loop reads six float arrays (x, y and z of position and velocity) of 50,000 × 4 bytes each, 1.2 MB in all, and apart from the partial lines at the ends of each array every fetched byte is used. It writes three of them back. 3. When a loop does this little arithmetic per element, memory traffic sets its speed, so the gain can approach the ratio of bytes moved (here about 2.7 times). Only a profile on the target hardware says how close a real loop gets. The second, often larger, win is **SIMD**. With SoA, eight consecutive x positions are already contiguous, so one vector instruction can update four or eight units at once without first gathering scattered fields. With AoS the same values sit 64 bytes apart. ## Where SoA meets ECS An ECS applies SoA at the level of components: all position components live in one array and all velocity components in another, so a system that queries both walks two dense arrays. Inside a component the fields still sit together (a position's x, y and z side by side), so each component is a small AoS. Going further, splitting a component's fields into separate arrays, or using **AoSoA** (blocks of, say, eight elements in which each field is its own small array), is an extra step teams take for the hottest SIMD loops. ## When AoS is the right choice SoA is not always better. AoS wins when: - code reads or writes **most fields of one element** together, such as a single unit's full state for a save or a network message; - access is **random and one element at a time**, where AoS needs one line for the record but SoA needs one line per field array; - records are small and every field is read in the loop anyway, so nothing is wasted. | Access pattern | Better layout | Reason | |---|---|---| | One or two fields across many elements | SoA | Fetched lines carry only needed fields, SIMD-friendly | | Most fields of one element | AoS | One record, one or two lines | | Random single-element lookups | AoS | Fewer separate lines per lookup | | SIMD over many elements, several fields | AoSoA | Vector-width runs, one element's fields nearby | ## Common mistakes - Saying SoA is faster because it is smaller. The total is about the same; what changes is how much of each fetched line is useful. - Assuming the processor fetches only what the code reads. It fetches whole lines. - Assuming SoA guarantees vectorisation. It removes the main obstacle; the loop still has to be written so the compiler, or the programmer, can use vector instructions. - Converting everything to SoA. A layout is chosen for the loops that dominate a profile, not as a blanket rule.
- When is AoS the better layout?When code touches most fields of one element together, or reads elements at random one at a time: a record that fits in one cache line arrives in a single miss, whereas SoA would take one miss per field array. Small records whose every field the loop reads are fine as AoS too; the waste appears when a loop reads a small fraction of a wide record.
- What is AoSoA and why would you use it?Array of structures of arrays: data is split into fixed-size blocks, say eight elements, and inside each block every field is its own small array. A loop still loads full vector-width runs of one field, but all fields of one element sit inside one block, so code needing several fields of nearby elements touches fewer distant memory regions than with fully separate arrays.
A personnel office can keep one folder per employee or one ledger per field. To total everyone's salary you read one ledger front to back instead of opening every folder and skipping past addresses and phone numbers; to read one employee's whole record, the folders win.
- SoA is faster because it uses less memory in total.
- The CPU fetches only the bytes the code reads, so unused fields cost nothing.
- Data layout matters only for GPU code, not for CPU game logic.
- SoA is better even when each access needs every field of one record.
- Splitting data into SoA guarantees the compiler will vectorise the loop.