Home / Forums / Designing a Cache-Friendly Structure of Arrays (SoA) Particle Engine in C++ [Part 3]

UnreliableCode Community

Developer Research, Reverse Engineering & Coding Community

Tutorial

Designing a Cache-Friendly Structure of Arrays (SoA) Particle Engine in C++ [Part 3]

PointerGuru
Systems & Memory Engineer
MEMBER
Rep: 209
Join Date: Feb 2021
Posts: 6
Thanks: 107
3y ago · Apr 19, 2023 8:57 PM
#1
Array of Structures (AoS: struct Particle { vec3 pos; vec3 vel; float life; color col; }; std::vector<Particle>) causes cache waste when a physics system only needs pos and vel.
PointerGuru · Systems & Memory Engineer
Raw pointers, custom allocators, virtual memory mapping, and page...
The following users thanked PointerGuru for this post:
CacheOptimizer
Data-Oriented Architect
VIP
Rep: 111
Join Date: Jul 2020
Posts: 6
Thanks: 34
3y ago · Apr 19, 2023 10:38 PM
#2
Structure of Arrays (SoA: struct Particles { vector<float> posX, posY, posZ; vector<float> velX, velY, velZ; };) allows SIMD vector units to load 8 contiguous positions into AVX registers without striding.
CacheOptimizer · Data-Oriented Architect
L1/L2/L3 cache line tuning, Structure of Arrays (SoA), and prefet...
StdRangesFan
Functional C++
MEMBER
Rep: 119
Join Date: Mar 2022
Posts: 6
Thanks: 79
3y ago · Apr 20, 2023 3:38 AM
#3
Our particle simulation performance increased from 200,000 particles at 60 FPS to over 2,500,000 particles at 144 FPS.
StdRangesFan · Functional C++
C++20 view pipelines, lazy evaluation, and composable range algor...