Data-oriented Design

Anton Christoffersson

Created: 2023-05-28 Sun 22:11

1. Introduction

  • Data-oriented design (DOD) is an alternative approach to object-oriented design (OOD) that emphasizes optimizing data layout to improve performance.
  • While OOD focuses on encapsulating data within objects, DOD separates data and behavior, and organizes data fields in a way that is optimized for how they are accessed and transformed during runtime.
  • DOD can lead to significant performance improvements in scenarios with large amounts of data and complex operations.

2. Limitations of Object-Oriented Design

OOD emphasizes encapsulating data within objects, which can lead to poor cache locality and frequent cache misses which cause performance issues.

2.1. Limitations of Object-Oriented Design

Simple example of an application performing work on some generic data.

const int ARRAY_SIZE = 100000;

struct FOOD {
  double d1;
  double d2;
  double d3;
};

int main() {
  FOOD dataArray[ARRAY_SIZE];

  for (int i = 0; i < ARRAY_SIZE; i++) {
    dataArray[i].d1 = i;
    dataArray[i].d2 = i;
    dataArray[i].d3 = i;
  }

  for (int i = 0; i < ARRAY_SIZE; i++) {
    dataArray[i].d1 += sqrt(dataArray[i].d1);
    dataArray[i].d2 += sqrt(dataArray[i].d2);
    dataArray[i].d3 += sqrt(dataArray[i].d3);
  }
}

3. Principles of Data-Oriented Design

  • DOD emphasizes optimizing data layout to improve memory locality leading to better cache efficiency.
  • This involves separating data and behavior, organizes data fields in a way that is optimized for how they are accessed and transformed during runtime.

3.1. Principles of Data-Oriented Design

Modified example code

const int ARRAY_SIZE = 100000;

struct FDOD {
  double d1[ARRAY_SIZE];
  double d2[ARRAY_SIZE];
  double d3[ARRAY_SIZE];
};

int main() {
  FDOD dataArray;

  for (int i = 0; i < ARRAY_SIZE; i++) {
    dataArray.d1[i] = i;
    dataArray.d2[i] = i;
    dataArray.d3[i] = i;
  }

  for (int i = 0; i < ARRAY_SIZE; i++) {
    dataArray.d1[i] += sqrt(dataArray.d1[i]);
  }
  for (int i = 0; i < ARRAY_SIZE; i++) {
    dataArray.d2[i] += sqrt(dataArray.d2[i]);
  }
  for (int i = 0; i < ARRAY_SIZE; i++) {
    dataArray.d3[i] += sqrt(dataArray.d3[i]);
  }
}

3.2. Principles of Data-Oriented Design

==OOD==
OOD update took avg 1316 microseconds.
D1  miss rate:       7.0% (    6.5%     +     7.7%  )
==DOD==
DOD update took avg 844 microseconds.
D1  miss rate:       4.8% (    5.1%     +     4.1%  )

3.3. Principles of Data-Oriented Design

Spec of the different memory acces speeds of 11th gen Intel of cpus:

Type Cycles
Register 0.5
L1 2-3
L2 6-8
L3 14-22
DRAM 90-100

4. Benefits of Data-Oriented Design

  • DOD can improve the performance of an application by optimizing the way data is organized and accessed, reducing cache misses and improving CPU utilization.
  • DOD can make code simpler and easier to maintain by separating data from behavior and minimizing dependencies between components.
  • DOD can makes it easier to split the work of processing data across multiple processors or threads by not locking data to objects.

5. Resources