skip to content

ECS and Data-Oriented Design

Why engines trade deep inheritance for composition over flat arrays: archetype and sparse-set storage, SoA layout and cache-friendly iteration. A signature senior question on designing for the cache.

on this pageshow

explore

questions

5

Why do game engines favour composing entities from components over deep inheritance hierarchies of game-object classes?

level: juniorimportance: must knowfreq 70%

answer

  1. features that cut across branches
  2. the diamond and the god object
  3. a container of component objects
  4. entity as an id, data in arrays
  5. systems run over matching sets

basics

~20 s

Deep inheritance forces every game object into one fixed taxonomy, so features that cut across branches end up duplicated or hoisted into a bloated base class. Composing entities from components lets any combination exist without a new class.

solid answer

~40 s

Game features cut across any class tree: a barrel, a door and a vehicle can all be damageable, and only some of them burn or can be driven. With inheritance you either duplicate code across branches, reach for multiple inheritance and hit the diamond (one shared ancestor inherited twice), or hoist features toward the root until it becomes a god object every class pays for. A **component-object model** fixes the taxonomy: an object is a container of components, so designers combine behaviours as data. An **ECS** goes a step further: an entity is just an id, components are plain data in contiguous per-type storage, and systems run one function over every entity whose components match a query, which also fixes the memory layout.

go deeper

for a junior

Recall the two failure shapes of deep hierarchies, the diamond and the god object, and be able to say what an entity, a component and a system each are.

for a middle

Explain how the component-object model fixes the taxonomy problem and why its scattered, separately allocated components still cost cache misses and indirect calls every frame.

for a senior

Show the trade-offs you have lived with: relationships as validated ids, scattered one-off logic, structural-change hazards, and where the layout gain actually appears in a profile.

for a principal

Frame the choice per subsystem: composition for expressiveness everywhere, contiguous ECS storage where many similar entities dominate frame time, and an object model where unique scripted logic dominates.

## The problem: one class tree for features that cut across it A classic engine starts with a root class for every object in the world (call it `WorldObject`) and derives from it: `Character`, `Vehicle` and `Prop`, then `Enemy` and `Player` under `Character`, `Car` and `Boat` under `Vehicle`, and so on. **Inheritance** works while every new feature belongs to exactly one branch. Game features rarely do. Being damageable applies to characters, vehicles and props; burning applies to some props and some characters; being drivable applies to vehicles and, sooner or later, to a mount that is also a character. When features cut across the tree, a team is pushed into one of a few bad shapes: - **Duplicated code.** The same damage logic is copied into `Character` and `Prop`, and the copies drift apart. - **The diamond.** A class inherits from two bases that share a common ancestor (an amphibious vehicle deriving from both `Car` and `Boat`), so the ancestor's state is either duplicated or needs special handling to be shared once, and calls into it become ambiguous. - **The god object.** To reuse a feature everywhere, it is hoisted toward the root until the root class carries health, inventory, physics, AI and audio fields that most objects never use. Every object pays their memory, and every change to the root risks everything below it. - **A programmer bottleneck.** Every new combination a designer wants needs a new class, written and compiled by a programmer. - **A fragile base.** A change to a widely used base class ripples into dozens of subclasses that overrode it in slightly different ways. ## First fix: the component-object model The **component-object model** keeps an object but stops using inheritance to describe what it can do. A game object becomes a container that owns a list of **components** (a health component, a burnable component, a drivable component), each holding its own data and usually its own update method. A barrel and a soldier both get the health component; only the barrel gets the explosive one. New combinations come from data an editor writes, not from new classes, and features no longer have to pick a branch. What it does not fix is the cost at run time. Each component is typically a separately allocated object, so the components of one type are scattered across the heap. A frame update walks every object, then every component, through pointers, paying an indirect call per component and a likely cache miss per hop. Components that need each other look each other up by type at run time. ## Second fix: the entity-component-system model An **entity-component-system** design (ECS) splits the three roles apart: 1. An **entity** is only an identifier, usually an index plus a generation count, with no data and no behaviour of its own. 2. A **component** is plain data, such as a position or a velocity, stored with the other components of its type in contiguous memory. 3. A **system** is a function that runs over every entity whose components match a query, such as everything with a position and a velocity. Composition is as free as in the component-object model, and the memory layout now follows the work: a movement system reads two dense arrays from start to end instead of chasing pointers. Behaviour for one kind of data lives in one place rather than being spread over many component classes. ## Comparison | | Deep inheritance | Component objects | ECS | |---|---|---|---| | How capabilities combine | Fixed by the class tree | Components attached per object | Components attached per entity id | | Where data lives | Inside each object | In separately allocated component objects | In contiguous per-type storage | | Where behaviour lives | Virtual methods on the classes | Update methods on each component | Systems that run over queries | | A new combination needs | A new class | Data only | Data only | | Typical hot-loop cost | Virtual calls, scattered objects | Virtual calls, pointer chasing | Linear walks over arrays | ## What composition costs Neither composition model is free, and a strong answer names the trade-offs: - **Relationships become ids.** A parent, a target or an inventory slot is a stored identifier that must be checked for validity, not a pointer that is simply followed. - **One-off logic can scatter.** A single unique, heavily scripted object may read more clearly as one object than as six components and four systems. - **Debugging changes shape.** You inspect rows of component data and which systems touched them, not one object in a debugger. - **Structural changes need care.** Adding or removing a component while a system iterates can disturb storage, which is why many designs defer those changes. - **Speed is not automatic.** The layout gain shows up where many similar entities are processed every frame; a few hundred heterogeneous objects may see little difference. The interview answer, then, is not that inheritance is slow. It is that a single class taxonomy cannot express features that cut across it, that composition fixes the expressiveness problem, and that an ECS additionally arranges data the way the processor wants to read it.

  • If a component-object model already fixes the taxonomy problem, what does an ECS add?
    The memory layout and the update model. In a component-object model each component is usually a separate heap object with its own update method, so a frame walks pointers to scattered memory and pays an indirect call per component. An ECS stores each component type contiguously and runs one system over all matching entities, so the hot loop reads dense arrays and the behaviour for one kind of data lives in one place.
  • Where does an ECS make things harder than an object model?
    Relationships and one-off logic. Parent links, targets and inventories become ids that must be checked for validity; a unique scripted object with complex behaviour is often clearer as one object than as a scatter of components and systems; and debugging means inspecting rows of data rather than one object. Adding or removing components while iterating also needs care, which is why many designs defer those changes.

  • Inheritance is only a problem because virtual calls are slow.
  • Multiple inheritance solves cross-cutting features cleanly.
  • An ECS entity is a base class that components derive from.
  • Moving shared behaviour into the root class is a fine way to reuse it.
  • An ECS is always faster, so every game object should become one.
open as a page

What is the difference between array-of-structures and structure-of-arrays layouts, and why does SoA speed up a movement system over fifty thousand units?

level: middleimportance: must knowfreq 65%

basics

~20 s

Array-of-structures stores each unit's fields together; structure-of-arrays stores each field in its own array. A movement system reads only positions and velocities, so SoA streams just those bytes and fills each fetched cache line with useful data.

open as a page

In an ECS, why is adding or removing a component while a system iterates its query dangerous, and how does a command buffer fix it?

level: middleimportance: should knowfreq 55%

basics

~20 s

Adding or removing a component moves an entity's data to other storage, which can skip or revisit entities and invalidate pointers mid-iteration. A command buffer records such changes and applies them later at a safe sync point.

open as a page

How do archetype storage and sparse-set storage differ in an ECS, and which suits a status that is added and removed every few frames?

level: seniorimportance: should knowfreq 55%

basics

~20 s

Archetype storage keeps entities with identical component sets in shared tables: dense multi-component iteration, but adding a component copies the whole entity. Sparse sets store each type separately: constant-time add and remove, indirect joins. A frequently toggled status suits sparse sets.

open as a page

When should a team adopt an ECS in an existing game built on inheritance-based objects, and how would you migrate it incrementally?

level: principalimportance: should knowfreq 35%

basics

~20 s

Adopt an ECS where profiling shows hot loops over many similar objects stalling on memory. In a shipped game, start with one hot subsystem as an ECS island, bridged to the object model by ids, and expand only on measured wins.

open as a page