Archetypes are the fundamental storage unit in an archetypal ECS. An archetype represents a unique combination of components. All entities with exactly the same set of components belong to the same archetype.Documentation Index
Fetch the complete documentation index at: https://mintlify.com/Ukendio/jecs/llms.txt
Use this file to discover all available pages before exploring further.
What is an Archetype?
When you create an entity, it starts with no components. All entities with no components belong to the ROOT_ARCHETYPE. When you add components, the entity moves to a different archetype.[]- ROOT_ARCHETYPE (no components)[Position]- Entities with only Position[Position, Velocity]- Entities with Position and Velocity[Position, Velocity, Health]- Entities with Position, Velocity, and Health
Archetype Transitions
When you add or remove a component, the entity moves to a different archetype. This transition involves several operations:- Remove the entity from the old archetype’s entity list
- Copy component data from old columns to new columns (if the component exists in both)
- Add the entity to the new archetype’s entity list
- Update the entity’s record to point to the new archetype
- Update the entity’s row index to its position in the new archetype
This is why adding/removing components is more expensive than setting component values. Moving archetypes requires copying data and updating multiple data structures.
Archetype Creation
When an entity needs to move to an archetype that doesn’t exist yet, jecs creates it. This involves:- Allocate a new archetype ID (increments max_archetype_id)
- Create storage columns for each component
- Register the archetype
- Update component records
- Propagate to cached queries that need to update their archetype lists
Archetype Graph
Archetypes are connected in a graph structure. Each archetype has edges that point to other archetypes you can reach by adding or removing components. For example:ROOT_ARCHETYPEhas an edge for+Position→[Position][Position]has an edge for+Velocity→[Position, Velocity][Position]has a backwards edge for-Position→ROOT_ARCHETYPE
Edge Caching
The archetype graph is cached. When you add a component, the ECS first checks if there’s already an edge from the current archetype for that component. If the edge exists: Use the cached archetype (fast path) If the edge doesn’t exist:- Create a new component list:
[Position, Velocity](sorted) - Hash it to get the type string
- Check if archetype exists (might have been created by another entity)
- If not, create it
- Cache the edge in both directions
[Position]→[Position, Velocity](for adding Velocity)[Position, Velocity]→[Position](for removing Velocity)
Structure of Arrays (SoA)
Archetypes store component data using a Structure of Arrays layout:Performance Benefits
The archetype + SoA design provides several performance benefits:1. Cache Locality
Component data for the same type is stored contiguously in memory. When you iterate over entities, you access memory sequentially:2. Batch Processing
You can process entire arrays of components at once:3. Query Efficiency
Queries only iterate over archetypes that match the query. If you query(Position, Velocity), you skip all entities without both components.
Archetype Fragmentation
Relationships can create many archetypes. Each different target creates a new archetype:Summary
- Archetypes group entities with identical component sets
- SoA layout stores components in separate arrays for cache efficiency
- Archetype graph caches transitions for fast component add/remove
- Query efficiency comes from only iterating matching archetypes
- Trade-off: Adding/removing components is slower than setting values, but queries are very fast