AoS vs. SoA: Best Layout for Your Access Pattern

Array of Structures (AoS) is optimal when operations frequently access all fields of a single object, benefiting from spatial locality. Conversely, Structure of Arrays (SoA) excels for batch operations on specific fields across many objects, maximizing cache hits for algorithms that iterate over a subset of data.

The top 3

  1. AoS: Best for Single-Entity, Full-Data Access: Array of Structs (AoS) is optimal when your code frequently accesses all members of a single 'object' or entity contiguously. This pattern leverages cache lines efficiently by bringing the entire object's data into the CPU cache at once, minimizing cache misses for subsequent accesses to that same object's fields.
  2. SoA: Unmatched for Columnar, Data-Parallel Operations: Struct of Arrays (SoA) excels when processing a single attribute across many entities, particularly with SIMD instructions or GPU computing. By grouping identical data types, SoA ensures contiguous memory access for that attribute, maximizing cache hits and enabling efficient vectorized processing, as seen in columnar databases.
  3. Hybrid Layouts: Balancing Mixed Access Patterns: For systems with diverse or unpredictable access patterns, hybrid data layouts like Array of Structs of Arrays (AoSoA) or tiled AoS/SoA combine the strengths of both. These strategies often involve grouping frequently co-accessed attributes into smaller AoS blocks within a larger SoA structure, optimizing for both entity-centric and attribute-centric operations.

Sources

Open the full topic