Entity–Component–System (ECS) is a software architecture that separates object identity, object data, and the operations performed on that data. An entity identifies an object; components describe its properties or capabilities; and systems process entities that possess particular combinations of components. ECS is widely used in game development and simulation, especially where many objects share overlapping behaviors. It favors assembling objects from independent parts rather than defining every kind of object through a hierarchy of inherited classes.
Core concepts
Entities
An entity is a distinct identity within an ECS world or registry. It commonly takes the form of an integer or an opaque handle rather than an object containing its own data and methods.
An entity might represent a character, projectile, light, sound emitter, or an abstract task. Its meaning comes from its components and relationships, not necessarily from an intrinsic class. For example, an entity with Position, Velocity, and Sprite components can represent a moving visible object; adding Health makes it eligible for systems that process damage.
Entity identifiers are often recycled after destruction. To distinguish a new entity from an older entity that used the same storage slot, implementations may combine an index with a generation counter. A handle is valid only when its generation matches the current generation of that slot. This reduces accidental access through stale identifiers, although it does not replace broader lifetime management.
Components
A component represents one aspect of an entity. Typical examples include:
| Component | Example contents |
|---|---|
Position |
Spatial coordinates |
Velocity |
Speed and direction |
Health |
Current and maximum health |
Renderable |
References to mesh and material assets |
Parent |
Identifier of another entity |
In a data-oriented ECS, components primarily contain data, while systems implement behavior. Some implementations allow component methods for local operations or validation, so “components contain no methods” is a design convention rather than a universal requirement.
A tag component carries no ordinary payload: its presence alone expresses a property such as Selected, Disabled, or Enemy. Many ECS implementations allow at most one component of each type per entity, although relationship and multi-instance models can extend this convention.
Components in ECS should not be confused with independently deployed software components or user-interface components. The shared terminology does not imply the same architecture.
Systems
A system implements an operation over entities selected by their components. A movement system might require both Position and Velocity, while a rendering system requires Position and Renderable.
For a simulation time step , a simple movement operation is:
The system applies this operation to each matching entity. The formula is one possible update rule, not a defining feature of ECS.
Systems may be functions, objects, scheduled jobs, or collections of routines. They may also use shared resources, such as elapsed time, input state, or asset stores, that do not belong naturally to individual entities.
Composition and queries
ECS makes composition the main mechanism for describing objects. Instead of creating a class for every combination of capabilities, an application attaches the needed components to each entity.
For example:
- A stationary decoration has
PositionandRenderable. - A projectile has
Position,Velocity, andDamage. - A moving character has
Position,Velocity,Health, andInputControlled.
The movement system can process both projectiles and characters without requiring them to inherit from a common movable-object class. This contrasts with inheritance-heavy forms of object-oriented programming, although ECS can be implemented using object-oriented languages and can coexist with object-oriented subsystems.
A query commonly specifies required components, excluded components, and optional components. It may also filter by component values, relationships, or whether data has changed.
If denotes the set of entities possessing component type , a movement query excluding frozen entities can be expressed as:
This resembles selection and joining in a relational database, but ECS implementations generally specialize for application-state access and repeated iteration rather than providing the full semantics of a database. Features such as persistence, transactions, and a general-purpose query language are not inherent to ECS.
In pseudocode, a movement system might be written as:
for each entity with Position and Velocity, excluding Frozen:
position = writable Position of entity
velocity = readable Velocity of entity
position += velocity * delta_time
Query declaration and execution vary. Some frameworks infer access requirements from function parameters; others use explicit query objects or manually maintained collections.
Storage models
ECS defines a separation of responsibilities, not a mandatory memory layout. Its performance depends heavily on the data structures used to store components and find matching entities.
Per-entity and map-based storage
A straightforward implementation stores a component collection for each entity, or a map from entity identifiers to component values for each component type. A hash table can support convenient lookup and dynamic modification.
Such designs are relatively easy to implement but may involve pointer chasing, scattered allocations, and repeated lookup during iteration. They remain useful when simplicity matters more than maximum throughput.
Sparse-set storage
A sparse-set design typically maintains a dense collection of component values and a mapping from entity indices to positions in that collection. A corresponding dense collection of entity identifiers identifies the owner of each value.
Removing a component may swap the last element into the removed element’s position. This supports efficient deletion but can change iteration order and invalidate references to moved elements.
For a query involving several component types, an implementation can iterate one component pool—often the smallest—and test whether each entity has the other required components. Additional grouping or indexing can reduce lookup overhead.
Archetype storage
An archetype is a set of component types. Entities with the same component-type combination are stored together, often in chunks containing separate arrays for each component.
A query selects matching archetypes and iterates their contents. This avoids performing the same component-presence checks separately for every entity. Contiguous storage can improve use of the CPU cache and reduce memory-access overhead.
Adding or removing a component generally changes an entity’s archetype. Its retained component values must then be transferred to storage for the destination archetype. This makes structural changes potentially more expensive than ordinary component-value updates.
Many distinct component combinations can also create numerous small archetypes, leading to partially filled chunks and increased bookkeeping. Hybrid implementations combine archetype storage, sparse sets, indirect storage, or other techniques.
Data-oriented design and performance
ECS is often associated with data-oriented design, which organizes programs around how data is stored, accessed, and transformed. The two are related but not synonymous: an ECS can have poor memory locality, and a non-ECS program can be strongly data-oriented.
Separating component types into contiguous arrays can avoid loading irrelevant fields. A movement system may need positions and velocities but not names, textures, or inventory contents. Processing only the required data can reduce memory traffic and make workloads more suitable for compiler optimization or single instruction, multiple data execution.
Performance benefits are workload-dependent. ECS does not automatically make an application faster. Costs can include:
- Query setup and matching.
- Component lookups across separate storage pools.
- Entity migration after component additions or removals.
- Indirection through entity relationships.
- Scheduling, synchronization, and framework bookkeeping.
A specialized loop over a simple array may outperform a general ECS for a narrow task. Meaningful comparison requires measuring representative workloads, including creation, destruction, mutation, and traversal—not only steady-state iteration.
Scheduling and parallel execution
Systems frequently run within a frame loop or simulation schedule. Their order may be significant: input can update movement intentions, movement can update positions, and rendering can consume the resulting state.
Frameworks can use declared component access to identify opportunities for parallel execution. Systems that only read the same data may run together. Systems that write disjoint data may also run concurrently.
If one system writes a component type while another reads or writes it, overlapping execution generally requires ordering or synchronization. Otherwise, the application may encounter a race condition. More precise query analysis can sometimes prove that two systems access disjoint entity sets even when they use the same component type.
Structural changes require particular care because adding, removing, or moving components can invalidate an active iteration. A common solution is a command buffer: systems record requested changes, and the framework applies them at a defined synchronization point.
Parallel scheduling alone does not guarantee deterministic results. Execution order, iteration order, floating-point arithmetic, and competing commands can all affect outcomes. Deterministic simulation requires explicit rules for these interactions.
Relationships, events, and lifecycle
ECS does not eliminate relationships between objects. A component can reference another entity to express parenthood, ownership, targeting, or membership. Some frameworks provide dedicated relationship facilities and relationship-aware queries.
References raise lifecycle questions. When a parent or owner is destroyed, related entities might be destroyed, detached, reassigned, or left with references that fail validity checks. These policies belong to the application or framework rather than following automatically from ECS.
Systems can communicate through shared components, event queues, or message passing. Events are useful for transient occurrences such as collisions or completed actions; components are generally useful for state that remains available for querying. Excessive event coupling can make execution flow difficult to trace, just as excessive shared mutable state can obscure dependencies.
Persistence likewise requires additional design. Runtime entity handles are not necessarily stable save-file or network identifiers. Loading a saved world may require remapping stored references to newly allocated entities.
Development and implementations
ECS emerged from broader component-based approaches in game development. These approaches addressed difficulties associated with deep inheritance hierarchies and numerous combinations of object capabilities. Later implementations increasingly emphasized compact data storage, bulk processing, and explicit execution dependencies.
The terminology has not always been consistent. A component-based engine may attach behavior-bearing objects to a game object and update those objects individually. A data-oriented ECS typically separates component data from systems that process groups of matching entities. Both support composition, but their storage and execution models can differ substantially.
Examples of ECS implementations include Unity’s Entities package, Bevy’s ECS, EnTT, and Flecs. They differ in storage strategies, query facilities, scheduling, relationship support, and integration with other engine services. “ECS” therefore identifies a family of architectural approaches rather than a single standardized interface.
Applications and limitations
ECS is particularly useful for games and simulations containing many entities with recurring combinations of properties. Other applications include visualization tools and interactive environments where bulk updates and dynamic composition are important.
The architecture also introduces trade-offs:
- Distributed behavior: Understanding one entity’s behavior may require tracing several systems rather than reading a single class.
- Implicit requirements: An application may require one component to accompany another, but a basic ECS does not automatically enforce that invariant.
- Ordering dependencies: Systems can depend on earlier updates even when their query signatures do not fully express the dependency.
- Structural-change costs: Frequent component additions and removals may cause migration, query maintenance, or synchronization overhead.
- Tooling requirements: Inspecting entities, queries, schedules, and deferred changes benefits from dedicated debugging tools.
- Integration boundaries: Existing physics, graphics, scripting, or user-interface libraries may use object models that require adapters.
ECS also does not prescribe persistence, networking, ownership policy, asset management, or the complete architecture of an application. It is often used alongside object-oriented code, event-driven modules, and specialized algorithms rather than as a replacement for every other programming technique.