The document provides an introduction to data-oriented design (DOD). It discusses how modern computer architectures have changed with memory latency now being the dominant factor over CPU speed. This has made the traditional object-oriented programming model focused on code inefficient. DOD is presented as an alternative paradigm that focuses on data transformations rather than objects. Key principles of DOD include keeping data close to CPU caches, processing data together in blocks, and separating data from code. Examples are given showing how DOD can improve performance and locality of reference. Benefits of DOD include maximum performance, ease of parallelization, and simpler code.
43. Our Programming
Model
• High-level languages
• OOP
• everywhere!
• objects scattered throughout the
address space
• access patterns are unpredictable
61. Distance Metaphor
•
•
L1 cache: it's on your desk, pick it up.
L2 cache: it's on the bookshelf in your office, get
up out of the chair.
62. 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.
L2 cache: it's on the bookshelf in your office, get
up out of the chair.
63. 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.
64. 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
67. Advice
• Keep your data closer to registers and
cache
• What’s good for memory - good for you
68. 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
};
69. 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]);
}
71. Example 2: Existence
struct Image
{
bool ready[SIZE];
Data pixels[SIZE];
};
Image image;
vs
Data ready_pixels[N];
Data no_pixels[M];
// N + M = SIZE
72. 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];
}
73. Example 3: Locality
std::vector<float> numbers;
float sum = 0.0f;
for (auto it : numbers)
sum += *it;
vs
std::list<float> numbers;
float sum = 0.0f;
for (auto it : numbers)
sum+ = *it;
75. Few Patterns
• A to B transform
• In place transform
• Existence based processing
• Data normalization
• DB design says hello!
• Task, gather, dispatch, and more...
78. Benefits of DOD
•
•
Maximum performance
•
CPU doesn’t wait & starve
Easy to parallelize
•
•
data is grouped, transforms separated
ready for Parallel Processing, OOP doesn’t
79. Benefits of DOD
•
•
•
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!
80. References: Memory
• Ulrich Drepper “What Every Computer
Programmer Should Know About Memory”
• Крис Касперски “Техника оптимизации
програм. Еффективное использование
памяти”
• Christer Ericson “Memory Optimization”
• Igor Ostrovsky “Gallery of Processor Cache
Effects”
81. 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”
•
•
Mike Acton “Typical C++ Bullshit”
Niklas Frykholm “Practical Examples of Data Oriented
Design”
Data Oriented Design @ Google+