How do you turn a C++ particle array-of-structs into a structure-of-arrays, and what does the update loop gain and lose?
answer
- one std::vector per field
- the loop reads only what it needs
- unit stride lets the compiler vectorise
- keeping N vectors the same size
- views::zip gives back a row
basics
~20 sReplace std::vector<Particle> with a struct holding one std::vector per field, all the same length. A loop that updates positions then streams only x, vx and friends, densely and vectorisably; you lose the Particle object, single-call insertion and cheap whole-particle access.
solid answer
~50 sArray-of-structs is `std::vector<Particle>` with `Particle {x, y, z, vx, vy, vz, colour, age, ...}`; structure-of-arrays is `struct Particles { std::vector<float> x, y, z, vx, vy, vz; ... };` where index `i` in every vector is particle `i`. The position update then reads `x` and `vx` as two dense float arrays: every byte fetched is used, the stride is one, and the compiler can turn the loop into SIMD instructions. The price is real: there is no `Particle&` any more, so code that wants one particle reads N arrays; adding or removing a particle must touch every vector and keep their sizes equal; and each vector is its own allocation. C++23's `std::views::zip(ps.x, ps.vx)` recovers a row-like view by yielding tuples of references. SoA wins when hot loops touch a few fields of many particles; AoS wins when code mostly works on whole particles.
code
cpp · 32 lines#include <cstddef>
#include <cstdio>
#include <ranges>
#include <vector>
// Structure of arrays: one std::vector per field, all the same size.
struct Particles {
std::vector<float> x, y, z;
std::vector<float> vx, vy, vz;
void push_back(float px, float py, float pz) {
x.push_back(px); y.push_back(py); z.push_back(pz);
vx.push_back(1.0f); vy.push_back(0.5f); vz.push_back(0.0f);
}
std::size_t size() const { return x.size(); }
};
void integrate(Particles& ps, float dt) {
// C++23: views::zip yields a tuple of references per index.
for (auto [px, v] : std::views::zip(ps.x, ps.vx)) px += v * dt;
for (std::size_t i = 0; i < ps.size(); ++i) { // the index form works in C++17
ps.y[i] += ps.vy[i] * dt;
ps.z[i] += ps.vz[i] * dt;
}
}
int main() {
Particles ps;
for (int i = 0; i < 3; ++i) ps.push_back(float(i), 0.0f, 0.0f);
integrate(ps, 0.5f);
std::printf("%g %g %g\n", ps.x[0], ps.x[2], ps.y[1]);
}go deeper
Know the two shapes: a vector of particle structs versus one vector per field, with index i meaning the same particle everywhere.
Explain why a few-field loop reads fewer bytes and vectorises in SoA, and list what you lose: the object, lock-step insertion and removal, more allocations.
Show how you keep SoA correct in a codebase: encapsulation, swap-and-pop, reserve together, zip or index loops, and measuring before converting.
Decide where the layout boundary sits: engine systems own SoA storage behind a small API so gameplay code keeps an object view.
## The two layouts in C++ **Array of structs (AoS)** is what object-oriented C++ produces by default: ```cpp struct Particle { float x, y, z, vx, vy, vz; float r, g, b, a; float age, life; }; std::vector<Particle> particles; // x y z vx vy vz r g b a age life | x y z ... ``` **Structure of arrays (SoA)** turns every field into its own sequence: ```cpp struct Particles { std::vector<float> x, y, z, vx, vy, vz, r, g, b, a, age, life; // index i = particle i }; ``` Nothing in the language enforces that `x[i]` and `vx[i]` belong to the same particle; that **invariant** is now yours to keep. ## What the update loop gains An entity-component update for 1,000,000 particles typically runs `x += vx * dt` and friends every frame. In the AoS layout above each particle is 48 bytes, of which the position update reads 24, so half of every cache line fetched is colour and age data the loop throws away. In SoA: - **Every fetched byte is used.** `x` and `vx` are dense `float` arrays, so a 64-byte line carries 16 useful values. - **Unit stride.** Consecutive iterations read consecutive addresses, which the prefetcher follows easily. - **Auto-vectorisation.** A loop of the form `x[i] += vx[i] * dt` over plain float arrays is the shape compilers vectorise most readily, processing 4, 8 or 16 floats per instruction depending on the target. - **Per-field passes.** Systems that only read `age` touch only `age`. One run on this author's machine of a one-field update over a million 80-byte particles took roughly a tenth of the AoS time with SoA; with a smaller struct the gap shrinks, which is the honest way to state it: SoA helps in proportion to the bytes a loop would otherwise drag in and discard. ## What you give up | Concern | AoS `std::vector<Particle>` | SoA `struct Particles` | |---|---|---| | A single particle as a value | `Particle&` | no object; read N arrays or build one | | Add or remove | one `push_back` / `erase` | every vector, in lock-step | | Invariant | the type keeps fields together | equal sizes are a convention you must guard | | Allocations | one buffer | one per field, unless you carve them from one block | | Whole-particle access (spawning, debugging) | one or two cache lines | up to one line per field | Practical rules for keeping SoA honest: 1. **Encapsulate it.** Make the vectors private and give the struct `push_back`, `swap_remove(i)` and `size()` that update all fields together, so no caller can desynchronise them. 2. **Remove with swap-and-pop** in every field: copy the last element into slot `i`, then `pop_back`, keeping the arrays dense and O(1) to erase. 3. **Reserve all fields together** with the known capacity so they grow in step and do not reallocate mid-frame. ## Getting a row back with C++23 views::zip C++23 added `std::views::zip`, which iterates several ranges in step and yields a tuple of references per index: ```cpp for (auto [px, v] : std::views::zip(ps.x, ps.vx)) px += v * dt; ``` The structured bindings name references into the two vectors, so the assignment writes through to `ps.x`. Before C++23 the plain index loop does the same job. A zip view stops at the shortest range, so it quietly hides a size mismatch rather than reporting it; an assertion in the struct's own methods is still worth having. ## Choosing between them - Pick **SoA** for hot loops that touch a few fields of many elements: physics integration, culling, particle simulation. - Keep **AoS** where code handles whole objects one at a time: gameplay logic on a handful of entities, UI, serialisation. - A **hybrid (AoSoA)** groups fields into small fixed blocks, for example 8 particles' `x` values together, then their `y` values, to match SIMD width while keeping one particle's data nearby. - Splitting only the rarely used fields away is **hot/cold splitting**, a lighter step than full SoA and often enough. The language-neutral reasoning about records versus parallel arrays belongs to the material on arrays and cache locality; what this question asks is how you build and maintain the layout in C++.
- How do you delete particle i from the structure of arrays in O(1) without breaking the index invariant?Swap-and-pop every field: copy the last element of each vector into slot `i` and `pop_back` each one, all inside one member function so no caller can update only some fields. The order of particles changes, which is fine for a simulation that does not depend on order; anything holding the old last index must be told it moved, so stable handles map to a slot through an indirection table.
- Why might the compiler still not vectorise x[i] += vx[i] * dt over two std::vector<float> members?The two data pointers could, as far as the compiler knows, overlap, so it either adds a runtime overlap check before a vector path or stays scalar. Floating-point reductions, such as summing into one float, are also kept in order unless reassociation is allowed. Reading the compiler's vectorisation remarks or its assembly tells you which happened.
- Structure-of-arrays is always faster, whatever the loop reads.
- The compiler converts a vector of structs into separate arrays when it would help.
- std::views::zip copies each element into a tuple, so writes do not reach the vectors.
- Keeping the field vectors the same size is guaranteed by std::vector itself.
- SoA saves memory because the struct's fields no longer need to be stored.
Assumes C++23 for std::views::zip; the structure-of-arrays layout and the index loop work in C++17.