SlideShare a Scribd company logo
Vu Phuong Hoang
ECSPart 1: Introduction to
Data-Oriented Design
2018
▪ found it too hard to reduce lags ?
▪ tried to improve core functions ?
▪ be defeated by a heavy loop ?
Check this out for some experiments
Have you ever ...
A “simple” loop
Frame Rate
TEST #1
TestCacheMissByOrder.cs
Find a minimum value in a table 1000x1000
Test #1 - Result
for (int r = 0; r < ROWS_COUNT; ++r) {
for (int c = 0; c < COLUMNS_COUNT; ++c) {
minValue = Math.Min(minValue, table[r][c]);
}
}
Iterate by each row, then by each column
4ms
for (int c = 0; c < COLUMNS_COUNT; ++c) {
for (int r = 0; r < ROWS_COUNT; ++r) {
minValue = Math.Min(minValue, table[r][c]);
}
}
Just swap the loops order
8ms ???
Test #1 - Result
for (int r = 0; r < ROWS_COUNT; ++r) {
for (int c = 0; c < COLUMNS_COUNT; ++c) {
minValue = Math.Min(minValue, table[r][c]);
}
}
Iterate by each row, then by each column
4ms
for (int c = 0; c < COLUMNS_COUNT; ++c) {
for (int r = 0; r < ROWS_COUNT; ++r) {
minValue = Math.Min(minValue, table[r][c]);
}
}
Just swap the loops order
8ms ???
CPU Cache
▪ Loading from Cache is faster than RAM
▪ Both data & instructions will be loaded
▪ References:
▪ Dogged Determination
▪ Fuzzy Reflection
▪ codeburst.io
CPU Cache
PS4 data loading latency
▪ When a value is read from memory, next values will be read too

à Data is loaded in batch (size = cache line)
▪ A cache line = 64 bytes
▪ Data already in Cache à Cache-hit
▪ Data not in Cache à Cache-miss à Need to load from slower memory
CPU Cache
H: Cache-Hit, M: Cache-Miss
Test #1 - Result explain
for (int r = 0; r < ROWS_COUNT; ++r) {
for (int c = 0; c < COLUMNS_COUNT; ++c) {
minValue = Math.Min(minValue, table[r][c]);
}
}
r1
r2
r3
for (int c = 0; c < COLUMNS_COUNT; ++c) {
for (int r = 0; r < ROWS_COUNT; ++r) {
minValue = Math.Min(minValue, table[r][c]);
}
}
c1 c2 c3
M H H H ...
M H H H ...
M
M M M
M M
M M
... ...
Test #1 - Take it further
int[][] table = new int[ ROWS_COUNT ][ ];
// table[i] = new int[ COLUMNS_COUNT ];
Iterate 2D array
4ms ???
int CELLS_COUNT = ROWS_COUNT * COLUMNS_COUNT
int[] flatTable = new int[ CELLS_COUNT ];
Iterate 1D array
2ms
Fragmentation
▪ Contiguous data is faster to load
▪ CPU allocates memory block where it fits
▪ Memory fragmentation is like a Swiss cheese
▪ Lead to cache-misses
Swiss-Cheese Memory
TEST #2
TestCacheMissByDataSize.cs
Read values in Arrays of different data types (10M elements)
Test #2 - Result
Iterate an array of int (4 bytes)
35ms
Iterate an array of struct (32 bytes)
58ms
Why ?
Bigger struct (36 bytes) is even worse
60ms
Test #2 - Result explain
Iterate an array of int (4 bytes)
35ms
Iterate an array of struct (32 bytes)
58ms
Why ? Answer: CPU Cache, again
Test #2 - Result explain
Cache Pollution
Un-used	data	
still	loaded
Less	space	
in	cache-line
More	
cache-misses
Test #2 - Result explain
Un-used	data	
still	loaded
Less	space	
in	cache-line
More	
cache-misses
GameObject in
OOP style ?
Test #2 - Take it further
Add 1 byte data to the struct.
Then its size comes from 32 to 36 bytes (expect 33).
Why ?
Add 1 byte data to the struct.
Then its size comes from 32 to 36 bytes (expect 33).
Why ?
Answer: Data alignment
More:
▪ Try appending 1 more byte, size keeps at 36.
▪ Try prepending 1 more byte, size goes to 40.
Test #2 - Take it further
▪ Data is put into 4-bytes “buckets”

for fast access
▪ When added data doesn’t fit
▪ Next (& empty) bucket will be used
▪ Wasted un-used bytes = padding
▪ References:
▪ Stdio.vn
▪ Wikipedia
▪ Song Ho Ahn
Data alignment
Without
data alignment
TEST #3
TestDataAlignment.cs
Change order of data in struct
Just re-order data from biggest to smallest size
8 bytes
Test #3 - Result
12 bytes
???
Cache-miss
▪ Fastest way to load data: NOT LOADING IT :)
▪ Second best ways ?
▪ Keep data small (if not, notice about data alignment)
▪ Keep data contiguous
▪ Separate data by function
▪ In Relational Database, sometimes we de-normalize for performance, too !
◆ Problem #1: Encapsulation makes it hard to do this
Functions are also data
▪ Function is split into instruction blocks
▪ CPU looks up these blocks from a table
▪ CPU loads these blocks into instruction cache (I$)
▪ Function call suffers from cache-miss, too !!!
▪ References:
▪ Wikipedia (Instruction Cycle)
▪ Wikipedia (Branch Misprediction)
Function call
TEST #4
TestVirtualFunctions.cs
How overriden functions affect performance ?
Test #4 - Result
Direct call
35ms
1-level indirect call
61ms
10-levels indirect call
411ms
▪ Fastest way to call a function: NOT CALLING IT :)
▪ Second best ways:
▪ Keep high-performance function small (fits in cache)
▪ Keep narrow class hierarchy
▪ 1 function to process multiple instances, not 1 function for each instance
◆ Problem #2: Encapsulation / Polymorphism makes it hard to do this
Function call
Wait, they are OOP core !
Encapsulation + Inheritance + Polymorphism
▪ Multiple inheritance
▪ Useful for game development, bad architecture
▪ “Diamond of dead”
◆ Problem #3: Not an easy way to implement multiple inheritance properly
Other OOP problems
▪ Multiple inheritance
▪ Useful for game development, bad architecture
▪ “Diamond of dead”
◆ Problem #3: Not an easy way to implement multiple inheritance properly
▪ Unit test
▪ My test uses some members, but I need to initialize them all !!!
◆ Problem #4: Unit test involves un-related constraints
Other OOP problems
▪ Multiple inheritance
▪ Useful for game development, bad architecture
▪ “Diamond of dead”
◆ Problem #3: Not an easy way to implement multiple inheritance properly
▪ Unit test
▪ My test uses some members, but I need to initialize them all !!!
◆ Problem #4: Unit test involves un-related constraints
▪ Jobify, False sharing, ...
Other OOP problems
Data-Oriented Design
▪ Focus on how data is laid out in memory
▪ Focus on how data is read / processed
▪ Build functions around data
Data-Oriented Design
▪ Focus on how data is laid out in memory
▪ Focus on how data is read / processed
▪ Build functions around data
▪ References:
▪ DICE
▪ Mike Acton (Insomniac Games, Unity)
▪ Richard Fabian
▪ Keith O’Connor (Ubisoft Montreal)
Data-Oriented Design
“The purpose of all programs, and all
parts of those programs, is to transform
data from one form to another ”
- Mike Acton -
“When there is one, there are many ”
- Mike Acton -
“Designing the code around the data,
not the other way around ”
- Linus Torvalds -
TEST #5
TestGoodEnoughAlgorithms.cs
Find closest object
Test #5 - Result
for (int i = 0; i < ELEMENTS_COUNT; ++i) {
d = GetDistance(center, objects[i].position);
if (minDistance > d) {
minDistance = d;
closestId = i;
}
}
Iterate Array of “GameObjects”
209ms
for (int i = 0; i < ELEMENTS_COUNT; ++i) {
d = GetDistance(center, positions[i]);
if (minDistance > d) {
minDistance = d;
closestId = i;
}
}
Iterate Array of positions
128ms
They’re almost identical, except line #2
Test #5 - Take it further
▪ You already knew DOD is faster (from previous test results)
▪ Let’s improve the algorithm (current: 209ms)
▪ Use GetSquareDistance instead of GetDistance à 137ms
▪ *Eliminate too far objects & pick the 1st close-enough object à 36ms
▪ Reduce branch mis-prediction à 34ms
*Human needs good-enough choice, not the optimal one.
Test #5 - Take it further
for (int i = 0; i < ELEMENTS_COUNT; ++i) {
d = GetSqDistance(center, objects[i].position);
if (d > MAX_SQ_DST) continue;
if (d < MIN_SQ_DST) { closestId = i; break; }
// ... original comparison here
}
Iterate Array of “GameObjects”
36ms
for (int i = 0; i < ELEMENTS_COUNT; ++i) {
d = GetSqDistance(center, positions[i]);
if (d > MAX_SQ_DST) continue;
if (d < MIN_SQ_DST) { closestId = i; break; }
// ... original comparison here
}
Iterate Array of positions
25ms
Your smart algorithm + DOD = AWESOME
▪ Reduce data cache-misses (Problem #1)
▪ Reduce function cache-misses, indirect function calls (Problem #2)
▪ Component over inheritance (Problem #3)
▪ Unit test = Feed input & Assert the output (Problem #4)
▪ References:
▪ Games From Within
▪ Tencent
Data-Oriented Design
ECSEntity
Component
System
Smart DOD
Architecture ?
▪ Performance & flexibility
▪ It’s the FUTURE (click links to see more)
▪ Mentioned top companies (Insomniac Games, Ubisoft, EA/DICE, ...)
▪ Sony
▪ Intel
▪ Apple
▪ Riot Games
▪ Unity !!! (other, other, other, other)
▪ More ...
Why should we care ?
These masterpieces
also use ECS
* Click images for more details
Q&A
End of Part 1
Ad

More Related Content

What's hot (20)

NDC 2011 영웅전 런칭팀 박영준
NDC 2011 영웅전 런칭팀 박영준NDC 2011 영웅전 런칭팀 박영준
NDC 2011 영웅전 런칭팀 박영준
영준 박
 
Component-Based Entity Systems (Demo)
Component-Based Entity Systems (Demo)Component-Based Entity Systems (Demo)
Component-Based Entity Systems (Demo)
Nick Pruehs
 
Parallel Futures of a Game Engine (v2.0)
Parallel Futures of a Game Engine (v2.0)Parallel Futures of a Game Engine (v2.0)
Parallel Futures of a Game Engine (v2.0)
repii
 
Game Programming 07 - Procedural Content Generation
Game Programming 07 - Procedural Content GenerationGame Programming 07 - Procedural Content Generation
Game Programming 07 - Procedural Content Generation
Nick Pruehs
 
Umg ,이벤트 바인딩, Invaidation Box
Umg ,이벤트 바인딩, Invaidation BoxUmg ,이벤트 바인딩, Invaidation Box
Umg ,이벤트 바인딩, Invaidation Box
대영 노
 
Making an independend MMO - The Albion Online Story
Making an independend MMO - The Albion Online StoryMaking an independend MMO - The Albion Online Story
Making an independend MMO - The Albion Online Story
David Salz
 
State-Based Scripting in Uncharted 2: Among Thieves
State-Based Scripting in Uncharted 2: Among ThievesState-Based Scripting in Uncharted 2: Among Thieves
State-Based Scripting in Uncharted 2: Among Thieves
Naughty Dog
 
임태현, MMO 서버 개발 포스트 모템, NDC2012
임태현, MMO 서버 개발 포스트 모템, NDC2012임태현, MMO 서버 개발 포스트 모템, NDC2012
임태현, MMO 서버 개발 포스트 모템, NDC2012
devCAT Studio, NEXON
 
나만의 엔진 개발하기
나만의 엔진 개발하기나만의 엔진 개발하기
나만의 엔진 개발하기
YEONG-CHEON YOU
 
게임제작개론 : #7 팀 역할과 게임 리소스에 대한 이해
게임제작개론 : #7 팀 역할과 게임 리소스에 대한 이해게임제작개론 : #7 팀 역할과 게임 리소스에 대한 이해
게임제작개론 : #7 팀 역할과 게임 리소스에 대한 이해
Seungmo Koo
 
06_게임엔진구성
06_게임엔진구성06_게임엔진구성
06_게임엔진구성
noerror
 
Unity - Internals: memory and performance
Unity - Internals: memory and performanceUnity - Internals: memory and performance
Unity - Internals: memory and performance
Codemotion
 
Optimizing unity games (Google IO 2014)
Optimizing unity games (Google IO 2014)Optimizing unity games (Google IO 2014)
Optimizing unity games (Google IO 2014)
Alexander Dolbilov
 
Optimization in Unity: simple tips for developing with "no surprises" / Anton...
Optimization in Unity: simple tips for developing with "no surprises" / Anton...Optimization in Unity: simple tips for developing with "no surprises" / Anton...
Optimization in Unity: simple tips for developing with "no surprises" / Anton...
DevGAMM Conference
 
Optimizing the Graphics Pipeline with Compute, GDC 2016
Optimizing the Graphics Pipeline with Compute, GDC 2016Optimizing the Graphics Pipeline with Compute, GDC 2016
Optimizing the Graphics Pipeline with Compute, GDC 2016
Graham Wihlidal
 
Future Directions for Compute-for-Graphics
Future Directions for Compute-for-GraphicsFuture Directions for Compute-for-Graphics
Future Directions for Compute-for-Graphics
Electronic Arts / DICE
 
혼자서 만드는 MMO게임 서버
혼자서 만드는 MMO게임 서버혼자서 만드는 MMO게임 서버
혼자서 만드는 MMO게임 서버
iFunFactory Inc.
 
Built for performance: the UIElements Renderer – Unite Copenhagen 2019
Built for performance: the UIElements Renderer – Unite Copenhagen 2019Built for performance: the UIElements Renderer – Unite Copenhagen 2019
Built for performance: the UIElements Renderer – Unite Copenhagen 2019
Unity Technologies
 
Practical Guide for Optimizing Unity on Mobiles
Practical Guide for Optimizing Unity on MobilesPractical Guide for Optimizing Unity on Mobiles
Practical Guide for Optimizing Unity on Mobiles
Valentin Simonov
 
김혁, <드래곤 하운드>의 PBR과 레이트레이싱 렌더링 기법, NDC2019
김혁, <드래곤 하운드>의 PBR과 레이트레이싱 렌더링 기법, NDC2019김혁, <드래곤 하운드>의 PBR과 레이트레이싱 렌더링 기법, NDC2019
김혁, <드래곤 하운드>의 PBR과 레이트레이싱 렌더링 기법, NDC2019
devCAT Studio, NEXON
 
NDC 2011 영웅전 런칭팀 박영준
NDC 2011 영웅전 런칭팀 박영준NDC 2011 영웅전 런칭팀 박영준
NDC 2011 영웅전 런칭팀 박영준
영준 박
 
Component-Based Entity Systems (Demo)
Component-Based Entity Systems (Demo)Component-Based Entity Systems (Demo)
Component-Based Entity Systems (Demo)
Nick Pruehs
 
Parallel Futures of a Game Engine (v2.0)
Parallel Futures of a Game Engine (v2.0)Parallel Futures of a Game Engine (v2.0)
Parallel Futures of a Game Engine (v2.0)
repii
 
Game Programming 07 - Procedural Content Generation
Game Programming 07 - Procedural Content GenerationGame Programming 07 - Procedural Content Generation
Game Programming 07 - Procedural Content Generation
Nick Pruehs
 
Umg ,이벤트 바인딩, Invaidation Box
Umg ,이벤트 바인딩, Invaidation BoxUmg ,이벤트 바인딩, Invaidation Box
Umg ,이벤트 바인딩, Invaidation Box
대영 노
 
Making an independend MMO - The Albion Online Story
Making an independend MMO - The Albion Online StoryMaking an independend MMO - The Albion Online Story
Making an independend MMO - The Albion Online Story
David Salz
 
State-Based Scripting in Uncharted 2: Among Thieves
State-Based Scripting in Uncharted 2: Among ThievesState-Based Scripting in Uncharted 2: Among Thieves
State-Based Scripting in Uncharted 2: Among Thieves
Naughty Dog
 
임태현, MMO 서버 개발 포스트 모템, NDC2012
임태현, MMO 서버 개발 포스트 모템, NDC2012임태현, MMO 서버 개발 포스트 모템, NDC2012
임태현, MMO 서버 개발 포스트 모템, NDC2012
devCAT Studio, NEXON
 
나만의 엔진 개발하기
나만의 엔진 개발하기나만의 엔진 개발하기
나만의 엔진 개발하기
YEONG-CHEON YOU
 
게임제작개론 : #7 팀 역할과 게임 리소스에 대한 이해
게임제작개론 : #7 팀 역할과 게임 리소스에 대한 이해게임제작개론 : #7 팀 역할과 게임 리소스에 대한 이해
게임제작개론 : #7 팀 역할과 게임 리소스에 대한 이해
Seungmo Koo
 
06_게임엔진구성
06_게임엔진구성06_게임엔진구성
06_게임엔진구성
noerror
 
Unity - Internals: memory and performance
Unity - Internals: memory and performanceUnity - Internals: memory and performance
Unity - Internals: memory and performance
Codemotion
 
Optimizing unity games (Google IO 2014)
Optimizing unity games (Google IO 2014)Optimizing unity games (Google IO 2014)
Optimizing unity games (Google IO 2014)
Alexander Dolbilov
 
Optimization in Unity: simple tips for developing with "no surprises" / Anton...
Optimization in Unity: simple tips for developing with "no surprises" / Anton...Optimization in Unity: simple tips for developing with "no surprises" / Anton...
Optimization in Unity: simple tips for developing with "no surprises" / Anton...
DevGAMM Conference
 
Optimizing the Graphics Pipeline with Compute, GDC 2016
Optimizing the Graphics Pipeline with Compute, GDC 2016Optimizing the Graphics Pipeline with Compute, GDC 2016
Optimizing the Graphics Pipeline with Compute, GDC 2016
Graham Wihlidal
 
Future Directions for Compute-for-Graphics
Future Directions for Compute-for-GraphicsFuture Directions for Compute-for-Graphics
Future Directions for Compute-for-Graphics
Electronic Arts / DICE
 
혼자서 만드는 MMO게임 서버
혼자서 만드는 MMO게임 서버혼자서 만드는 MMO게임 서버
혼자서 만드는 MMO게임 서버
iFunFactory Inc.
 
Built for performance: the UIElements Renderer – Unite Copenhagen 2019
Built for performance: the UIElements Renderer – Unite Copenhagen 2019Built for performance: the UIElements Renderer – Unite Copenhagen 2019
Built for performance: the UIElements Renderer – Unite Copenhagen 2019
Unity Technologies
 
Practical Guide for Optimizing Unity on Mobiles
Practical Guide for Optimizing Unity on MobilesPractical Guide for Optimizing Unity on Mobiles
Practical Guide for Optimizing Unity on Mobiles
Valentin Simonov
 
김혁, <드래곤 하운드>의 PBR과 레이트레이싱 렌더링 기법, NDC2019
김혁, <드래곤 하운드>의 PBR과 레이트레이싱 렌더링 기법, NDC2019김혁, <드래곤 하운드>의 PBR과 레이트레이싱 렌더링 기법, NDC2019
김혁, <드래곤 하운드>의 PBR과 레이트레이싱 렌더링 기법, NDC2019
devCAT Studio, NEXON
 

Similar to ECS (Part 1/3) - Introduction to Data-Oriented Design (20)

Cache & CPU performance
Cache & CPU performanceCache & CPU performance
Cache & CPU performance
so61pi
 
introduction to data structures and types
introduction to data structures and typesintroduction to data structures and types
introduction to data structures and types
ankita946617
 
"Optimization of a .NET application- is it simple ! / ?", Yevhen Tatarynov
"Optimization of a .NET application- is it simple ! / ?",  Yevhen Tatarynov"Optimization of a .NET application- is it simple ! / ?",  Yevhen Tatarynov
"Optimization of a .NET application- is it simple ! / ?", Yevhen Tatarynov
Fwdays
 
Rainer Grimm, “Functional Programming in C++11”
Rainer Grimm, “Functional Programming in C++11”Rainer Grimm, “Functional Programming in C++11”
Rainer Grimm, “Functional Programming in C++11”
Platonov Sergey
 
Lecture 1 and 2 of Data Structures & Algorithms
Lecture 1 and 2 of Data Structures & AlgorithmsLecture 1 and 2 of Data Structures & Algorithms
Lecture 1 and 2 of Data Structures & Algorithms
haseebanjum2611
 
Advance data structure & algorithm
Advance data structure & algorithmAdvance data structure & algorithm
Advance data structure & algorithm
K Hari Shankar
 
Basic terminologies & asymptotic notations
Basic terminologies & asymptotic notationsBasic terminologies & asymptotic notations
Basic terminologies & asymptotic notations
Rajendran
 
Rocky Nevin's presentation at eComm 2008
Rocky Nevin's presentation at eComm 2008Rocky Nevin's presentation at eComm 2008
Rocky Nevin's presentation at eComm 2008
eComm2008
 
Tiling matrix-matrix multiply, code tuning
Tiling matrix-matrix multiply, code tuningTiling matrix-matrix multiply, code tuning
Tiling matrix-matrix multiply, code tuning
mukhi265
 
Intro.ppt
Intro.pptIntro.ppt
Intro.ppt
SharabiNaif
 
Intro.ppt
Intro.pptIntro.ppt
Intro.ppt
Anonymous9etQKwW
 
Intro_2.ppt
Intro_2.pptIntro_2.ppt
Intro_2.ppt
MumitAhmed1
 
Quick Wins
Quick WinsQuick Wins
Quick Wins
HighLoad2009
 
Java OOP Programming language (Part 8) - Java Database JDBC
Java OOP Programming language (Part 8) - Java Database JDBCJava OOP Programming language (Part 8) - Java Database JDBC
Java OOP Programming language (Part 8) - Java Database JDBC
OUM SAOKOSAL
 
Algorithm Design and Analysis
Algorithm Design and AnalysisAlgorithm Design and Analysis
Algorithm Design and Analysis
Sayed Chhattan Shah
 
How Database Convergence Impacts the Coming Decades of Data Management
How Database Convergence Impacts the Coming Decades of Data ManagementHow Database Convergence Impacts the Coming Decades of Data Management
How Database Convergence Impacts the Coming Decades of Data Management
SingleStore
 
4java Basic Syntax
4java Basic Syntax4java Basic Syntax
4java Basic Syntax
Adil Jafri
 
Data Structures and Algorithms (DSA) in C
Data Structures and Algorithms (DSA) in CData Structures and Algorithms (DSA) in C
Data Structures and Algorithms (DSA) in C
Nabajyoti Banik
 
Data Structure - Lecture 2 - Recursion Stack Queue.pdf
Data Structure - Lecture 2 - Recursion Stack Queue.pdfData Structure - Lecture 2 - Recursion Stack Queue.pdf
Data Structure - Lecture 2 - Recursion Stack Queue.pdf
donotreply20
 
Hub 102 - Lesson 5 - Algorithm: Sorting & Searching
Hub 102 - Lesson 5 - Algorithm: Sorting & SearchingHub 102 - Lesson 5 - Algorithm: Sorting & Searching
Hub 102 - Lesson 5 - Algorithm: Sorting & Searching
Tiểu Hổ
 
Cache & CPU performance
Cache & CPU performanceCache & CPU performance
Cache & CPU performance
so61pi
 
introduction to data structures and types
introduction to data structures and typesintroduction to data structures and types
introduction to data structures and types
ankita946617
 
"Optimization of a .NET application- is it simple ! / ?", Yevhen Tatarynov
"Optimization of a .NET application- is it simple ! / ?",  Yevhen Tatarynov"Optimization of a .NET application- is it simple ! / ?",  Yevhen Tatarynov
"Optimization of a .NET application- is it simple ! / ?", Yevhen Tatarynov
Fwdays
 
Rainer Grimm, “Functional Programming in C++11”
Rainer Grimm, “Functional Programming in C++11”Rainer Grimm, “Functional Programming in C++11”
Rainer Grimm, “Functional Programming in C++11”
Platonov Sergey
 
Lecture 1 and 2 of Data Structures & Algorithms
Lecture 1 and 2 of Data Structures & AlgorithmsLecture 1 and 2 of Data Structures & Algorithms
Lecture 1 and 2 of Data Structures & Algorithms
haseebanjum2611
 
Advance data structure & algorithm
Advance data structure & algorithmAdvance data structure & algorithm
Advance data structure & algorithm
K Hari Shankar
 
Basic terminologies & asymptotic notations
Basic terminologies & asymptotic notationsBasic terminologies & asymptotic notations
Basic terminologies & asymptotic notations
Rajendran
 
Rocky Nevin's presentation at eComm 2008
Rocky Nevin's presentation at eComm 2008Rocky Nevin's presentation at eComm 2008
Rocky Nevin's presentation at eComm 2008
eComm2008
 
Tiling matrix-matrix multiply, code tuning
Tiling matrix-matrix multiply, code tuningTiling matrix-matrix multiply, code tuning
Tiling matrix-matrix multiply, code tuning
mukhi265
 
Java OOP Programming language (Part 8) - Java Database JDBC
Java OOP Programming language (Part 8) - Java Database JDBCJava OOP Programming language (Part 8) - Java Database JDBC
Java OOP Programming language (Part 8) - Java Database JDBC
OUM SAOKOSAL
 
How Database Convergence Impacts the Coming Decades of Data Management
How Database Convergence Impacts the Coming Decades of Data ManagementHow Database Convergence Impacts the Coming Decades of Data Management
How Database Convergence Impacts the Coming Decades of Data Management
SingleStore
 
4java Basic Syntax
4java Basic Syntax4java Basic Syntax
4java Basic Syntax
Adil Jafri
 
Data Structures and Algorithms (DSA) in C
Data Structures and Algorithms (DSA) in CData Structures and Algorithms (DSA) in C
Data Structures and Algorithms (DSA) in C
Nabajyoti Banik
 
Data Structure - Lecture 2 - Recursion Stack Queue.pdf
Data Structure - Lecture 2 - Recursion Stack Queue.pdfData Structure - Lecture 2 - Recursion Stack Queue.pdf
Data Structure - Lecture 2 - Recursion Stack Queue.pdf
donotreply20
 
Hub 102 - Lesson 5 - Algorithm: Sorting & Searching
Hub 102 - Lesson 5 - Algorithm: Sorting & SearchingHub 102 - Lesson 5 - Algorithm: Sorting & Searching
Hub 102 - Lesson 5 - Algorithm: Sorting & Searching
Tiểu Hổ
 
Ad

More from Phuong Hoang Vu (11)

Abalanche - Unity Shader Graph #1: Shader & PBR Materials
Abalanche - Unity Shader Graph #1: Shader & PBR MaterialsAbalanche - Unity Shader Graph #1: Shader & PBR Materials
Abalanche - Unity Shader Graph #1: Shader & PBR Materials
Phuong Hoang Vu
 
Introduce phaser
Introduce phaserIntroduce phaser
Introduce phaser
Phuong Hoang Vu
 
Unity Visual Effect Graph
Unity Visual Effect GraphUnity Visual Effect Graph
Unity Visual Effect Graph
Phuong Hoang Vu
 
[UX Series] 6 - Animation principles
[UX Series] 6 - Animation principles[UX Series] 6 - Animation principles
[UX Series] 6 - Animation principles
Phuong Hoang Vu
 
[UX Series] 5 - Navigation
[UX Series] 5 - Navigation[UX Series] 5 - Navigation
[UX Series] 5 - Navigation
Phuong Hoang Vu
 
[UX Series] 4 - Contrast in design
[UX Series] 4 - Contrast in design[UX Series] 4 - Contrast in design
[UX Series] 4 - Contrast in design
Phuong Hoang Vu
 
[UX Series] 2 - Clean design. Less is more
[UX Series] 2 - Clean design. Less is more[UX Series] 2 - Clean design. Less is more
[UX Series] 2 - Clean design. Less is more
Phuong Hoang Vu
 
[UX Series] 3 - User behavior patterns and design principles
[UX Series] 3 - User behavior patterns and design principles[UX Series] 3 - User behavior patterns and design principles
[UX Series] 3 - User behavior patterns and design principles
Phuong Hoang Vu
 
[UX Series] 1b - 12 standard screen layouts
[UX Series] 1b - 12 standard screen layouts[UX Series] 1b - 12 standard screen layouts
[UX Series] 1b - 12 standard screen layouts
Phuong Hoang Vu
 
[UX Series] 1 - UX Introduction
[UX Series] 1 - UX Introduction[UX Series] 1 - UX Introduction
[UX Series] 1 - UX Introduction
Phuong Hoang Vu
 
Cross platform mobile approaches
Cross platform mobile approachesCross platform mobile approaches
Cross platform mobile approaches
Phuong Hoang Vu
 
Abalanche - Unity Shader Graph #1: Shader & PBR Materials
Abalanche - Unity Shader Graph #1: Shader & PBR MaterialsAbalanche - Unity Shader Graph #1: Shader & PBR Materials
Abalanche - Unity Shader Graph #1: Shader & PBR Materials
Phuong Hoang Vu
 
Unity Visual Effect Graph
Unity Visual Effect GraphUnity Visual Effect Graph
Unity Visual Effect Graph
Phuong Hoang Vu
 
[UX Series] 6 - Animation principles
[UX Series] 6 - Animation principles[UX Series] 6 - Animation principles
[UX Series] 6 - Animation principles
Phuong Hoang Vu
 
[UX Series] 5 - Navigation
[UX Series] 5 - Navigation[UX Series] 5 - Navigation
[UX Series] 5 - Navigation
Phuong Hoang Vu
 
[UX Series] 4 - Contrast in design
[UX Series] 4 - Contrast in design[UX Series] 4 - Contrast in design
[UX Series] 4 - Contrast in design
Phuong Hoang Vu
 
[UX Series] 2 - Clean design. Less is more
[UX Series] 2 - Clean design. Less is more[UX Series] 2 - Clean design. Less is more
[UX Series] 2 - Clean design. Less is more
Phuong Hoang Vu
 
[UX Series] 3 - User behavior patterns and design principles
[UX Series] 3 - User behavior patterns and design principles[UX Series] 3 - User behavior patterns and design principles
[UX Series] 3 - User behavior patterns and design principles
Phuong Hoang Vu
 
[UX Series] 1b - 12 standard screen layouts
[UX Series] 1b - 12 standard screen layouts[UX Series] 1b - 12 standard screen layouts
[UX Series] 1b - 12 standard screen layouts
Phuong Hoang Vu
 
[UX Series] 1 - UX Introduction
[UX Series] 1 - UX Introduction[UX Series] 1 - UX Introduction
[UX Series] 1 - UX Introduction
Phuong Hoang Vu
 
Cross platform mobile approaches
Cross platform mobile approachesCross platform mobile approaches
Cross platform mobile approaches
Phuong Hoang Vu
 
Ad

Recently uploaded (20)

fennec fox optimization algorithm for optimal solution
fennec fox optimization algorithm for optimal solutionfennec fox optimization algorithm for optimal solution
fennec fox optimization algorithm for optimal solution
shallal2
 
Everything You Need to Know About Agentforce? (Put AI Agents to Work)
Everything You Need to Know About Agentforce? (Put AI Agents to Work)Everything You Need to Know About Agentforce? (Put AI Agents to Work)
Everything You Need to Know About Agentforce? (Put AI Agents to Work)
Cyntexa
 
Mastering Testing in the Modern F&B Landscape
Mastering Testing in the Modern F&B LandscapeMastering Testing in the Modern F&B Landscape
Mastering Testing in the Modern F&B Landscape
marketing943205
 
Viam product demo_ Deploying and scaling AI with hardware.pdf
Viam product demo_ Deploying and scaling AI with hardware.pdfViam product demo_ Deploying and scaling AI with hardware.pdf
Viam product demo_ Deploying and scaling AI with hardware.pdf
camilalamoratta
 
Canadian book publishing: Insights from the latest salary survey - Tech Forum...
Canadian book publishing: Insights from the latest salary survey - Tech Forum...Canadian book publishing: Insights from the latest salary survey - Tech Forum...
Canadian book publishing: Insights from the latest salary survey - Tech Forum...
BookNet Canada
 
Config 2025 presentation recap covering both days
Config 2025 presentation recap covering both daysConfig 2025 presentation recap covering both days
Config 2025 presentation recap covering both days
TrishAntoni1
 
RTP Over QUIC: An Interesting Opportunity Or Wasted Time?
RTP Over QUIC: An Interesting Opportunity Or Wasted Time?RTP Over QUIC: An Interesting Opportunity Or Wasted Time?
RTP Over QUIC: An Interesting Opportunity Or Wasted Time?
Lorenzo Miniero
 
GyrusAI - Broadcasting & Streaming Applications Driven by AI and ML
GyrusAI - Broadcasting & Streaming Applications Driven by AI and MLGyrusAI - Broadcasting & Streaming Applications Driven by AI and ML
GyrusAI - Broadcasting & Streaming Applications Driven by AI and ML
Gyrus AI
 
Agentic Automation - Delhi UiPath Community Meetup
Agentic Automation - Delhi UiPath Community MeetupAgentic Automation - Delhi UiPath Community Meetup
Agentic Automation - Delhi UiPath Community Meetup
Manoj Batra (1600 + Connections)
 
Hybridize Functions: A Tool for Automatically Refactoring Imperative Deep Lea...
Hybridize Functions: A Tool for Automatically Refactoring Imperative Deep Lea...Hybridize Functions: A Tool for Automatically Refactoring Imperative Deep Lea...
Hybridize Functions: A Tool for Automatically Refactoring Imperative Deep Lea...
Raffi Khatchadourian
 
Cybersecurity Threat Vectors and Mitigation
Cybersecurity Threat Vectors and MitigationCybersecurity Threat Vectors and Mitigation
Cybersecurity Threat Vectors and Mitigation
VICTOR MAESTRE RAMIREZ
 
How analogue intelligence complements AI
How analogue intelligence complements AIHow analogue intelligence complements AI
How analogue intelligence complements AI
Paul Rowe
 
The No-Code Way to Build a Marketing Team with One AI Agent (Download the n8n...
The No-Code Way to Build a Marketing Team with One AI Agent (Download the n8n...The No-Code Way to Build a Marketing Team with One AI Agent (Download the n8n...
The No-Code Way to Build a Marketing Team with One AI Agent (Download the n8n...
SOFTTECHHUB
 
AI 3-in-1: Agents, RAG, and Local Models - Brent Laster
AI 3-in-1: Agents, RAG, and Local Models - Brent LasterAI 3-in-1: Agents, RAG, and Local Models - Brent Laster
AI 3-in-1: Agents, RAG, and Local Models - Brent Laster
All Things Open
 
UiPath Agentic Automation: Community Developer Opportunities
UiPath Agentic Automation: Community Developer OpportunitiesUiPath Agentic Automation: Community Developer Opportunities
UiPath Agentic Automation: Community Developer Opportunities
DianaGray10
 
GDG Cloud Southlake #42: Suresh Mathew: Autonomous Resource Optimization: How...
GDG Cloud Southlake #42: Suresh Mathew: Autonomous Resource Optimization: How...GDG Cloud Southlake #42: Suresh Mathew: Autonomous Resource Optimization: How...
GDG Cloud Southlake #42: Suresh Mathew: Autonomous Resource Optimization: How...
James Anderson
 
Hybridize Functions: A Tool for Automatically Refactoring Imperative Deep Lea...
Hybridize Functions: A Tool for Automatically Refactoring Imperative Deep Lea...Hybridize Functions: A Tool for Automatically Refactoring Imperative Deep Lea...
Hybridize Functions: A Tool for Automatically Refactoring Imperative Deep Lea...
Raffi Khatchadourian
 
AsyncAPI v3 : Streamlining Event-Driven API Design
AsyncAPI v3 : Streamlining Event-Driven API DesignAsyncAPI v3 : Streamlining Event-Driven API Design
AsyncAPI v3 : Streamlining Event-Driven API Design
leonid54
 
On-Device or Remote? On the Energy Efficiency of Fetching LLM-Generated Conte...
On-Device or Remote? On the Energy Efficiency of Fetching LLM-Generated Conte...On-Device or Remote? On the Energy Efficiency of Fetching LLM-Generated Conte...
On-Device or Remote? On the Energy Efficiency of Fetching LLM-Generated Conte...
Ivano Malavolta
 
Smart Investments Leveraging Agentic AI for Real Estate Success.pptx
Smart Investments Leveraging Agentic AI for Real Estate Success.pptxSmart Investments Leveraging Agentic AI for Real Estate Success.pptx
Smart Investments Leveraging Agentic AI for Real Estate Success.pptx
Seasia Infotech
 
fennec fox optimization algorithm for optimal solution
fennec fox optimization algorithm for optimal solutionfennec fox optimization algorithm for optimal solution
fennec fox optimization algorithm for optimal solution
shallal2
 
Everything You Need to Know About Agentforce? (Put AI Agents to Work)
Everything You Need to Know About Agentforce? (Put AI Agents to Work)Everything You Need to Know About Agentforce? (Put AI Agents to Work)
Everything You Need to Know About Agentforce? (Put AI Agents to Work)
Cyntexa
 
Mastering Testing in the Modern F&B Landscape
Mastering Testing in the Modern F&B LandscapeMastering Testing in the Modern F&B Landscape
Mastering Testing in the Modern F&B Landscape
marketing943205
 
Viam product demo_ Deploying and scaling AI with hardware.pdf
Viam product demo_ Deploying and scaling AI with hardware.pdfViam product demo_ Deploying and scaling AI with hardware.pdf
Viam product demo_ Deploying and scaling AI with hardware.pdf
camilalamoratta
 
Canadian book publishing: Insights from the latest salary survey - Tech Forum...
Canadian book publishing: Insights from the latest salary survey - Tech Forum...Canadian book publishing: Insights from the latest salary survey - Tech Forum...
Canadian book publishing: Insights from the latest salary survey - Tech Forum...
BookNet Canada
 
Config 2025 presentation recap covering both days
Config 2025 presentation recap covering both daysConfig 2025 presentation recap covering both days
Config 2025 presentation recap covering both days
TrishAntoni1
 
RTP Over QUIC: An Interesting Opportunity Or Wasted Time?
RTP Over QUIC: An Interesting Opportunity Or Wasted Time?RTP Over QUIC: An Interesting Opportunity Or Wasted Time?
RTP Over QUIC: An Interesting Opportunity Or Wasted Time?
Lorenzo Miniero
 
GyrusAI - Broadcasting & Streaming Applications Driven by AI and ML
GyrusAI - Broadcasting & Streaming Applications Driven by AI and MLGyrusAI - Broadcasting & Streaming Applications Driven by AI and ML
GyrusAI - Broadcasting & Streaming Applications Driven by AI and ML
Gyrus AI
 
Hybridize Functions: A Tool for Automatically Refactoring Imperative Deep Lea...
Hybridize Functions: A Tool for Automatically Refactoring Imperative Deep Lea...Hybridize Functions: A Tool for Automatically Refactoring Imperative Deep Lea...
Hybridize Functions: A Tool for Automatically Refactoring Imperative Deep Lea...
Raffi Khatchadourian
 
Cybersecurity Threat Vectors and Mitigation
Cybersecurity Threat Vectors and MitigationCybersecurity Threat Vectors and Mitigation
Cybersecurity Threat Vectors and Mitigation
VICTOR MAESTRE RAMIREZ
 
How analogue intelligence complements AI
How analogue intelligence complements AIHow analogue intelligence complements AI
How analogue intelligence complements AI
Paul Rowe
 
The No-Code Way to Build a Marketing Team with One AI Agent (Download the n8n...
The No-Code Way to Build a Marketing Team with One AI Agent (Download the n8n...The No-Code Way to Build a Marketing Team with One AI Agent (Download the n8n...
The No-Code Way to Build a Marketing Team with One AI Agent (Download the n8n...
SOFTTECHHUB
 
AI 3-in-1: Agents, RAG, and Local Models - Brent Laster
AI 3-in-1: Agents, RAG, and Local Models - Brent LasterAI 3-in-1: Agents, RAG, and Local Models - Brent Laster
AI 3-in-1: Agents, RAG, and Local Models - Brent Laster
All Things Open
 
UiPath Agentic Automation: Community Developer Opportunities
UiPath Agentic Automation: Community Developer OpportunitiesUiPath Agentic Automation: Community Developer Opportunities
UiPath Agentic Automation: Community Developer Opportunities
DianaGray10
 
GDG Cloud Southlake #42: Suresh Mathew: Autonomous Resource Optimization: How...
GDG Cloud Southlake #42: Suresh Mathew: Autonomous Resource Optimization: How...GDG Cloud Southlake #42: Suresh Mathew: Autonomous Resource Optimization: How...
GDG Cloud Southlake #42: Suresh Mathew: Autonomous Resource Optimization: How...
James Anderson
 
Hybridize Functions: A Tool for Automatically Refactoring Imperative Deep Lea...
Hybridize Functions: A Tool for Automatically Refactoring Imperative Deep Lea...Hybridize Functions: A Tool for Automatically Refactoring Imperative Deep Lea...
Hybridize Functions: A Tool for Automatically Refactoring Imperative Deep Lea...
Raffi Khatchadourian
 
AsyncAPI v3 : Streamlining Event-Driven API Design
AsyncAPI v3 : Streamlining Event-Driven API DesignAsyncAPI v3 : Streamlining Event-Driven API Design
AsyncAPI v3 : Streamlining Event-Driven API Design
leonid54
 
On-Device or Remote? On the Energy Efficiency of Fetching LLM-Generated Conte...
On-Device or Remote? On the Energy Efficiency of Fetching LLM-Generated Conte...On-Device or Remote? On the Energy Efficiency of Fetching LLM-Generated Conte...
On-Device or Remote? On the Energy Efficiency of Fetching LLM-Generated Conte...
Ivano Malavolta
 
Smart Investments Leveraging Agentic AI for Real Estate Success.pptx
Smart Investments Leveraging Agentic AI for Real Estate Success.pptxSmart Investments Leveraging Agentic AI for Real Estate Success.pptx
Smart Investments Leveraging Agentic AI for Real Estate Success.pptx
Seasia Infotech
 

ECS (Part 1/3) - Introduction to Data-Oriented Design

  • 1. Vu Phuong Hoang ECSPart 1: Introduction to Data-Oriented Design 2018
  • 2. ▪ found it too hard to reduce lags ? ▪ tried to improve core functions ? ▪ be defeated by a heavy loop ? Check this out for some experiments Have you ever ... A “simple” loop Frame Rate
  • 3. TEST #1 TestCacheMissByOrder.cs Find a minimum value in a table 1000x1000
  • 4. Test #1 - Result for (int r = 0; r < ROWS_COUNT; ++r) { for (int c = 0; c < COLUMNS_COUNT; ++c) { minValue = Math.Min(minValue, table[r][c]); } } Iterate by each row, then by each column 4ms for (int c = 0; c < COLUMNS_COUNT; ++c) { for (int r = 0; r < ROWS_COUNT; ++r) { minValue = Math.Min(minValue, table[r][c]); } } Just swap the loops order 8ms ???
  • 5. Test #1 - Result for (int r = 0; r < ROWS_COUNT; ++r) { for (int c = 0; c < COLUMNS_COUNT; ++c) { minValue = Math.Min(minValue, table[r][c]); } } Iterate by each row, then by each column 4ms for (int c = 0; c < COLUMNS_COUNT; ++c) { for (int r = 0; r < ROWS_COUNT; ++r) { minValue = Math.Min(minValue, table[r][c]); } } Just swap the loops order 8ms ??? CPU Cache
  • 6. ▪ Loading from Cache is faster than RAM ▪ Both data & instructions will be loaded ▪ References: ▪ Dogged Determination ▪ Fuzzy Reflection ▪ codeburst.io CPU Cache PS4 data loading latency
  • 7. ▪ When a value is read from memory, next values will be read too
 à Data is loaded in batch (size = cache line) ▪ A cache line = 64 bytes ▪ Data already in Cache à Cache-hit ▪ Data not in Cache à Cache-miss à Need to load from slower memory CPU Cache
  • 8. H: Cache-Hit, M: Cache-Miss Test #1 - Result explain for (int r = 0; r < ROWS_COUNT; ++r) { for (int c = 0; c < COLUMNS_COUNT; ++c) { minValue = Math.Min(minValue, table[r][c]); } } r1 r2 r3 for (int c = 0; c < COLUMNS_COUNT; ++c) { for (int r = 0; r < ROWS_COUNT; ++r) { minValue = Math.Min(minValue, table[r][c]); } } c1 c2 c3 M H H H ... M H H H ... M M M M M M M M ... ...
  • 9. Test #1 - Take it further int[][] table = new int[ ROWS_COUNT ][ ]; // table[i] = new int[ COLUMNS_COUNT ]; Iterate 2D array 4ms ??? int CELLS_COUNT = ROWS_COUNT * COLUMNS_COUNT int[] flatTable = new int[ CELLS_COUNT ]; Iterate 1D array 2ms Fragmentation
  • 10. ▪ Contiguous data is faster to load ▪ CPU allocates memory block where it fits ▪ Memory fragmentation is like a Swiss cheese ▪ Lead to cache-misses Swiss-Cheese Memory
  • 11. TEST #2 TestCacheMissByDataSize.cs Read values in Arrays of different data types (10M elements)
  • 12. Test #2 - Result Iterate an array of int (4 bytes) 35ms Iterate an array of struct (32 bytes) 58ms Why ? Bigger struct (36 bytes) is even worse 60ms
  • 13. Test #2 - Result explain Iterate an array of int (4 bytes) 35ms Iterate an array of struct (32 bytes) 58ms Why ? Answer: CPU Cache, again
  • 14. Test #2 - Result explain Cache Pollution Un-used data still loaded Less space in cache-line More cache-misses
  • 15. Test #2 - Result explain Un-used data still loaded Less space in cache-line More cache-misses GameObject in OOP style ?
  • 16. Test #2 - Take it further Add 1 byte data to the struct. Then its size comes from 32 to 36 bytes (expect 33). Why ?
  • 17. Add 1 byte data to the struct. Then its size comes from 32 to 36 bytes (expect 33). Why ? Answer: Data alignment More: ▪ Try appending 1 more byte, size keeps at 36. ▪ Try prepending 1 more byte, size goes to 40. Test #2 - Take it further
  • 18. ▪ Data is put into 4-bytes “buckets”
 for fast access ▪ When added data doesn’t fit ▪ Next (& empty) bucket will be used ▪ Wasted un-used bytes = padding ▪ References: ▪ Stdio.vn ▪ Wikipedia ▪ Song Ho Ahn Data alignment Without data alignment
  • 20. Just re-order data from biggest to smallest size 8 bytes Test #3 - Result 12 bytes ???
  • 21. Cache-miss ▪ Fastest way to load data: NOT LOADING IT :) ▪ Second best ways ? ▪ Keep data small (if not, notice about data alignment) ▪ Keep data contiguous ▪ Separate data by function ▪ In Relational Database, sometimes we de-normalize for performance, too ! ◆ Problem #1: Encapsulation makes it hard to do this
  • 23. ▪ Function is split into instruction blocks ▪ CPU looks up these blocks from a table ▪ CPU loads these blocks into instruction cache (I$) ▪ Function call suffers from cache-miss, too !!! ▪ References: ▪ Wikipedia (Instruction Cycle) ▪ Wikipedia (Branch Misprediction) Function call
  • 24. TEST #4 TestVirtualFunctions.cs How overriden functions affect performance ?
  • 25. Test #4 - Result Direct call 35ms 1-level indirect call 61ms 10-levels indirect call 411ms
  • 26. ▪ Fastest way to call a function: NOT CALLING IT :) ▪ Second best ways: ▪ Keep high-performance function small (fits in cache) ▪ Keep narrow class hierarchy ▪ 1 function to process multiple instances, not 1 function for each instance ◆ Problem #2: Encapsulation / Polymorphism makes it hard to do this Function call
  • 27. Wait, they are OOP core ! Encapsulation + Inheritance + Polymorphism
  • 28. ▪ Multiple inheritance ▪ Useful for game development, bad architecture ▪ “Diamond of dead” ◆ Problem #3: Not an easy way to implement multiple inheritance properly Other OOP problems
  • 29. ▪ Multiple inheritance ▪ Useful for game development, bad architecture ▪ “Diamond of dead” ◆ Problem #3: Not an easy way to implement multiple inheritance properly ▪ Unit test ▪ My test uses some members, but I need to initialize them all !!! ◆ Problem #4: Unit test involves un-related constraints Other OOP problems
  • 30. ▪ Multiple inheritance ▪ Useful for game development, bad architecture ▪ “Diamond of dead” ◆ Problem #3: Not an easy way to implement multiple inheritance properly ▪ Unit test ▪ My test uses some members, but I need to initialize them all !!! ◆ Problem #4: Unit test involves un-related constraints ▪ Jobify, False sharing, ... Other OOP problems
  • 32. ▪ Focus on how data is laid out in memory ▪ Focus on how data is read / processed ▪ Build functions around data Data-Oriented Design
  • 33. ▪ Focus on how data is laid out in memory ▪ Focus on how data is read / processed ▪ Build functions around data ▪ References: ▪ DICE ▪ Mike Acton (Insomniac Games, Unity) ▪ Richard Fabian ▪ Keith O’Connor (Ubisoft Montreal) Data-Oriented Design
  • 34. “The purpose of all programs, and all parts of those programs, is to transform data from one form to another ” - Mike Acton -
  • 35. “When there is one, there are many ” - Mike Acton -
  • 36. “Designing the code around the data, not the other way around ” - Linus Torvalds -
  • 38. Test #5 - Result for (int i = 0; i < ELEMENTS_COUNT; ++i) { d = GetDistance(center, objects[i].position); if (minDistance > d) { minDistance = d; closestId = i; } } Iterate Array of “GameObjects” 209ms for (int i = 0; i < ELEMENTS_COUNT; ++i) { d = GetDistance(center, positions[i]); if (minDistance > d) { minDistance = d; closestId = i; } } Iterate Array of positions 128ms They’re almost identical, except line #2
  • 39. Test #5 - Take it further ▪ You already knew DOD is faster (from previous test results) ▪ Let’s improve the algorithm (current: 209ms) ▪ Use GetSquareDistance instead of GetDistance à 137ms ▪ *Eliminate too far objects & pick the 1st close-enough object à 36ms ▪ Reduce branch mis-prediction à 34ms *Human needs good-enough choice, not the optimal one.
  • 40. Test #5 - Take it further for (int i = 0; i < ELEMENTS_COUNT; ++i) { d = GetSqDistance(center, objects[i].position); if (d > MAX_SQ_DST) continue; if (d < MIN_SQ_DST) { closestId = i; break; } // ... original comparison here } Iterate Array of “GameObjects” 36ms for (int i = 0; i < ELEMENTS_COUNT; ++i) { d = GetSqDistance(center, positions[i]); if (d > MAX_SQ_DST) continue; if (d < MIN_SQ_DST) { closestId = i; break; } // ... original comparison here } Iterate Array of positions 25ms Your smart algorithm + DOD = AWESOME
  • 41. ▪ Reduce data cache-misses (Problem #1) ▪ Reduce function cache-misses, indirect function calls (Problem #2) ▪ Component over inheritance (Problem #3) ▪ Unit test = Feed input & Assert the output (Problem #4) ▪ References: ▪ Games From Within ▪ Tencent Data-Oriented Design
  • 43. ▪ Performance & flexibility ▪ It’s the FUTURE (click links to see more) ▪ Mentioned top companies (Insomniac Games, Ubisoft, EA/DICE, ...) ▪ Sony ▪ Intel ▪ Apple ▪ Riot Games ▪ Unity !!! (other, other, other, other) ▪ More ... Why should we care ?
  • 44. These masterpieces also use ECS * Click images for more details