Data-Oriented Programming
created: 18 January 2021
revision: 1
Hot-Cold Splitting
- Split often used data from rarely used one. Could be tricky
Spatial locality
- data that will be compared or used in calculation to be in close range (in the source code)
- Instead of using and
if(something)
inside afor/while
loop, order data according to thatsomething
and loop over applicable ones only.- See
swap
operation for arrays inC
- See
Data alignment (very good one)
- Memory chunks are taken by 8 bytes. If an object is 4 bytes and the next is 2 to 4 bytes, they can be put together. But if the next is 8, there is not enough place in the current to start, so a new one is taken and the resting 4 bytes from the initial chunks are not used.
struct MyStruct{
bool b;
char * c;
int i;
}
struct MyStructOptimized{
char * c; // 8 bytes
int i; // 4 bytes
bool b; // 1 byte
}
sizeof(MyStruct) // return 24 bytes on 64bit platform
sizeof(MyStructOptimized) // return bytes 16 on 64bit platform
// memory 00 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 /21 22 23
// myStruct b - - - - - - - c c c c c c c c i i i i - - - -
// myOptim c c c c c c c c i i i i b - - -
Array of Structures vs Structure of Arrays
class MyClass{
private:
int64_t _timeToGo
int64_t _direction
int64_t _somethingElse
}
#define MAX_OBJECT 30
class MyClass2{
private:
int64_t _timeToGo[MAX_OBJECT]
int64_t _direction[MAX_OBJECT]
int64_t _somethingElse[MAX_OBJECT]
}