Diseño Orientado a Datos

DOD

Escrito por Darjus Kazakevic · revisado el

Start from the data and how it is read, not from the nouns in the domain.

Data-oriented design (DOD) asks a different first question. Not “what are the things?”, but “what is the data, how much of it is there, and how is it read?”. On modern CPUs this often matters more than the algorithm. A read from main memory takes about 100 nanoseconds; a read from the L1 cache takes about 1.

In practice you store arrays of fields instead of arrays of objects. A loop that needs one field then reads only that field, in order, and the CPU can load it ahead of time. Game engines and analytics databases work this way. For code that is not hot, the layout is harder to read and gains nothing.

En esencia — Lay out data by how the hot code reads it, because memory access, not arithmetic, usually decides the speed.

Cuándo recurrir a él

Recurre a él cuando

  • A loop over large arrays that a profiler shows is memory-bound
  • Many entities processed the same way, every frame or every batch
  • Simulation, analytics or game code where throughput is the product

Reconsidéralo cuando

  • Code that is not hot, such as a checkout that runs once per customer
  • Small collections that already fit in the cache
  • Domain logic with many rules and few records, where clarity matters most

Conceptos clave

Cache line
The CPU loads memory in blocks of 64 bytes, not one value at a time. If the next value you need is in the same block, reading it is almost free. If not, the CPU waits.
Array of structs vs struct of arrays
An array of structs (AoS) stores each record's fields together. A struct of arrays (SoA) stores each field in its own array. A loop over one or two fields reads far less memory with SoA.
Entity Component System (ECS)
A common DOD architecture in games. An entity is only an ID. Components are plain data, stored in arrays by type. Systems are loops that run over every entity that has the components they need.
Hot and cold data
Split the fields a hot loop reads from the fields it rarely touches. The hot part stays small, so more of it fits in the cache at once.

Reglas de diseño

Lay out data by how it is read
Group the fields a hot loop reads into contiguous arrays. Each pass then loads only what it uses, and the CPU can prefetch the next values.
Measure before you rearrange
Struct-of-arrays code is harder to read and change. Pay that cost only where a profiler shows a loop is memory-bound. Elsewhere, the object that reads clearly is the right shape.
Process data in batches, not one item at a time
Write functions that take arrays and loop inside, not per-item methods called in a loop. One call over 10,000 items streams through memory; 10,000 calls jump around it.

Errores habituales

  • Rewriting cold code into struct-of-arrays “for speed”, with no profile. The code gets harder and nothing gets faster.
  • Holding an index into the arrays, then reordering them. The index goes stale and the wrong entity is updated.
  • Hot and cold fields mixed together again over time, until the hot loop reads 200-byte records to use 8 bytes.