DOP: Structure of Arrays (SoA) vs Array of Structures (AoS)
Organize data in memory to match CPU cache lines and SIMD hardware for orders-of-magnitude faster batch processing.
Intent & Objective
Transform traditional Array-of-Structures (AoS) where each object bundles unrelated fields together, into Structure-of-Arrays (SoA) where identical fields are stored in contiguous memory buffers for optimal CPU cache hits.
"Reading a 1-column spreadsheet of all salaries in seconds vs opening 10,000 thick folders and flipping past medical and address records."
The Problem
CPU cache misses. When iterating over 1,000,000 game particles to update position, traditional OOP loads 64-byte object chunks containing color, mass, texture IDs, and mesh pointers into the L1 cache, wasting 75% of cache bandwidth on data the position calculation never reads.
The Solution
Store fields in separate parallel arrays (`x[]`, `y[]`, `vx[]`, `vy[]`). Process entire streams of data sequentially using SIMD vectorization and prefetching.
Key Participants & Architectural Roles
Code Samples (Traditional OOP vs Functional Programming)
Available in C++, Python, and JavaParadigm Contrast: Traditional OOP vs Functional / Data-Oriented
OOP models domain objects as individual nodes scattered across the heap with virtual table pointers (Array of Structures). Data-Oriented Design (DOD/DOP) arranges data according to how hardware reads it (Structure of Arrays), treating the CPU cache and memory bus as the primary constraint.
Advantages
- β Drastically reduces CPU cache misses and memory bus bandwidth saturation
- β Enables auto-vectorization and SIMD instruction parallelism (AVX-512, NEON)
- β Enormous speedups (5x to 50x) in game engines, physics, and financial quant modeling
Trade-offs
- β Less intuitive to think about than individual cohesive object instances
When to Apply
- β High-performance data pipelines, game physics, particle systems, simulations, and quantitative analysis
- β Batch processing over thousands or millions of records
When to Avoid
- ! CRUD apps querying 1 single user by ID at a time