Learning path

Full curriculum

Full curriculum

Unit content

Array of structures and structure of arrays

Suppose many entities each contain several fields such as position, velocity and temperature. Two common layouts are an array of structures (AoS) and a structure of arrays (SoA).

Array of structures

AoS stores all fields of one entity together:

[x y t][x y t][x y t]...

This is convenient when operations usually need most fields of one entity at a time.

Structure of arrays

SoA stores each field contiguously:

[x x x ...]
[y y y ...]
[t t t ...]

This can improve locality when a loop processes one or two fields across many entities because cache lines contain more immediately useful values.

Neither layout is universally better

The useful arrangement follows the access pattern. Code that constantly needs complete records may prefer AoS, while bulk operations on selected fields may favour SoA or hybrid layouts.

Representation can follow use

Changing layout does not change the mathematical entities being represented. It changes how their data is grouped in memory, which can alter cache traffic, vectorization opportunities and update complexity.