Developer knowledge network · moderated exchange

UnreliableCode Topluluğu

Geliştirici Araştırması, Tersine Mühendislik ve Kodlama Topluluğu

Knowledge indexCanlı
4Categories
919Threads
2.8KGönderiler
Discussion

Understanding Cache Locality: Array of Structures (AoS) vs Structure of Arrays (SoA) [StackOverflow Architecture Guide]

profiler_pat
Performance Hunter
MEMBER
Temsilci: 146
Katılım Tarihi: Aug 2019
Gönderiler: 33
Teşekkürler: 31
1 ay önce · Jul 24, 2026 6:43 AM
#1

Why data layout matters more than asymptotic time complexity for CPU cache performance:

  • Array of Structures (AoS): struct Particle { float x, y, z; int id; char name[32]; }; std::vector<Particle> particles;
    Iterating over positions loads 48 bytes per particle into CPU cache lines, wasting 75% bandwidth on unused name data!
  • Structure of Arrays (SoA): struct ParticleSystem { std::vector<float> posX, posY, posZ; std::vector<int> id; };
    Positions are contiguous in RAM. CPU prefetcher loads 16 consecutive float coordinates per 64-byte cache line, enabling SIMD vectorization and 3.8x faster execution!
simd_vector_ace
SIMD & Intrinsics
MEMBER
Temsilci: 151
Katılım Tarihi: May 2020
Gönderiler: 8
Teşekkürler: 36
1 ay önce · Jul 24, 2026 12:54 PM
#2

SoA layout is the foundational principle behind Data-Oriented Design (DOD) and Entity Component Systems (ECS).

assembly_micro_dev
x86_64 Micro-arch
MEMBER
Temsilci: 186
Katılım Tarihi: Oct 2021
Gönderiler: 10
Teşekkürler: 27
1 ay önce · Jul 25, 2026 3:21 AM
#3

Cache misses cost 100-300 CPU cycles each. Keeping hot data packed tightly in memory is how you write real-time software.