Catalog / Data-Oriented Patterns / DOP: Structure of Arrays (SoA) vs Array of Structures (AoS)
DOP

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.

πŸ‘₯ Real-World Human Analogies & Social Roles
1 Intuitive Scenarios
Employee Salary Spreadsheet vs Thick Folders Office Administration

"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

Array of Structures (AoS)
Traditional OOP layout: `vector<Particle>` (poor cache locality for single-field operations)
Structure of Arrays (SoA)
Data-oriented layout: `Particles{ vector<float> x; vector<float> y; }`
Batch Processing Kernel
Tight loop iterating over contiguous memory vectors

Code Samples (Traditional OOP vs Functional Programming)

Available in C++, Python, and Java

Paradigm 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