ECS & Data-Oriented Design

ECS & Data-Oriented Design

Entity Component System (ECS) organizes game state around entities, components, and systems. Data-Oriented Design (DOD) is broader: it designs data layout and processing around how data is accessed and transformed. They are related, not synonyms. An ECS can have poor data locality, and DOD does not require ECS.

1. The Three ECS Parts

A movement system might process Transform + Velocity; a render extraction system might process Transform + Sprite.

Processing flow

flowchart TD
    A[Input and gameplay events] --> B[Update component data]
    B --> C[Movement system]
    C --> D[Physics and collision]
    D --> E[Gameplay systems]
    E --> F[Render extraction]
    F --> G[Renderer]
    H[Entity IDs and component storage] --> C
    H --> D
    H --> E
    H --> F

The exact order depends on dependencies. For example, physics may need to run before gameplay reads final collision results.

2. Why Separate Data and Behavior?

An object-oriented design may group an enemy's position, velocity, health, update(), render(), and take_damage() together. This is often perfectly reasonable.

A data-oriented design asks what data a particular operation actually needs. A movement pass needs positions and velocities—not dialogue trees, inventories, or animation metadata. Processing only the relevant data in batches can reduce indirection and improve cache use.

3. Memory Layout: AoS vs SoA

Array of Structures (AoS)

Each entity's fields are grouped together:

Entity_State :: struct {
    position: [3]f32,
    velocity: [3]f32,
    health: f32,
}
entities: []Entity_State

This is convenient when code commonly uses most fields for one entity. A movement pass, however, steps over fields such as health that it does not need.

Structure of Arrays (SoA)

Each field is stored in its own array:

positions:  [][3]f32
velocities: [][3]f32
health:     []f32

A movement pass can iterate over contiguous position and velocity arrays:

flowchart LR
    A[Movement system] --> B[Positions array]
    A --> C[Velocities array]
    B --> D[Update positions]
    C --> D
    E[Health array] -. not needed .-> A

Neither layout is always best. AoS can be simpler for small groups or operations using most fields. SoA often suits large homogeneous batches. Array of Structures of Arrays (AoSoA) is another option when chunking and SIMD-friendly processing matter.

4. Common ECS Storage Strategies

Sparse sets

A component type typically has a packed array of component values, a packed array of entity IDs, and a sparse lookup from entity ID to packed position. This supports straightforward component insertion/removal and fast iteration over one component type. Queries requiring several component types need to match entity sets or choose one set as the driver.

Archetypes and chunks

An archetype represents one unique combination of component types. Entities with the same combination are stored together in chunks.

Archetype: Transform + Velocity
  Entity 1 | Entity 8 | Entity 12

Archetype: Transform + Sprite
  Entity 2 | Entity 5 | Entity 9

Archetype: Transform + Velocity + Sprite
  Entity 3 | Entity 4

A query for Transform + Velocity visits only matching archetypes. Adding or removing a component may move an entity to a different archetype.

flowchart TD
    A[Create entity] --> B[Choose component set]
    B --> C[Find matching archetype]
    C --> D[Place entity in chunk]
    D --> E[System queries required components]
    E --> F[Iterate matching chunks]
    F --> G{Component set changed?}
    G -->|Yes| H[Move entity to new archetype]
    G -->|No| E

Archetypes can make matching queries and iteration efficient, but structural changes can be more expensive. Sparse sets are often simpler and more flexible. Choose based on workload, not fashion.

5. A Small Odin System

This SoA-style example assumes array indices correspond to entities. It is an illustration of data processing, not a complete ECS:

Position :: [3]f32
Velocity :: [3]f32

move_system :: proc(
    positions: []Position,
    velocities: []Velocity,
    dt: f32,
) {
    assert(len(positions) == len(velocities))

    for i in 0..<len(positions) {
        positions[i] += velocities[i] * dt
    }
}

A production ECS also needs entity-to-component lookup, safe add/remove/destroy operations, query logic, and rules for structural changes during iteration. Use generation counters in entity handles so a recycled ID slot does not accidentally validate a stale reference.

6. Queries, Dependencies, and Scheduling

Systems should make clear which components they read and write. This exposes ordering requirements and can eventually help schedule independent work in parallel.

System Reads Writes
Movement Velocity Transform
Physics Transform, Collider, Velocity Transform, Velocity, Contact
Health DamageEvent, Health Health, DeathEvent
Render extraction Transform, Renderable RenderInstance buffer
flowchart TD
    A[Input] --> B[Movement]
    B --> C[Physics]
    C --> D[Gameplay reactions]
    D --> E[Render extraction]
    B --> F[Audio events]
    D --> F

This is an example dependency graph, not a universal schedule. Define who owns writes to shared data and how events cross system boundaries.

7. Practical DOD Rules

8. ECS Is Not the Same as Every Component-Based Engine

Architecture Typical organization Important distinction
Data-oriented ECS Entity IDs, component storage, systems Queries process component data; storage layout is a core design choice
O3DE Component-Entity Entities composed from lifecycle-managed components Component objects and EBus are central; it is not automatically an archetype/SoA ECS
Godot Node tree, scenes, Resources, engine servers Composition is organized around a scene hierarchy
Unreal Actors, Actor Components, subsystems, Mass Entity Actor composition is not itself a data-oriented ECS; Mass is a separate entity-oriented framework

See O3DE Overview & Architecture and Godot Basics & Architecture for engine-specific models.

9. When Should You Use ECS?

ECS is worth considering when many entities share small sets of data, need repeated batch processing, or benefit from flexible composition and queryable systems.

It may be unnecessary when there are few entities, most objects have unique behavior, or a full query/archetype engine would add more complexity than it removes.

Start with the simplest data layout that solves the problem. Add query caches, archetypes, job scheduling, and SIMD paths only when profiling or project requirements justify them.