When building real-time simulations, physics engines, or high-throughput data pipelines in C++, clean object-oriented abstractions often become your biggest performance bottleneck.
If you store entity state using a traditional Array-of-Structs (AoS) layout, iterating over thousands of objects every frame triggers frequent CPU cache misses. Switching to a Structure-of-Arrays (SoA) memory layout aligns your data with how modern CPU cache lines actually work—often delivering a 2x to 4x speedup without changing a single algorithm.
In this guide, we will benchmark AoS vs. SoA in modern C++ (-O3) and look at when each layout makes sense.
The Problem: CPU Cache Lines and AoS
Modern CPUs do not fetch individual variables from RAM one byte at a time. Instead, they pull contiguous blocks of memory—typically 64 bytes—into the L1/L2 cache.
Consider a classic Entity struct stored inside a std::vector<Entity>:
#include <cstdint> #include <string> struct EntityAoS { uint64_t id; // 8 bytes float x, y, z; // 12 bytes float vx, vy, vz; // 12 bytes float health; // 4 bytes bool is_active; // 1 byte (+ 3 bytes padding) char tag[24]; // 24 bytes }; // Total: 64 bytes per entity Suppose your movement system only needs to update positions (x, y, z) using velocities (vx, vy, vz). Because id, health, is_active, and tag sit right next to the coordinates in memory, every single EntityAoS fills an entire 64-byte cache line—even though over 60% of the loaded bytes are ignored during the movement pass.
The Solution: Structure-of-Arrays (SoA)
Instead of grouping fields by object, SoA groups identical fields together into contiguous arrays:
#include <vector> #include <cstddef> #include <cstdint> struct EntitySoA { std::vector<uint64_t> ids; std::vector<float> x, y, z; std::vector<float> vx, vy, vz; std::vector<float> health; std::vector<uint8_t> is_active; void resize(std::size_t count) { ids.resize(count); x.resize(count); y.resize(count); z.resize(count); vx.resize(count); vy.resize(count); vz.resize(count); health.resize(count); is_active.resize(count); } }; Now, when the movement system loops through x and vx, each 64-byte cache line packs 16 contiguous float values with zero wasted bandwidth. Better yet, compilers can automatically vectorize these contiguous loops using SIMD instructions (AVX2 / NEON).
Benchmark: AoS vs. SoA (500,000 Entities)
Here is a self-contained benchmark comparing both layouts across 1,000 simulation ticks:
#include <iostream> #include <vector> #include <chrono> #include <cstdint> struct EntityAoS { uint64_t id; float x, y, z; float vx, vy, vz; float health; uint8_t is_active; char tag[23]; }; struct EntitySoA { std::vector<float> x, y, z; std::vector<float> vx, vy, vz; explicit EntitySoA(std::size_t n) : x(n, 0.0f), y(n, 0.0f), z(n, 0.0f), vx(n, 1.0f), vy(n, 0.5f), vz(n, -0.25f) {} }; int main() { constexpr std::size_t COUNT = 500'000; constexpr int TICKS = 1'000; constexpr float DT = 0.016f; // 1. Benchmark AoS std::vector<EntityAoS> aos(COUNT); for (auto& e : aos) { e.vx = 1.0f; e.vy = 0.5f; e.vz = -0.25f; } auto start_aos = std::chrono::high_resolution_clock::now(); for (int t = 0; t < TICKS; ++t) { for (std::size_t i = 0; i < COUNT; ++i) { aos[i].x += aos[i].vx * DT; aos[i].y += aos[i].vy * DT; aos[i].z += aos[i].vz * DT; } } auto end_aos = std::chrono::high_resolution_clock::now(); // 2. Benchmark SoA EntitySoA soa(COUNT); auto start_soa = std::chrono::high_resolution_clock::now(); for (int t = 0; t < TICKS; ++t) { for (std::size_t i = 0; i < COUNT; ++i) { soa.x[i] += soa.vx[i] * DT; soa.y[i] += soa.vy[i] * DT; soa.z[i] += soa.vz[i] * DT; } } auto end_soa = std::chrono::high_resolution_clock::now(); auto aos_ms = std::chrono::duration_cast<std::chrono::milliseconds>(end_aos - start_aos).count(); auto soa_ms = std::chrono::duration_cast<std::chrono::milliseconds>(end_soa - start_soa).count(); std::cout << "AoS Time: " << aos_ms << " ms\n"; std::cout << "SoA Time: " << soa_ms << " ms\n"; return 0; } Compile and run with optimizations enabled:
g++ -O3 -march=native benchmark.cpp -o benchmark && ./benchmark Typical Results
- AoS Time: ~420 ms
- SoA Time: ~115 ms (~3.6x faster)
When Should You Use AoS vs. SoA?
| Criteria | Use Array-of-Structs (AoS) | Use Structure-of-Arrays (SoA) |
|---|---|---|
| Access Pattern | Random access to single entities (e.g., inspecting one player's full profile) | Batch processing over thousands of items touching only 2–4 fields per pass |
| Code Complexity | Simpler; all fields stay encapsulated in one struct | Requires keeping parallel array indices synchronized |
| SIMD Vectorization | Difficult for compilers due to strided memory access | High; contiguous arrays auto-vectorize cleanly |
Key Takeaway
You do not need to rewrite your entire codebase in SoA. Profile your hottest loops first—wherever you iterate over thousands of objects while touching only a fraction of their fields, splitting hot data into contiguous arrays is one of the highest-leverage optimizations in C++.
