21. Distance Metaphor
•
•
L1 cache: it's on your desk, pick it up.
•
Main memory: it's on the shelf in your garage
downstairs, might as well get a snack while you're
down there.
•
Disk: it's in, um, California. Walk there. Walk back.
Really.
L2 cache: it's on the bookshelf in your office, get
up out of the chair.
http://hacksoflife.blogspot.com/2011/04/going-to-california-with-aching-in-my.html
23. Modern Programming
• High-level languages and abstractions
• OOP
• everywhere!
• objects scattered throughout the
address space
• memory access patterns are
unpredictable
27. Example 1: AoS vs SoA
struct Tile
{
bool ready;
Data pixels; // big chunk of data
};
Tile tiles[SIZE];
vs
struct Image
{
bool ready[SIZE];
// hot data
Data pixels[SIZE]; // cold data
};
28. Example 1: AoS vs SoA
for (int i = 0; i < SIZE; ++i)
{
if (tiles[i].ready)
draw(tiles[i].pixels);
}
!
vs
for (int i = 0; i < SIZE; ++i)
{
if (image.ready[i])
draw(image.pixels[i]);
}
32. Example 2: Existence
struct Image
{
bool ready[SIZE];
Data pixels[SIZE];
};
Image image;
vs
Data ready_pixels[N];
// N ≤ SIZE
!
33. Example 2: Existence
for (int i = 0; i < SIZE; ++i)
{
if (image.ready[i])
draw(image.pixels[i]);
}
!
vs
for (int i = 0; i < N; ++i)
{
draw(ready_pixels[i];
}
34. Example 3: Locality
!
array<float> numbers;
float sum = 0.0f;
for (auto it : numbers)
sum += *it;
!
vs
list<float> numbers;
float sum = 0.0f;
for (auto it : numbers)
sum+ = *it;
36. Advice
• Keep your data closer to registers and
cache (hot data)
• Don’t touch what you don’t have to (cold
data)
• Predictable access patterns (e.g. linear
arrays) - good
• What’s good for memory - good for you
37. DOD Patterns
• A to B transform
• In-place transform
• Existence based processing
• Data normalization
• DB design says hello!
• Task, gather, dispatch, and more...
38. DOD Benefits
• Maximum performance
• CPU doesn’t wait & starve
• Easy to parallelize
• data is grouped, transforms separated
• ready for Parallel Processing, OOP doesn’t
• Simpler code
• surprise!
39. References: Memory
• Ulrich Drepper “What Every Computer
Programmer Should Know About Memory”
• Крис Касперски “Техника оптимизации
програм. Еффективное использование
памяти”
• Christer Ericson “Memory Optimization”
• Igor Ostrovsky “Gallery of Processor Cache
Effects”
40. References: DOD
• Noel Llopis “Data-Oriented Design”, Game Developer
Magazine, September 2009
• Richard Fabian “Data-Oriented Desing”, book draft
http://www.dataorienteddesign.com/dodmain/
• Tony Albrecht “Pitfalls of Object-Oriented Programming”
• Niklas Frykholm “Practical Examples of Data Oriented
Design”, also everything on http://bitsquid.blogspot.com/
• Mike Acton “Typical C++ Bullshit”
• Data Oriented Design @ Google+
41. Bonus: Object or not?
Q: What is a table?
A: Flat top and 4 legs.
Q: Object? (OOP)
A:Yes.
Q: If we remove one leg. Is it still an object?
A: …
DOD: There is no table :)
42. Bonus: Object or not?
Q:You are modelling a pile of sand. Is it an object?
A:Yes.
Q: What is the border line number of particles N after
which just a bunch of sand particles start forming a pile? 10?
1000? 1000000?
(i.e. can we say that N particles are just a bunch of particles,
but N+1 particles become a pile of sand?)
A: …
DOD: Sand particles are data.