Skip to content

KenseiECS Architecture ​

How the framework works internally. Written for contributors and for users who want to reason about the cost of an operation before they write it. The user-facing API is documented in the guide and the core concepts; this document explains the mechanisms behind that API.

All code discussed here lives in KenseiECS/Core, KenseiECS/Systems, KenseiECS/Unity and KenseiECS.Generators.

Data model at a glance ​

World
├── entity slots     _generations[i]  _alive[i]  _componentCounts[i]      indexed by entity index
├── component masks  _componentMasks[word][i]                            ulong per (word, entity); 64 types per word
├── free list        _freeIndices[] / _freeCount, _nextIndex             slot recycling stack and high-water mark
├── pool registry    _pools[typeIndex]                                   ComponentPoolBase, created on first Pool<T>()
├── filter registry  _allFilters                                         plus _filtersByType[typeIndex] -> { Include[], Exclude[], Any[] } or null
├── groups           _groups                                             owning groups; each owned pool points back via _ownerGroup
├── world listeners  IWorldEventListener[]                               copy-on-write; _dispatch = listeners present and not suppressed
└── counters         _tick, _changeVersion

ComponentPool<T>     _sparse[entity] -> dense index      _denseEntities[d] -> entity      _denseData[d] -> T
                     _changedVersions[d] (opt-in)        _addHooks / _removeHooks (listeners, owner group, tracking; list owner / running frames)
Filter               paged sparse[entity] -> dense slot  _denseEntities[slot] -> entity   slot 0 = terminator
Group                _pools[]  _count                    members occupy dense 0.._count-1 of every owned pool, same order

Three kinds of identifiers appear throughout:

  • int entity index: a slot number. Filters and groups yield these; pools are addressed by them.
  • Entity handle: (Index, Generation), 8 bytes. World methods take these.
  • int type index: ComponentType<T>.Index, process-wide.

Entity slots ​

An entity is a slot index into parallel arrays owned by World:

ArrayMeaning
_generations[i]Generation currently stamped on the slot. Starts at 1.
_alive[i]Whether the slot currently holds a live entity.
_componentCounts[i]Number of components on the entity. Drives auto-destroy.
_componentMasks[w][i]Bit t & 63 of word t >> 6 is set when the entity has component type t.
_usedWords[i]Which mask words from 1 on may hold a component of the entity: bit w for word w (1 to 62), bit 63 for every word from 63 on. Set by an Add whose word was zero, reset only when the slot is released or the world cleared, so a clear bit means a zero word. DestroyEntity and CopyEntity read word 0 and the words with a bit. Allocated with the second mask word, so a world whose types all have indices below 64 has none (type indices are process-wide).

_nextIndex is the high-water mark: slots below it have been handed out at least once. _freeIndices is a stack of released slots. CreateEntityInternal pops the free stack if it is not empty, otherwise takes _nextIndex++ and grows every slot array (doubling) when needed.

Generation changes on reuse, not on free ​

csharp
// CreateEntityInternal, free-list branch
index = _freeIndices[--_freeCount];
int generation = _generations[index] + 1;
if (generation == 0) {
    generation = 1;
}
_generations[index] = generation;

DestroyEntity clears _alive[i] and pushes the slot onto the free stack but leaves _generations[i] alone. The generation only advances when the slot is handed to a new entity. The consequence:

  • IsAlive(handle) is idx < _nextIndex && _alive[idx] && _generations[idx] == handle.Generation. A handle to a destroyed entity fails on _alive; after the slot is reused it fails on the generation.
  • GetEntity(int) is simply new Entity(index, _generations[index]). On a dead slot it returns exactly the handle the dead entity had while alive, and IsAlive is false for it. If the generation were bumped on free instead, GetEntity on a dead slot would return the handle of whatever entity is created in that slot next: a forged handle that starts out dead and later comes alive for an entity the caller never saw. Bumping on reuse closes that hole, which is why the debug layer no longer needs to throw on GetEntity for dead slots.
  • TryGetEntity(int, out Entity) is the bounds-checked form for arbitrary integers: it returns true and the handle only when index < _nextIndex and the slot is alive, and Entity.Null otherwise. GetEntity has no bounds check and is meant for indices a filter, group or pool just yielded.

Entity.Null is default(Entity), i.e. (0, 0). Generations start at 1 and wrap back to 1, never 0, so Entity.Null never matches a real entity in any world.

World.Clear() increments the generation of every slot below _nextIndex (alive or not), then resets _nextIndex and the free stack to zero. Every handle issued before Clear therefore fails the generation check afterwards, even if its slot is immediately reused. An AutoReset that throws during Clear does not stop it: every component of every pool is reset and dropped, filters and groups are emptied, then the first exception leaves (up to 2.4.0 the pools after the failing one, the filters and the groups kept their contents while the entities were already gone).

Component type registry ​

ComponentType<T>.Index is a static readonly int initialized from ComponentType.Register(typeof(T)) on first touch of the generic instantiation. Registration is process-wide, guarded by a lock, and stores the reverse map index -> Type, so ComponentType.TypeOf(int) and ComponentType.NameOf(int) resolve any index issued so far. Indices are dense and assigned in first-touch order; they are not stable across runs and must not be persisted. Snapshots identify types by name for this reason.

Because indices are process-wide and not per-world, every World in the process shares the same numbering. A world only allocates a pool for a type when Pool<T>() is first called on it; _pools, _filtersByType and the mask words are sized by the highest type index the world has seen.

Component pools ​

ComponentPool<T> is a sparse set per component type:

_sparse[entityIndex]      -> dense index, or -1
_denseEntities[dense]     -> entityIndex          (ComponentPoolBase)
_denseData[dense]         -> T                     (ComponentPool<T>)
_changedVersions[dense]   -> int                   (ComponentPool<T>, null unless TrackChanges was called)
_count                    -> number of live entries; dense arrays have no gaps

Has(int) is entityIndex < _sparse.Length && _sparse[entityIndex] != -1. Get(int) is two array reads. Iterating RawData/RawEntities from 0 to Count walks every component of the type contiguously.

Hooks and the _addHooks / _removeHooks fast path ​

Three optional features hang off a pool, each represented by a nullable field: IComponentListener<T> listeners (_listeners), an owning group (_ownerGroup) and change tracking (_changedVersions). A fourth is fixed by the type: _listOwner is set for components that hold a pooled list (EventBuffer<T>, see built on the core), and only Add needs it. One bool per direction is kept equal to "at least one of the three is set", plus the list owner for _addHooks and "a hooked call of this pool is running" for _removeHooks: AddListener and TrackChanges set both, the constructor sets _addHooks for a list owner, SetOwnerGroup, the RemoveListener that empties the list and the end of the outermost running call recompute them through RefreshHooks.

Add tests _addHooks and Remove tests _removeHooks. A pool with no hooks runs the plain path with one predictable branch and no further field reads; a pool with hooks calls the NoInlining helpers AddHooked / RemoveHooked, which examine the fields individually (RemoveHooked hands removals with typed listeners to RemoveNotified, which holds the try/finally of the hook frames, so a tracking or group pool does not pay for it). Most pools in a world have no hooks, so the common structural change stays at its minimum cost regardless of how many features exist.

Re-entrant calls: hook frames ​

Typed listeners run user code in the middle of an Add or Remove of their own pool, and that code may call the pool again: remove the same component (an OnRemoved that destroys its entity), remove a linked entity's component that removes this one in turn, or remove the component and add it again, to the same entity or to a new one that takes the freed slot. A pool with listeners therefore keeps a small stack of frames, one per Add or Remove whose typed listeners are running: the entity, whether it is a removal, a Done flag and, for a removal, the next listener to notify. The innermost frame lives in plain fields (_topEntity, _topNext, _topRemoving, _topDone), so a call that does not nest touches no array; a nested call moves it into _frames (four entries, allocated by the first AddListener, doubled for deeper nesting) and back when it returns.

  • A Remove that finds a frame for its entity that is a removal and not Done joins it: it continues the notification from the frame's next listener, over the listener array the removal started with, and performs the removal. Linked entities that remove each other (A removes B, B removes A) therefore end: the second Remove of A joins A's frame instead of starting a new notification, so every listener gets one OnRemoved per component and a Has-guarded cycle cannot recurse forever.
  • The swap-remove marks every frame of its entity Done. A call whose frame is Done stops after the listener that returned: the component it was working on is gone, and whatever the slot holds now (the same entity's re-added component, or a new entity's) is a different component, announced by its own Add. The outer Remove does not remove it, and the outer Add gives the rest of its listeners no OnAdded. World.Clear from a listener marks every frame Done too, and ends a DestroyEntity in progress (see world.DestroyEntity).
  • Listeners that unsubscribe during a removal do not shift it: AddListener and RemoveListener record the array being replaced in every running removal that has not recorded one yet, so the common case stores no array reference (and pays no GC write barrier).
  • While a frame runs, _removeHooks stays set even if the last listener unsubscribed, so every nested Remove of the pool goes through the hooked path and keeps the frames exact.

A pool without listeners never pushes a frame: no user code runs inside its Add/Remove before the swap-remove, and filter and world listeners run after it.

Add ​

Add(int entityIndex, T value) throws if the entity already has the component (in every build), grows the sparse array to cover entityIndex and the dense arrays if full, writes the three arrays and increments _count. Then:

  • Without hooks: World.OnComponentAdded(entityIndex, TypeIndex) and return a ref to the component. OnComponentAdded tells whether a filter or world listener ran and, if one did, whether the entity still has the component: when none ran, nothing can have moved it and the ref is taken from the dense index Add wrote. While no filter of that type has listeners and no world listener is registered, no user code can run there: OnComponentAdded updates the filters without any re-check and reads nothing else; otherwise it reads the slot's generation first and re-checks the entity after each filter whose listeners ran. Each type's entry in _filtersByType counts the filters listing it that have listeners (Filter.AddListener and RemoveListener update every type of the filter when it gets its first or loses its last listener), so a listened filter of one type does not slow down the Add of the others. The part of OnComponentAdded that every Add runs (count, mask bit, the _filtersByType lookup) is inlined into the pool's Add; a type no filter mentions returns from there without a call, a type with filters continues in OnComponentAddedFiltered. In the same way OnComponentRemoved is inlined into Remove: without world listeners, a type no filter mentions clears its bit and decides the auto-destroy from the count it has just decremented, and a type with filters continues in OnComponentRemovedFiltered, which re-reads the count and the alive flag before the auto-destroy only when a filter listener could have run. While no user code can run, the filter loops (UpdateFiltersOnAddQuietly, UpdateFiltersOnRemoveQuietly) read the mask arrays once, not once per filter.
  • With hooks, AddHooked, in this order: for a list owner, check the stored value's list (below); stamp _changedVersions[dense] with world.NextChangeVersion() if tracking; group.OnAdded(entityIndex) if owned; with typed listeners, read the slot's generation and push an Add frame; World.OnComponentAdded (masks, counts, filters, world listeners); IComponentListener<T>.OnAdded with a ref to the stored value, to the typed listeners that were subscribed when the Add started and still are after the world update: one that a filter or world listener unsubscribed gets nothing, and one it subscribed is not told of this Add, which happened before it subscribed (the listener array is compared with the one read at the start; only when it changed, StillSubscribed builds the array to tell, an allocation); pop the frame; return the ref. A pool without typed listeners when the Add starts pushes no frame and sends no OnAdded for it. Either way a typed listener subscribed during an Add gets its first OnAdded from the next one (up to 2.4.0 it got this one when the pool already had a typed listener).

Every callback on the way (group, filter and world listeners, typed listeners) may move the component, by adding or removing this type elsewhere, or remove it again, even destroy the entity and let a new entity take its slot. So the dense index is re-read from _sparse before each typed listener and before returning the ref. Once the component is removed (the frame is Done), the remaining typed listeners get no OnAdded, also when the slot has the type again. Add returns a ref to a static scratch value (_orphan, reset to default each time) when the component is gone or the slot's generation changed, i.e. the entity was destroyed and the slot reused: writes through it land nowhere instead of on whichever entity took the freed slot. If a listener removed the component and added it to the same entity again, the ref points at the entity's new component.

The returned ref points at a dense slot, not at the entity. It is valid until the next Add of the same type to any entity (the dense array may be reallocated), or the next Remove of the same type from any entity (swap-remove moves the last component into the freed slot and clears the tail, so a ref to the entity that was last now points past _count). In a pool owned by a group, an Add or Remove of any of the group's types can also swap dense slots of every owned pool, for whichever entities occupy them.

Remove and swap-remove ​

Remove(int entityIndex) is a no-op when the component is absent. Otherwise, in order:

  1. With hooks, RemoveHooked (with typed listeners, RemoveNotified): IComponentListener<T>.OnRemoved runs first, before any index is read, because a listener may remove other components of the same type and shift the dense layout; the data is still intact here. The removal runs in a frame (hook frames): a listener that removes this very component again (an OnRemoved that destroys its own entity, or a linked entity that removes it back) joins the frame, continues the notification from the next listener and performs the removal; back in the outer Remove, the frame is Done and it stops. Every listener gets one OnRemoved, and the component is removed once.
  2. If T implements IAutoReset<T>, AutoReset runs on the component being removed. It is the only user code after the typed listeners and runs before the pool changes anything for the removal, on the hooked path (TakeOut) as on the plain one: when it throws, the component stays, its entity is still a member of the owning group and the component keeps its own change version. Up to 2.4.0 the hooked path ran it after the two steps below, so a throwing AutoReset left the entity out of its group although it still had every owned component, and the component with the version of the pool's last one. AutoReset only resets the value, so the order makes no other difference. Then, with hooks, group.OnRemoving(entityIndex) if owned, which moves the entity to the end of the member range in every owned pool while it still holds every owned component, and, if tracking, the version of the last dense entry is copied into the slot about to be freed (versions[_sparse[e]] = versions[_count - 1]), since that entry is the one the swap-remove will move there.
  3. Swap-remove: the last dense entry (_count - 1) is moved into the removed slot, and its owner's sparse entry is repointed. The vacated tail slot is set to default(T), not AutoReset: after the move, the tail is a bitwise duplicate of the live component that was moved, so running AutoReset on it would clear reference fields the live copy still uses.
  4. _sparse[entityIndex] = -1, _count--. With frames running, every frame of the entity is marked Done.
  5. World.OnComponentRemoved(entityIndex, TypeIndex): mask, count, filters, world listeners, auto-destroy.

The AutoReset branch tests the instance field _autoReset (the delegate bound at pool creation) for null. A static readonly bool would fold into a constant under the JIT, but IL2CPP turns each access into a class-init check plus a field load through the generic context.

SwapDense ​

SwapDense(int a, int b) exchanges two dense slots in full: _denseEntities, _denseData, _changedVersions when present, and the two sparse entries. It is internal and used only by owning groups. It is a no-op when a == b.

AutoReset and AutoCopy bridges ​

IAutoReset<T>.AutoReset(ref T) and IAutoCopy<T>.AutoCopy(ref T) are invoked through the interface on one boxed default(T) the pool constructor makes ((object)default(T) as IAutoReset<T>, null when T does not implement it). This is one boxing allocation per implemented bridge per pool, no allocation per call (the component is passed by ref), works for explicit interface implementations, and avoids runtime generic instantiation of a value-type bridge, which is not AOT-safe under IL2CPP. The call is plain IL, so Unity's managed code stripping, .NET trimming and NativeAOT see it and keep the implementations of every component type the code uses. Up to 2.4.0 the pool built closed delegates with Delegate.CreateDelegate over a method taken from typeof(T).GetInterfaceMap(...): no IL called the methods, stripping at Medium and High removed them (the interface map came back empty and the pool constructor threw). On CoreCLR the interface call costs what the delegate did: Remove with AutoReset measures the same within noise.

Growth ​

Sparse arrays grow to max(length * 2, entityIndex + 1) and are filled with -1; dense arrays (entities, data and versions when present) grow to max(length * 2, needed). Reserve sizes them exactly instead, for pools and filters alike: it states the need, and doubling would overshoot it. Starting sizes come from WorldConfig.InitialPoolSparseCapacity and InitialPoolDenseCapacity. A pool's sparse array only grows when a high-index entity actually receives the component, so a type that lives on few entities does not pay for the whole world.

Parallel arrays are allocated before any of them is replaced, both when a pool's dense arrays grow and when the world's slot arrays do (GrowEntityCapacity): an OutOfMemoryException leaves them all at the old size rather than some grown and some not. Growth moves the data to new arrays, so a ref or a RawData/RawEntities array taken before it no longer reaches the pool.

ComponentPoolBase vs ComponentPool<T> ​

ComponentPoolBase holds everything that does not depend on T: the sparse array, _denseEntities, _count, _ownerGroup, TypeIndex, ComponentType, Has, Remove (abstract), the introspection properties (SparseCapacity, DenseCapacity, ComponentSize, AllocatedBytes) and the internal GetDenseIndex, SwapDense, SetOwnerGroup, AddDefault, Clear, CopyTo, WriteComponents, ReadComponent. World, Group and WorldSerializer work through this base so that DestroyEntity, CopyEntity, Warmup, Clear, group swaps and snapshots can operate on pools without knowing T.

ComponentPool<T> is sealed so that the fast path in World.Pool<T>(), pools[typeIdx] is ComponentPool<T> pool, compiles to a single method-table comparison.

Clear, AddDefault, CopyTo, SwapDense, WriteComponents and ReadComponent are internal because each is one step of a larger operation that maintains the surrounding invariants:

  • Clear empties the pool without notifying World; only World.Clear calls it, and World.Clear resets masks, counts, filters and groups itself. Called on its own it would leave masks and filters pointing at components that no longer exist.
  • AddDefault exists for Warmup, which runs it on a temporary entity with events suppressed.
  • CopyTo is the per-pool step of CopyEntity, which allocates the destination slot first and dispatches a single OnEntityCreated after every pool has copied.
  • SwapDense permutes dense slots; only a group knows which permutation keeps the alignment invariant.
  • WriteComponents/ReadComponent are the per-pool steps of WorldSerializer.Save/Load, which own the stream format and the entity slots.

Remove stays public because it notifies World and is safe to call directly (that is how World.Remove<T>, OneFrameCleanup<T> (through RemoveAll, see built on the core) and CommandBuffer call it).

Component bitmasks ​

The mask is stored word-major: _componentMasks[word] is a ulong[] indexed by entity, and the world keeps _maskWordCount such arrays. Type index t lives in word t >> 6, bit t & 63. Two things grow it:

  • GrowEntityCapacity resizes every word array when a new slot is allocated past the current capacity. World.ReserveEntities(n) runs it ahead of time and sizes the free-slot list for n. Of the pools and filters it extends only the reserved ones (a pool given Reserve(k), a filter whose Inc types are all reserved or that has a Reserve of its own), whose sparse side then covers the n slots, also when they are reserved after it; every other pool and filter keeps its size and grows on demand (see World config). The obsolete World.Reserve(n) grew the sparse side of every pool and filter, those created later included: about 4 × n bytes per pool and 4 KB per 1024 indices per filter. The reserved sizes are recorded after everything was allocated, so a reserve that fails does not make later pools and filters try the same allocation.
  • EnsureMaskWords allocates a fresh ulong[entityCapacity] for each new word when a pool is created for a type in a new 64-type block, or when a filter is registered that constrains such a block. The second word also brings _usedWords (see the table above). Type indices are process-wide, so a world gets the second word, and the summary, as soon as it uses any type whose index is 64 or above, whatever the number of types it uses itself.

Word-major layout means a single-word filter test reads one array, and adding a new 64-type block never touches existing arrays. With about 1000 component types the mask is 16 words, i.e. 128 bytes per entity slot, plus 8 bytes of _usedWords.

Has<T> ​

World.Has<T>(entity) reads the mask only:

csharp
int typeIdx = ComponentType<T>.Index;
int word = typeIdx >> 6;
if (word >= _maskWordCount) {
    return false;
}
ulong[] maskWord = _componentMasks[word];
if ((uint)entity.Index >= (uint)maskWord.Length) {
    return false;
}
return (maskWord[entity.Index] & (1UL << (typeIdx & 63))) != 0;

It never creates a pool and never touches pool memory.

DestroyEntity and CopyEntity walk the mask ​

Both iterate only the set bits of the entity's words:

csharp
for (int w = 0; w < _maskWordCount; w++) {
    ulong mask = _componentMasks[w][idx];
    while (mask != 0) {
        int bit = TrailingZeroCount(mask);
        int typeIdx = (w << 6) | bit;
        _pools[typeIdx].Remove(idx);      // or .CopyTo(srcIdx, dstIdx)
        mask &= mask - 1;
    }
}

TrailingZeroCount is BitOperations.TrailingZeroCount on .NET 5+. Elsewhere (Unity, netstandard2.1) it is the population count of ~mask & (mask - 1), the zeros below the lowest set bit, written as the usual shift-and-mask sum: no lookup table, so World has no static constructor and IL2CPP adds no class-initialization check or static-field load to each bit, and clang compiles the expression to a single count-trailing-zeros (rbit + clz on ARM64; MSVC keeps the arithmetic, still without a memory load). The cost is proportional to the number of mask words plus the number of components on the entity, not to the number of registered types. GetComponentTypes uses the same walk. DrainComponents and CopyEntity read word 0 and then only the words whose bit is set in _usedWords[i] (re-read after every word whose components ran user code, as are the mask arrays and the word count), and DrainComponents stops after the last word that holds one of the entity's components (see world.DestroyEntity).

Filters ​

A Filter is a sparse set of entity indices with no payload, kept up to date by World on every structural change.

Constraints and precomputed masks ​

The builder produces three sorted, deduplicated int[] of type indices: IncludedTypeIndices, ExcludedTypeIndices, AnyTypeIndices. The constructor turns them into three ulong[] masks sized to the highest constrained type index, plus:

  • ActiveWords: the words in which any of the three masks has a bit, so matching skips words the filter does not constrain.
  • SingleWord / SingleIncludeMask / SingleExcludeMask / SingleAnyMask: when exactly one word is active, matching reads these scalar fields instead of walking arrays. SingleWord is -1 for multi-word filters.

A filter is single-word when all of its constrained types fall in the same 64-type block. With many component types this depends on first-touch registration order, not on how many types the filter names.

The all-ones Any mask trick ​

The single-word test is:

csharp
ulong entityWord = _componentMasks[w][entityIndex];
return (entityWord & include) == include
    && (entityWord & exclude) == 0
    && (entityWord & any) != 0;

When the filter has no Any constraint, SingleAnyMask is ulong.MaxValue. End() guarantees that a filter without Any has at least one Inc, so a word that passes the include test is non-zero, and (entityWord & ulong.MaxValue) != 0 is true. The Any test therefore costs one AND per match with no branch on HasAny. The multi-word path accumulates anyHit across ActiveWords and returns anyHit || !filter.HasAny.

Sparse set with a 1-based dense array and a slot-0 sentinel ​

_denseEntities[0]        = FreeSlot (-1), permanent terminator
_denseEntities[1.._count] = entity indices, no gaps
_denseEntities[_count+1..] = FreeSlot
sparse[entity]           = dense slot (1-based), or -1

Entities is new ReadOnlySpan<int>(_denseEntities, 1, _count). First()/Single() read slot 1.

The sparse side is paged: _sparsePages[entityIndex >> 10][entityIndex & 1023], with 1024-entry pages allocated on first touch and filled with -1. Iteration never reads the sparse side, so the extra indirection is not on the hot path, and a filter matching a few entities in a world with many slots pays only for the pages it touches instead of an int per entity slot. Contains(int) returns false for an index whose page was never allocated.

AddEntity returns early if the entity is already present. This matters because the world may test the same filter twice for one change (for example a type that is in a filter's Any list and a different type in its Inc list), and because UpdateFilterForEntity calls AddEntity without knowing the current membership.

RemoveEntity swap-removes: the entity in the last slot moves into the freed slot, the freed last slot is written with FreeSlot, the sparse entry becomes -1, _count--.

The reverse enumerator ​

Filter.Enumerator is a ref struct holding the filter, a Span<int> over _denseEntities (the array itself under IL2CPP, see below), _index and _current. It starts at _count + 1 and walks down:

csharp
public bool MoveNext() {
    int i = _index - 1;
    int entity = _entities[i];
    if (entity == FreeSlot) {
        Filter filter = _filter;
        _entities = filter._denseEntities;
        int count = filter._count;
        if (i > count) {
            i = count;
        }
        if (i == 0) {
            _index = 1;
            return false;
        }
        entity = _entities[i];
    }

    _index = i;
    _current = entity;
    return true;
}

The normal step is one span load, one compare against FreeSlot, two field writes. Reading FreeSlot means one of three things, all handled by the same slow path:

  1. The terminator at slot 0: normal end of iteration. _index is parked at 1 so extra MoveNext calls keep probing the terminator instead of reading out of range.
  2. The live range shrank below the cursor because several entities were removed in one loop step: re-read _count, clamp i to it, continue from there.
  3. The cached span points at a retired array (see growth below): re-cache and continue at the same slot, which holds the same entity in the new array.

Why the hot path is call-free: MoveNext makes no method calls on this and reads nothing from the heap except the element. Passing a struct by reference to a helper exposes its address, which prevents the JIT from promoting the enumerator's fields to registers; keeping the slow path inline avoids that. Using a Span<int> rather than the raw array keeps the length inside the promoted struct, so the bounds check on _entities[i] does not become a heap load of array.Length after a re-cache. Under IL2CPP the field is the raw int[] instead: the enumerator's Il2CppSetOption attributes drop the array's bounds check, which they cannot do for a Span indexer (an IL2CPP intrinsic that always checks), and the span also added a metadata-initialization check to every inlined MoveNext. The cursor never leaves the cached array: it starts at _count, below the array's length, only goes down, and is clamped to the count of the array it re-caches. No per-step version or count check is needed because the sentinel doubles as the end-of-range check.

Why reverse: swap-remove fills a freed slot with the entity from the highest live slot. Iterating from high to low, that entity has already been visited, so removing the current entity (or anything at or above the cursor) never causes a double visit. New entities are appended above the cursor and are not visited in the current loop.

Poisoning the retired array on growth ​

GrowDense allocates the bigger array, copies, fills the new tail with FreeSlot, then fills the old array entirely with FreeSlot:

csharp
_denseEntities = grown;
Array.Fill(old, FreeSlot, 0, old.Length);

An enumerator still holding a span over the old array lands on the FreeSlot path on its next step and re-caches the current array. Without poisoning it would keep reading stale entity indices from the retired array.

The per-type table and which lists are tested on add vs. remove ​

World._filtersByType is an array indexed by type index. Each entry is either null, for a type no filter mentions, or a TypeFilters object with three arrays: Include, Exclude, Any. RegisterFilter creates the entry on demand (TypeFiltersFor) and appends the filter to the array matching each constraint kind. Keeping the three lists in one object means a structural change looks up one entry and tests it for null once; for the many types no filter constrains, that single branch is the entire filter cost.

When the entry exists, UpdateFiltersOnAdd / UpdateFiltersOnRemove walk the three arrays:

EventInclude filters of tExclude filters of tAny filters of t
component t addedfull mask test, AddEntity on matchRemoveEntity without a test (an excluded type appeared, the entity cannot match)full mask test, AddEntity on match
component t removedRemoveEntity without a test (a required type is gone)full mask test, AddEntity on matchfull test, AddEntity or RemoveEntity (another Any type may still satisfy it)

Adding t can only move an entity into filters that require t and out of filters that exclude it; removing t only the reverse. Half of the updates therefore skip the mask test entirely. The lists are arrays, not List<Filter>, because this loop runs on every Add and Remove.

The untested half relies on t staying put, and a filter listener can break that: an OnEntityRemoved that adds t back during a removal, or an OnEntityAdded/OnEntityRemoved that removes t again (or destroys the entity, whose slot a new entity may then take) during an add. The nested call has already updated every filter for the entity, so the outer loop must not undo it. AddEntity and RemoveEntity return whether listeners ran; only then is the entity re-checked: on remove, the Include loop stops when t is back; on add, the update stops when the entity no longer has t or the slot's generation changed, before the Exclude filters would drop an entity that matches them. Without listeners the loops cost what they did before.

EntityMatchesFilter performs no bounds check against _maskWordCount: RegisterFilter calls EnsureMaskWords for every word the filter constrains, so the mask arrays it reads always exist, and an unregistered type's word simply reads zeros.

Registration, deduplication, PopulateFilter ​

FilterBuilder.End():

  1. Throws if there is neither an Inc nor an Any constraint.
  2. Throws if a type is in both Inc and Exc, or in both Any and Exc.
  3. Drops any Any type that is also in Inc (redundant: a required type always satisfies "at least one of").
  4. Sorts all three lists and calls World.RegisterFilter.

RegisterFilter compares the three sorted arrays against every registered filter and returns the existing instance on a match. Order of Inc<A>().Inc<B>() versus Inc<B>().Inc<A>() does not matter. Static specs (world.Filter<Inc<A, B>, Exc<C>>()) and generated Init code go through the same builder, so they deduplicate against builder-made filters. The comparison is linear in the number of registered filters, which is why filters belong in Init, not Run.

For a new filter, PopulateFilter first checks that every Inc type has a pool; if one does not, nothing can match yet and the scan is skipped. Otherwise it walks every slot below _nextIndex, tests alive entities against the filter and adds the matches. Filters are never unregistered; they live until World.Destroy.

Owning groups ​

A filter finds entities; reading their components still goes through each pool's sparse array. An owning group removes that indirection for a fixed set of component types by rearranging the pools themselves. world.Group<T1, T2>() (also three and four types) returns a Group<T1, T2> whose Data1, Data2 and Entities spans can be indexed in lockstep.

The alignment invariant ​

A group holds its owned pools (ComponentPoolBase[] _pools) and a member count _count. The invariant it maintains at all times:

In every owned pool, dense slots 0 .. _count-1 hold the components of exactly the entities that have all owned components, in the same entity order in every pool.

Members are packed at the front; entities that have some but not all of the owned types sit in the remaining slots _count .. pool.Count-1 in no particular order. From this:

  • Entities is _pools[0].RawEntities[0 .. _count), Data1 is _p1.RawData[0 .. Count), and so on. Data1[i], Data2[i] and Entities[i] belong to the same entity.
  • Iteration is a plain loop over spans: no sparse lookup, no mask test, one bounds check per span access.
  • Contains(e) (internal) is pools[0].Has(e) && pools[0].GetDenseIndex(e) < _count. Checking the first pool is enough because a member sits at the same slot in every pool.

Because the group permutes dense slots, the order of RawData/RawEntities in an owned pool is no longer insertion order: members come first. The two arrays stay parallel, and sparse entries are always repointed, so pool.Get(e) and filter loops are unaffected.

OnAdded: joining the group ​

AddHooked calls group.OnAdded(entityIndex) immediately after the pool has stored the new component, before filters, world listeners and pool listeners run:

  1. If any owned pool lacks the entity, return: not a member yet.
  2. If pools[0].GetDenseIndex(e) < _count, return: already a member.
  3. slot = _count++; for every owned pool, pool.SwapDense(pool.GetDenseIndex(e), slot).

Step 3 moves the entity's component into the first non-member slot of each pool and moves whatever non-member was there into the entity's old slot. AddHooked then re-reads the dense index from _sparse, so the ref it returns and hands to pool listeners points at the moved component.

OnRemoving: leaving the group ​

RemoveHooked calls group.OnRemoving(entityIndex) after pool listeners and AutoReset and before the swap-remove, while the entity still holds every owned component:

  1. If not Contains(e), return.
  2. last = --_count; for every owned pool, pool.SwapDense(pool.GetDenseIndex(e), last).

The entity now sits at slot last in every owned pool, which is the first non-member slot, and the member that used to be last has taken its place. The pool that is actually removing the component then swap-removes from slot last as usual: the pool's final element (a non-member, or the component itself) lands there, outside the member range. In the other owned pools the entity's components stay at slot last among the non-members, which is exactly where an entity that lacks one owned type belongs.

DestroyEntity drains components one pool at a time; the first owned pool's Remove ends the membership, the remaining owned pools see !Contains and do nothing.

Populate ​

RegisterGroup throws if any pool already has an owner or if a type is listed twice, sets _ownerGroup on every pool (which sets _addHooks and _removeHooks), adds the group to _groups and calls Populate(). Populate picks the pool with the fewest components and calls OnAdded for each of its dense entities, walking forward. A hit swaps the member to slot _count, which is at or below the cursor; the non-member displaced to the cursor's slot was already visited, so the forward walk still sees every entity exactly once. Cost: O(smallest pool count × P), P being the number of owned pools.

Interaction with filters, Clear, Warmup and snapshots ​

  • Filters store entity indices and never look at dense order, so a filter over grouped types keeps working, and destroying entities from inside a filter loop keeps the group exact (Group_FilterAndGroup_Coexist in the tests).
  • World.Clear sets every group's _count to 0 after emptying the pools.
  • Warmup adds a default of every registered type to its temporary entity, which makes it a member of every group whose types are all registered; destroying it removes it again, so no group keeps the dummy. If a listener destroys the temporary entity, Warmup checks its handle before every type and continues on a fresh one, never on an entity the listener created in the freed slot; a type a listener already added is skipped.
  • WorldSerializer.Load restores components through pool.Add, so groups that exist before Load fill normally.

Why a pool belongs to one group ​

The invariant fixes the dense position of every member in the pool. Two groups owning the same pool would each demand that the array be packed by their own membership, and both orders cannot hold at once. RegisterGroup therefore rejects a pool that already has an owner. world.Group<A, B>() returns the existing group only for the same types in the same order (OwnsExactly compares position by position); asking for Group<B, A>() afterwards throws, because the pools are owned.

Destroying members inside a group loop ​

Iterate downward. Destroying the member at index i swaps it with the last member (at an index >= i) and then swap-removes within the pool, which touches only slots >= i; slots below i are untouched, so the loop continues correctly. A forward loop would skip the member that was moved into slot i.

Cost per structural change ​

  • Add of an owned type: P Has checks; if the entity thereby gains all owned types, P SwapDense calls, each moving two ints, two T values, two version entries when tracked, and two sparse writes. Add of a type nobody owns: unchanged.
  • Remove of an owned type from a member: P swaps plus the ordinary swap-remove. From a non-member: one Contains.
  • Iteration: O(members), contiguous, no indirection.

Change tracking ​

Change tracking is opt-in per pool. TrackChanges() allocates _changedVersions, an int[] parallel to the dense arrays (resized with them in GrowDense), stamps every existing component with one fresh version so they all count as "changed now", and sets _addHooks and _removeHooks. TracksChanges reports the state.

Per-slot versions and the world-wide counter ​

World._changeVersion is a counter: NextChangeVersion() returns the next value (see counter wrap for what happens after int.MaxValue), and World.ChangeVersion reads the current value. Three operations take the next value and write it at the component's dense slot:

  • Add on a tracking pool (inside AddHooked, before the group swap so the value travels with the component).
  • Modify(entityIndex), which stamps and returns the ref.
  • MarkChanged(entityIndex), which stamps without returning anything.

ChangedVersion(entityIndex) reads the entry and throws InvalidOperationException if the pool does not track. ChangedSince(entityIndex, version) is ChangedVersion(entityIndex) > version until the counter first wraps, and a comparison by age after that.

The consumer pattern is to store world.ChangeVersion at the end of a system's Run and pass it to ChangedSince the next time. Because every write takes a distinct, increasing number, a bookmark taken at any point in a frame separates "written before" from "written after" exactly, whether the producer runs earlier or later in the pipeline than the consumer. A per-tick stamp could not tell a write made earlier in the same frame (already handled) from one made later (not yet handled). World.Clear resets the tick but not the change counter, so bookmarks stay meaningful across it.

Versions follow the component ​

Versions are stored per dense slot, so every operation that moves components moves versions with them:

  • Remove: RemoveHooked copies versions[_count - 1] (the entry of the last component, which the swap-remove is about to move into the freed slot) into the freed slot's entry. The removed component's own version is discarded with it; the stale value left at the tail is overwritten by the next Add.
  • SwapDense: swaps the two version entries along with the data, so a group joining or leaving keeps each component's version (Group_ChangeTracking_FollowsSwaps in the tests).
  • GrowDense: resizes the version array with the dense arrays.

Counter wrap ​

The counter advances with every Add, Modify and MarkChanged on a tracking pool, so a game that marks a few thousand components per frame passes int.MaxValue after hours of play. When the increment overflows, the counter starts again at 1 (0 and below never name a write) and the world sets _changeVersionWrapped; every pool copies that flag into its own _changeCounterWrapped when the wrap happens and when it starts tracking, so ChangedSince reads a field of the pool, not of the world.

Until the first wrap nothing changes: ChangedSince is the plain changed > version. After it, ChangedSince compares ages (World.ChangedAfterWrap): the age of a version is the number of writes between it and the current counter value, current - version, plus int.MaxValue when that is negative (the ring 1..int.MaxValue). A component changed since a bookmark when its version is younger than the bookmark; a bookmark of 0 or below, the value of a world that tracked no write yet, means "everything changed since".

Ages only order versions that are less than one turn of the counter old. A component left alone for a whole turn would look new again, so the stored versions are aged: at the wrap and then every ChangeVersionAgingPeriod (2^29) writes, NextChangeVersion calls AgeChangeVersions, which walks every tracking pool and gives each version older than ChangeVersionAgeLimit (2^30) writes exactly that age. Before the first wrap those calls (one test of the low bits per write, a call every 2^29 writes) age nothing. Modify and MarkChanged may run in a ParallelRunner job, and the counter is incremented without synchronization: workers can hand out the same version, and a worker that read the counter late can write it back below versions other workers already stored. Three things keep such writes from being lost after the first wrap. A stored version whose age comes out larger than any stored version can have (ChangeVersionLeadLimit: the age limit plus an aging period plus 2^28) is ahead of the counter, and StoredChangeVersionAge counts it as age 0, the newest, for ChangedSince and for the aging. AgeChangeVersions replaces a version with Interlocked.CompareExchange, only if it is still the old one it read, so a version a worker has just written is never overwritten. The first wrap (FirstWrap) is aged by one worker, chosen by a compare-exchange on _changeVersionWrapGate, by the plain difference to int.MaxValue (the versions of the old turn may be of any age then); the others spin until it sets _changeVersionWrapped before they store a version of the new turn, which that pass would take for ancient, and the aging worker sets the flag also when its pass throws, so the others go on. What remains is harmless: a job's writes may share a version, and may show as changed against a bookmark taken after the job; no write is lost. The consequences for a caller, also stated on World.ChangeVersion:

  • After the wrap, a bookmark is exact for the next 2^30 (about a billion) counted writes. Against an older bookmark, a component written before it can be reported as changed (its version was aged to 2^30); a bookmark older than int.MaxValue writes means nothing.
  • Versions must be compared with ChangedSince. A hand-written pool.ChangedVersion(e) > bookmark keeps working until the wrap and breaks after it.

Up to 2.4.0 the counter went negative at the wrap, so ChangedSince reported nothing as changed against a bookmark taken before it and old components as changed against one taken after it.

Why Get stays untracked ​

Get is the read path and by far the most frequent pool operation. Tracking it would put a counter increment and an array store on every read, and, since a ref return cannot tell a read from a write, would mark every read as a change and make ChangedSince meaningless. Writes therefore opt in: Modify for read-modify-write, MarkChanged after a write done some other way. Writes through Get, RawData, group Data spans or the inspector's SetRaw are invisible to tracking unless followed by MarkChanged. CommandBuffer.Set on an existing component and a CopyEntity that overwrites a component write through Modify, so they count as changes.

Cost: 4 bytes per dense slot on tracking pools; one store plus one counter increment per Add, Modify and MarkChanged; two reads and a test of the pool's wrap flag per ChangedSince (an age comparison once the counter has wrapped). Pools that do not track pay nothing beyond the shared _addHooks / _removeHooks branch.

Structural change flow ​

world.Add<T>(entity, value) ​

  1. Under KENSEI_DEBUG, ValidateHandle: the handle must be alive, or be the entity whose DestroyEntity is running (see the debug section).
  2. Pool<T>(): fast path type check on _pools[typeIdx]; on first use CreatePool<T> grows _pools and the mask words to cover the type index and allocates the pool.
  3. pool.Add(entity.Index, value):
    • Under KENSEI_DEBUG, the slot must be alive or dying.
    • Throws InvalidOperationException if the component is already present.
    • Grows sparse/dense arrays as needed, stores the value, _count++.
    • If _addHooks: list ownership check, version stamp, group OnAdded (may swap the component to the front), then the steps below, then IComponentListener<T>.OnAdded.
    • World.OnComponentAdded: _componentCounts[e]++, sets the mask bit (and the word's bit in _usedWords[e] when the word was zero and is not word 0), looks up _filtersByType[t]; if it exists, Include (test, add), Exclude (remove), Any (test, add), each AddEntity/RemoveEntity firing IFilterListener callbacks synchronously; after a filter whose listeners ran, the update stops if the entity lost t or the slot was reused. With world listeners the Add is their notification from the start (BeginNotify, before the filters), then every IWorldEventListener.OnComponentAdded(e, t) while the entity still has t (NotifyWorld): a filter listener that took t away told them nothing, and they hear of neither (see notification frames).
    • Returns ref _denseData[dense], or the scratch value when the component is gone or the slot holds another entity.

CreateEntity<T>(T) is CreateEntityInternal() (allocate a live, empty slot), then this Add, then OnEntityCreated to world listeners. CopyEntity is CreateEntityInternal(), then CopyTo per set mask bit of the source (each of which goes through pool.Add and the flow above), then a single OnEntityCreated. With world listeners both begin the creation as a notification of its own (a LifecycleKey frame) before the first component is added, so an entity that a listener of one of its components destroys while it is built gets neither OnEntityCreated nor OnEntityDestroyed.

world.Remove<T>(entity) ​

  1. Under KENSEI_DEBUG, ValidateHandle.
  2. If no pool exists for T, return. Remove never creates a pool.
  3. pool.Remove(entity.Index):
    • Return if the component is absent.
    • If _removeHooks: IComponentListener<T>.OnRemoved with the data intact.
    • AutoReset on the removed component, if implemented.
    • If _removeHooks: group OnRemoving (member moved to the end of the member range in every owned pool), version copy for the slot about to be freed.
    • Swap-remove, tail defaulted, sparse cleared, _count--.
    • World.OnComponentRemoved: decrements _componentCounts[e] (only if the entity is alive; see the drain loop below), clears the mask bit, looks up _filtersByType[t]; if it exists, Include (remove), Exclude (test, add), Any (test, add or remove). For an entity whose DestroyEntity is draining it, only Include and Any (remove): its components not removed yet are still in the mask and would let it into Exclude filters of t. With world listeners the removal is their notification from the start (BeginNotify reads the slot's generation before the filters), then OnComponentRemoved(e, t) to them while the entity is still alive and still lacks t: a filter listener that added t back told them nothing, and they hear of neither. A listener that destroys the entity and creates another that takes the slot gets the slot it would get without listeners; before CreateEntity hands it out, the world listeners not told of the removal yet are told (see notification frames below).
    • Auto-destroy: if _componentCounts[e] == 0 and the entity is alive, DestroyEntityInternal(e) runs before Remove returns.

_dispatch is a single bool kept equal to _eventListeners.Length > 0 && !_suppressEvents; AddEventListener, RemoveEventListener and SuppressEvents recompute it, so the hot path tests one field instead of two.

world.DestroyEntity(entity) ​

csharp
public void DestroyEntity(Entity entity) {
    if (!IsAlive(entity)) {
        return;
    }
    DestroyEntityInternal(entity.Index);
}

DestroyEntityInternal(idx):

  1. _alive[idx] = false before anything else. A listener that calls DestroyEntity on the same entity re-entrantly hits the IsAlive check and becomes a no-op; a listener that calls Remove on it does not decrement the count (see step 3). Under KENSEI_DEBUG the slot is pushed onto a stack of dying slots, which handle validation accepts.

  2. OnEntityDestroyed(idx) to world listeners. The entity is dead (IsAlive false) but its components are still readable.

  3. DrainComponents(idx), in a finally so it runs even if a listener threw:

    csharp
    do {
        int remaining = _componentCounts[idx];     // never below the set bits
        _componentCounts[idx] = 0;
        if (_orphans) {
            remaining = int.MaxValue;          // an orphan's removal broke that: read every word
        } else if (remaining == 0) {
            return;                            // auto-destroy: no bit is set
        }
        var masks = _componentMasks;
        int words = _maskWordCount;
        for (int w = 0; w < words; w = NextUsedWord(idx, w)) {
            ulong mask = masks[w][idx];
            if (mask == 0) {
                continue;                      // a word emptied by Remove keeps its bit
            }
            do {
                if (_clears != clears) {
                    return;                    // a listener called World.Clear
                }
                int bit = TrailingZeroCount(mask);
                _pools[(w << 6) | bit].Remove(idx);
                mask &= mask - 1;
                remaining--;
            } while (mask != 0);
            if (remaining <= 0 && _componentCounts[idx] == 0) {
                return;                        // every bit met, nothing added
            }
            masks = _componentMasks;           // a listener may have added a mask word
            words = _maskWordCount;
        }
    } while (_componentCounts[idx] != 0 && _clears == clears);

    Each Remove runs the full pool flow (pool listeners, AutoReset, group swap, filter updates, world OnComponentRemoved). Listeners may re-add components to the dying entity. Such a component goes through the normal Add flow, so the dying entity enters the filters it matches together with the components not removed yet (filter listeners get OnEntityAdded for an entity whose IsAlive is false) and leaves them again when the next pass removes it. OnComponentAdded increments _componentCounts unconditionally, while OnComponentRemoved skips the decrement for a dead entity, so after a pass a non-zero count means "something was re-added" and the loop runs again, reading one int instead of rescanning every mask word. The mask is not cleared ahead of the removals: each Remove clears its own bit, so the listeners of one removal still see the components not removed yet (world.Has agrees with the pools, as in a plain Remove), and a component re-added during the pass sets its bit again for the next pass. Up to 2.2.1 each mask word was zeroed before its bits were iterated, so Has said false for components still in their pools, and components in later words could let the dying entity into Exclude filters. A pass stops early once it has met as many set bits as the count it started from while the count is still 0 (nothing was added): the count is never below the number of set bits (equal while the entity is alive, an Add raises both, a removal from the dying entity lowers only the bits), so the bits met are every bit there was, and the words after are empty. An auto-destroy (count 0) reads no word at all; a component that a listener removed before the pass reached it, or one added during the pass, makes the pass read every word as before. The one case where the count can fall below the bits is an orphan (see exception safety): an interrupted destroy leaves components in their pools without mask bits, and removing one later lowers the count of the entity that took the slot. The world remembers that it released a slot with bits still set (_orphans), and from then until World.Clear every pass reads every word without stopping early. After word 0 the pass reads only the words whose bit is set in _usedWords[idx], a clear bit meaning a zero word. The mask arrays, the word count and the summary are read once and again only after a word whose bits were removed, since only a removal runs code that can add a mask word or a component in a new word: an empty word costs the load of its array and of the slot's bits in it. The loop is bounded by _maskWordCount, not by the length of the outer array, which an EnsureMaskWords that ran out of memory may have left longer. A listener that calls World.Clear changes _clears: the drain stops before the next removal, because the slot has been reset and may already hold an entity the listener created. Under KENSEI_DEBUG more than 1000 passes throws.

  4. ReleaseSlot(idx), in an inner finally: count and mask words zeroed after an interrupted drain (a completed one leaves them zero, which is what lets CreateEntityInternal take a free slot's count without writing it), and when that drain left bits set, the slot taken out of every filter and owning group (LeaveFiltersAndGroups); the slot's _usedWords cleared, _aliveCount--, _releases++, and the slot pushed onto the free stack (growing the stack is a separate, non-inlined call), except a slot whose drain left orphans, which is kept off the free stack until Clear (KeepSlot, see exception safety). Skipped after a World.Clear from a listener, which has already released every slot. Under KENSEI_DEBUG the debug name is dropped and the slot leaves the stack of dying slots it joined in step 1.

Warmup uses the same machinery on a temporary entity with events suppressed, so world listeners and the profiler never see it.

Event ordering and exception safety ​

Ordering follows directly from the flows above:

  • On Add: version stamp and group swap (invisible to listeners), then filter listeners (per filter, in table order), then IWorldEventListener.OnComponentAdded, then IComponentListener<T>.OnAdded. For CreateEntity/CopyEntity, OnEntityCreated comes last, after every component is in place, so listeners never observe an entity without components; it does not come at all when a listener of one of the components destroyed the entity (CopyEntity stops copying then, and when the source is destroyed; it checks both handles whenever _releases changed during a copied component). A listener that removes the component again ends the Add for everyone after it: a filter listener's removal tells world listeners nothing (with world listeners the Add is their notification from its start, before the filters are updated, and none of them has heard of it yet; see the notification frames below), and typed listeners after the removal get no OnAdded. A filter listener that removes the component and adds it back right away tells world listeners of neither, and the outer Add, whose change holds again, sends one OnComponentAdded; typed listeners get one OnAdded, from the nested Add. Up to 2.4.0 world listeners got the OnComponentRemoved alone after a removal, and the removal and then two additions after a removal and a re-add (as documented in 2.2.1 and 2.2.2).
  • On Remove: IComponentListener<T>.OnRemoved (data intact, entity still a group member), then AutoReset, then the group swap and version copy, then the swap-remove, then filter listeners, then IWorldEventListener.OnComponentRemoved with the entity still alive, then auto-destroy if it was the last component. A filter listener that adds the component back tells world listeners of neither the removal nor the addition (up to 2.4.0 they got the OnComponentAdded alone). One that destroys the entity and creates one in its slot (a replacement) does not change the slot the new entity gets: the world listeners not told of the removal yet hear it before the new entity's events.
  • On DestroyEntity: OnEntityDestroyed first, with the dead flag set and components still readable; then one Remove flow per component.
  • Notification frames. A listener may undo the change it is told about, directly or from a callback further in (a listener of another entity's change, a filter listener under a world notification). Every listener has to hear an entity's changes alternately, and the last call it gets has to match the world. Filter and World each keep a stack of the notifications in progress (NotifyFrame, innermost last, re-indexed after every callback since a nested notification may grow it). A filter's frame holds the slot, Told (the position of the listener being told) and the listener arrays below; the world's also holds the slot's generation, the key (the component type, or LifecycleKey for OnEntityCreated and OnEntityDestroyed), the Kind (added, removed, created, destroyed), Told (-1 before the first listener) and Last, the position of the last listener to be told (-2 until ResolveNotify has chosen them). A notification of the same entity nested in an open one (a filter: the same slot; the world: the same slot, generation and key) goes only to the listeners that heard of the change it undoes: positions 0 to the open frame's Told, in the same order. The outer notification goes on only while its change holds, checked before every listener but the first (the world checks before the first too when user code ran since the change): a filter checks that the entity is still in it (or still out), the world (ChangeHolds) that the slot still has the frame's generation and that the component's mask bit is set (or clear) or the slot alive (or dead). Once the change is undone it stops, so the listeners after the one that undid it heard of neither. A notification of another entity, or of another key, goes to every listener. Fast path: with one listener and no open frame of the entity (for a world Add or Remove, also no filter listener of the type), no frame is pushed: a nested notification would be limited to that listener, which is every listener, and there is no listener after it to stop for. Up to 2.4.0 only typed listeners stopped: a second filter listener got OnEntityRemoved and then OnEntityAdded for an entity the first had taken out of the filter again, and the same for world listeners.
  • Changes count from the start. With world listeners, Add and Remove begin their frame before the filters are updated, and CreateEntity and CopyEntity begin a LifecycleKey frame of kind created before the first component is added (Told -1): a filter listener, or a listener of the new entity's component, that undoes them tells world listeners nothing. A destroy nested in the creation is a lifecycle notification nested in it, so it goes only to the listeners that heard OnEntityCreated: none before it is sent. OnEntityCreated itself goes to the listeners subscribed when it is sent (also one that a listener of the components subscribed while they were added), and a listener that is to hear it but has not yet, or that subscribed before it was sent, is not told of a destroy that undoes it; one subscribed while it is sent is. Without world listeners when the creation began, no frame is pushed: CreateEntity and CopyEntity record the entity in _unannounced until its OnEntityCreated (the innermost creation; the ones it is nested in, when a listener of a component of an entity being made creates another, are pushed to _unannouncedOuter, allocated only then), and NotifyEntityDestroyed skips every such entity (Unannounced), so a world listener that a listener of its components subscribed meanwhile does not hear the destroy of an entity whose creation it never heard of. This holds for every creation still in progress, the outer ones of nested creations included. To such a listener the entity's lifecycle is absent until OnEntityCreated, while its components follow the general rule: it hears the component changes that happen after it subscribed. Each creation saves the innermost entry and the depth when it begins and puts them back when it ends, also through its handler (since 2.5.1; the handler is in CreateEntityObserved and CopyEntityObserved only, not in the CreateEntity of a world without listeners), so an entry is there exactly while its creation runs: a NextTick or a SystemsRunner.Run that a listener calls inside a creation keeps it. An exception that leaves a creation before OnEntityCreated, with or without world listeners, records the entity in a list of its own (_cut, CutCreation): it gets no OnEntityDestroyed either, until NextTick outside a runner or the end of the outermost SystemsRunner.Run forgets that list (ForgetCut). Clear and WorldSerializer.Load end the work in progress (AbandonWorkInProgress), as the restored generations may give an entity the handle of one that work is about: every frame gets generation 0 (no entity has it: it matches no notification and its change never holds; TellWorld reads the generation from the frame) and Last -1 (no listener left to tell), the entries are emptied (DropUnannounced; the depth is kept, which every creation still running restores when it ends), and _clears is raised, which stops a DestroyEntity and a CopyEntity in progress. A creation that finds its own entry empty when it ends announces nothing and puts back none of the entries it saved.
  • Replacements. Listeners never change which slot or handle an entity gets. A listener may destroy the entity whose component's removal is being told and create another, which takes the freed slot (the top of the free stack). With world listeners CreateEntity takes the slot through CreateEntityNotified, and CopyEntity calls FinishRemovalsOfNextSlot before it checks its source (a listener told there may destroy the source; CopyEntity then returns Entity.Null, or throws under KENSEI_DEBUG, as for any dead source, instead of leaving an entity without components) and makes the copy in CopyEntityNotifying: while the slot it is about to hand out has an open removal frame of the old entity with listeners left (RemovalOpen: kind removed, same generation, Last not reached), FinishRemovalsOfSlot tells those listeners OnComponentRemoved first, innermost frame first, advancing each frame's Told. Then the slot goes to the new entity, whose events go to every listener, and the outer removal loops find nothing left: NotifyWorld skips the positions up to Told, and ChangeHolds fails once the slot holds the new generation. A removal that FinishRemovalsOfSlot reached before its own loop began (its filter update was still running) goes on after the listeners already told. A filter tells entities by slot, as it holds them, so to a filter's frame a new entity in the slot is the same entity. Up to 2.4.0 such a replacement cut the removal short: the listeners after the one that made it never heard the removal, and then heard the new entity's component come.
  • Warmup, Clear and WorldSerializer.Load fire no world events and no profiler records: Warmup and Load call SuppressEvents(true), which clears _dispatch and is honoured by EcsProfiler; Clear never calls Remove at all, it resets pools, filters and groups directly. Filter listeners and pool listeners are not suppressed: they hang off Filter.AddEntity/RemoveEntity and AddHooked/RemoveHooked, which Load and Warmup do exercise.

Listener lists (World, Filter, ComponentPool<T>) are copy-on-write arrays. A dispatch loops over the array it captured at its start, so a listener that adds or removes listeners mid-dispatch neither shifts the loop nor gets skipped or invoked twice. A listener hears the changes that happen after it subscribed. AddListener/RemoveListener (AddEventListener/RemoveEventListener) store the array of the moment in every open frame that has none yet (NotifyFrame.Base: the listeners when that change happened). A notification nested in a frame whose listeners changed goes to Join: of the listeners the outer one tells (Listeners, or Base) those up to its Told that are still subscribed, in their order, then those of the array when the nested change happened that were not in the outer frame's Base (they subscribed after the outer change, while the entity was as it left it, so the nested change is news to them), are still subscribed and have not heard of the outer change. A listener subscribed after a change happened is not told of it: a world listener that a filter listener subscribes while the filters are updated hears the changes after it, not the one in progress (up to 2.4.0 it got that change, and could hear it twice). A listener unsubscribed during a dispatch is still called by that dispatch, which goes over the listeners it chose, but is in no nested one. Join allocates, only after a listener subscribed or unsubscribed during a notification. Listeners are compared by reference, so an object unsubscribed and subscribed again during one notification counts as having stayed subscribed (a known limitation). Each dispatch of a structural change starts when the ones before it in the order above are done, and goes to the listeners that were subscribed when the change happened and still are when it starts: the OnAdded dispatch of an Add to the typed listeners subscribed when the Add started, less those the filter and world listeners unsubscribed (see Add), and the world listeners of an Add or a Remove are the ones subscribed when it happened, before the filter listeners ran.

Exception safety:

  • A world listener or AutoReset that throws during DestroyEntity propagates after the finally chain has drained what it could and released the slot. Components whose Remove did not run stay orphaned in their pools (the pool's sparse entry was never cleared for them), while the slot's mask and count are reset, and ReleaseSlot takes the slot out of every filter it is still in and every owning group (LeaveFiltersAndGroups, without notifying anyone) and keeps it off the free list until Clear (KeepSlot, _keptSlots): a new entity in it would meet the orphans (an Add of one of their types throws "already has", and a pool's Has, which an owning group checks, would see them as the new entity's). A snapshot stores the kept slots, and the loaded world keeps them off its free list too. Up to 2.4.0 the slot stayed in the filters of those components, and an owning group kept counting it as a member. An orphan belongs to no entity: WorldSerializer.Load refuses a world whose pools hold one. Treat the exception as fatal for that world or Clear() it. Before the finally chain existed, the slot was never released at all.
  • A filter or world listener that throws in the middle of an Add or Remove (outside a DestroyEntity drain, above) runs after the pool, the mask and the count hold the change, but possibly before every filter of the type was updated or before the auto-destroy of an entity that lost its last component. The dispatch that catches the exception (Filter.Notify, the world's OnComponentRemoved dispatch) calls World.SettleAfterListenerThrew before rethrowing: every filter of the world is set to the slot's state without notifying its listeners (Filter.SetMemberQuietly; a live entity ends up in exactly the filters it matches, a dying one only leaves those it no longer matches), and a live entity without components is destroyed, which reports OnEntityDestroyed (a world listener that throws there too is not reported: the slot is released in a finally, and the first exception stays the one on its way out). Then the exception leaves Add, Remove or DestroyEntity; the listeners not called yet are not called for that change, typed OnAdded listeners included. No handler sits in the Add and Remove paths themselves: the cost is one pass over every filter of the world, paid only when a listener throws. Under KENSEI_DEBUG the removal-during-iteration check of a filter ends the same way: Filter.RemoveEntity decides before its swap-remove whether the removal breaks a running loop, makes the removal, then ReportRemovalDuringIteration settles the entity and throws, so the report leaves filters that agree with the pools (SetMemberQuietly does not check again while it settles). Up to 2.4.0 the filters the update had not reached kept their old membership (an entity without the component stayed in an Inc filter) and an entity whose last component was removed stayed alive without components.
  • A pool listener or AutoReset that throws during Remove runs before anything is modified; the component stays, in its group and with its change version. Hook frames are popped in finally, so a throwing listener leaves no re-entrancy state behind.
  • SystemsRunner.Init records progress in _initProgress; if a system's Init throws, the runner walks _initSystems[0.._initProgress) in reverse and destroys the systems there that implement IDestroySystem (a nested runner that completed its Init is walked system by system by the same rule; a system with only IDestroySystem is not in _initSystems, so no rollback reaches it), resets the progress and stays uninitialized, so the next Init starts over. Exceptions from those Destroy calls are collected into one AggregateException after the original one, marked in Exception.Data ("KenseiECS.SystemsRunner.InitFailure"), so that a parent whose own rollback throws as well merges its inner exceptions into its list instead of nesting it. Run cleans OneFrame components also when a system threw, so a throwing system cannot leave events to be processed twice. When the cleanup of one type throws as well (a listener of its components, AutoReset), the types after it are still cleaned (CleanOneFramesAfterFailure, which drops their exceptions), and the first exception is the one Run reports: the system's when a system threw, otherwise the one of the first cleanup that failed. Up to 2.4.0 the cleanup's exception replaced the system's and the types after it were not cleaned. Destroy resets the initialized flag before calling IDestroySystem.Destroy in reverse order.
  • SystemsRunner.Destroy walks _destroySystems in reverse and nested runners system by system, collecting the exceptions: every system is destroyed, then one exception is rethrown as is, several as one flat AggregateException in the order they were thrown (up to 2.4.0 Destroy stopped at the first throwing system).
  • CopyEntity destroys the copy when an AutoCopy or a listener of a copied component throws, before the exception leaves (the caller never gets the handle); the copy is destroyed inside its own creation, so world listeners hear nothing of it.
  • CommandBuffer.Playback clears the buffer in a finally; a throwing command discards the rest. Commands that a listener records into the buffer during Playback are applied by the same Playback, after the ones recorded before; the table of resolved pending entities grows when they create more entities than it was sized for (up to 2.4.0 Playback threw IndexOutOfRangeException once they went past its size, 16 at first, and dropped the rest).
  • WorldSerializer.Load restores SuppressEvents(false) in a finally; a malformed stream leaves a partially restored world, which the caller should Clear().

Snapshots ​

WorldSerializer (Core/Serialization) writes every live entity that has components, and those components, to a Stream and restores them into an empty world. Entities keep their index and generation, so Entity values stored in components, or held by the caller, are valid in the restored world; component types are identified by name, not by their runtime index.

File layout (version 2) ​

Integers are written by BinaryWriter (little-endian); strings are BinaryWriter strings (length-prefixed UTF-8).

uint32   magic          0x5343454B, the bytes "KECS"
int32    version        2
int32    tick           world.Tick
int32    entityCount    number of live entities with components
  entityCount × { int32 index; int32 generation; }        in ascending index order
int32    slotCount      the high-water mark (_nextIndex)                  version 2 only
int32    freeCount      number of free slots below slotCount              version 2 only
  freeCount × { int32 index; int32 generation; }          free-slot list order, bottom to top
int32    keptCount      slots kept off the free list until Clear          version 2 only
  keptCount × { int32 index; int32 generation; }
int32    raisedCount    slots at or above slotCount with generation ≠ 1   version 2 only
  raisedCount × { int32 index; int32 generation; }        in ascending index order
int32    poolCount      number of pools with a component of a saved entity
  poolCount × {
    string  assembly-qualified component type name
    int32   count
    count × { int32 entityIndex; payload }               in the pool's dense order, saved entities only
  }

Below the high-water mark every slot without an entity is a free slot. They are written in the order of the world's free-slot list, bottom to top (World.FreeSlotAt; CreateEntity takes the top one first), so the loaded world reuses them in the same order: the entities created after Load get the slots, and the handles, they would have got in the saved world. Save writes the live entities that have components, not EntityCount: an entity whose DestroyEntity is running (Save called by one of its listeners) is still counted there, and an entity inside the removal of its last component (Save called by a listener of that removal) is alive without components until the auto-destroy that follows. Neither is saved, nor are its components (ComponentPoolBase.SavedCount and WriteComponents take only components whose entity holds them, HoldsComponent); its slot is not on the free list yet and is written after it, on top, where its destroy will put it. Up to 2.4.0 Save called from a DestroyEntity listener wrote the dying entity, a stream Load rejected. The slots kept off the free list until Clear (an interrupted DestroyEntity left orphans there) follow as keptCount; the orphans themselves are not written (up to 2.4.0 they were, and Load put them on a free slot). Above the mark only the slots whose generation is not that of a new world (1), which a Clear raises, are written. A slot keeps its generation when it is freed and the next entity in it takes the next one (see Generation changes on reuse), so with these generations a handle that was dead when the world was saved stays dead after Load: the next entity in its slot gets the generation it would have got in the saved world.

Version 1 (2.4.0 and earlier) has no slotCount, no free, kept or raised slots; Load still reads it, and then the slots without an entity start from the generation of a new world, so a handle that was dead at Save can name the next entity created in its slot. 2.4.0 and earlier refuse a version 2 snapshot ("Unsupported snapshot version 2").

payload is either what the registered IComponentFormatter<T>.Write produced, or, for a component type with no formatter, sizeof(T) raw bytes taken with MemoryMarshal.AsBytes over the dense element. Pools with no components are skipped. Save iterates AliveEntities (linear in the high-water mark) and ActivePools (linear in registered types), then each pool's dense array.

Blittable path vs formatter path ​

ComponentPool<T> decides per type: IsBlittable = !RuntimeHelpers.IsReferenceOrContainsReferences<T>(). A registered formatter is always used when present, blittable or not. Without a formatter, a blittable type is written and read bit-for-bit, and a type that contains references throws InvalidOperationException from WriteComponents/ReadComponent. The raw form is the in-memory layout of the struct, padding included, so a snapshot written on one platform is readable only where T has the same layout and endianness. Entity is two ints and therefore round-trips raw. On read the pool fills a default(T) through a Span<byte> in a loop until sizeof(T) bytes arrived, throwing EndOfStreamException if the stream ends first.

RestoreEntity and FinishRestore ​

Load refuses a world with EntityCount != 0, and one whose pools still hold components (orphans of a DestroyEntity that a throwing listener or AutoReset interrupted: without entities a component in a pool belongs to no entity, and the entity restored into its slot would find it in the pool but not in its mask), with InvalidOperationException (call Clear() first). Up to 2.4.0 it did not check, and a stored component that met an orphan in its pool threw "already has component" halfway through the restore. Then it validates magic and version, reads the tick and the entity count, notes this world's own high-water mark (ownMark), and calls SuppressEvents(true) for the rest of the restore:

  1. For each stored entity, World.RestoreEntity(index, generation): grows the slot arrays if needed, throws if the slot is already alive (a corrupt stream restoring one slot twice), stamps the generation verbatim, marks the slot alive with zero components, and raises _nextIndex past it. The serializer also notes the generation it restored into each slot (_restoredGenerations, kept between calls so a repeated Load does not allocate it again; a Load takes the array out of the serializer while it runs and puts it back at the end, so a Load that a listener runs meanwhile with the same serializer, into another world, uses an array of its own and the outer one keeps checking its components against its own generations).
  2. Version 2: reads the saved high-water mark (savedMark), then the free slots and the raised slots, calling World.RestoreFreeSlot(index, generation, savedFree, savedMark, ownMark) for each. A count out of range, a free or kept slot listed twice or at or past savedMark, and a raised slot below it throw InvalidDataException. Kept slots get their generation the same way and are marked like the free ones. RestoreFreeSlot grows the slot arrays if needed, throws if the slot holds a restored entity, and stamps a generation without making the slot alive. The world loaded into may have a past of its own in the slot (a rollback: Clear, then Load into the same world), and a view or a system may still hold the handle of an entity that lived there after the snapshot was taken. So of the generation the next entity in the slot would get in the saved world (the stored one + 1 for a free slot, skipping 0; the stored one itself for a raised slot) and the one it would get in this world (its generation + 1 below ownMark; above it, its generation when a Clear raised it, not 1), the later is kept (LaterGeneration: generations count past int.MaxValue into the negative numbers, so "later" is the sign of the difference). Below the new high-water mark the slot is free, and one less is stored, since its next entity adds one. A slot this world never used takes the saved generation as it is.
  3. World.FinishRestore(tick, savedMark, freeList, freeCount, listed, kept, keptCount): raises _nextIndex to savedMark (0 for version 1, which leaves it where step 1 put it) and rebuilds the free stack. At the bottom go the slots below _nextIndex without an entity that the snapshot does not list (this world's own free slots above savedMark; for version 1 every free slot), pushed from the highest index down, so that the lowest of them is taken first; on top of them the saved free list in its order. The kept slots of the snapshot go to _keptSlots and stay off the stack until Clear; this world's own kept slots are dropped (they hold no orphans, which Load refused) and are free like its other slots. The entities created after Load take the saved world's free slots in the order it would have taken them, then the other free slots in ascending order, as the saved world would take new slots. It also sets _tick. Only these three methods write generations and _nextIndex directly; they are internal and valid only on an empty world.
  4. For each stored pool: resolve the type name to a ComponentPoolBase (ResolvePool), then for each entry read the entity index and call pool.ReadComponent with the generation noted for that slot in step 1. It decodes the value and then:
    • if the entity restored into the slot is no longer alive (a pool or filter listener of a component restored before destroyed it, and may have created an entity of its own in the slot), the value is dropped, as CreateEntity and CopyEntity drop the rest of an entity a listener destroyed. Up to 2.4.0 it was added to the freed slot, where the next entity inherited it, or to the listener's entity; under KENSEI_DEBUG Load threw;
    • if the entity already has the component, as its mask bit tells (a listener derived it from another restored component, in a world wired like the saved one; the pool would also hold an orphan, which belongs to no entity), RestoreOver writes the stored value over it through Modify (so it is marked changed), after resetting the old value as a Remove would (AutoReset, or the list owner's Replace for a pooled list). No listener is told: there is no second OnAdded. Up to 2.4.0 Load threw "already has component";
    • otherwise it calls the ordinary pool.Add(entityIndex, value).

Because step 4 is the normal Add, masks, component counts, filters, groups, change versions and pool listeners all update as they would for live code; because _dispatch is false, world listeners and the profiler observe nothing, and no OnEntityCreated is dispatched. SuppressEvents(false) runs in a finally.

Type resolution: Type.GetType(assemblyQualifiedName) first; if that fails (the assembly version embedded in the name changed), the namespace-qualified part before the first comma is looked up in every loaded assembly. A type that is not loaded throws InvalidDataException. Pools are reached through a per-type accessor: Register<T>() (with or without a formatter) installs a typed accessor that calls world.Pool<T>() directly, while a type that appears only in the stream gets a reflection accessor built with MethodInfo.MakeGenericMethod. Registering is what makes Load independent of reflection under IL2CPP for a type no other code touches.

Not part of a snapshot: debug names, listeners, filter and group registrations (they belong to the World object; those that exist before Load are filled), and the change counter (restored components on tracking pools are stamped as changed by Add).

Built on the core ​

  • CommandBuffer records (Op, PendingId, Entity, TypeIndex, PayloadIndex) structs and stores payloads in per-type PayloadStore<T> arrays indexed by type index, so nothing is boxed. Playback resolves PendingEntity ids through world.CreateEntity, skips commands whose Entity is dead, and applies Add via pool.Add (throws on duplicate), Set via overwrite-or-add, Remove via world.GetPool(typeIndex)?.Remove (no pool creation) and Destroy via world.DestroyEntity. After the first frame the arrays are reused; Clear only wipes payload arrays for types that contain references, so the GC can collect them. It walks the list of stores the buffer has created (_storeList), not every slot of the type-indexed _stores.
  • EventBuffer<T> is a component holding a List<T> rented from a static per-type ListPool<T>; its AutoReset returns the list, so OneFrame<EventBuffer<T>> stops allocating once the pool and its lists have grown to the peak load. A struct copy of the component shares the list, and a copy kept past the component's removal (a var b = pool.Get(e) written back with Add or CommandBuffer.Set after a OneFrame cleanup) would hand a pooled list to two entities. The pool hands out a PooledList<T> that records whether it is free and which entity holds it, as the holder pool's PoolId (unique across worlds) and the entity index rather than a reference: on IL2CPP every store of a reference field pays a GC write barrier, and these fields change on every rent and return. EventBuffer<T> implements the internal IPooledListComponent<T>, from which its pool takes a PooledListOwner<T> once (_listOwner, an abstract class: a virtual call is cheaper than an interface call on a boxed struct): every store of a value (Add, CommandBuffer.Set's overwrite, CopyEntity, snapshot loads) claims a fresh list for the entity, replaces a list that is free or held by another entity with an empty list of its own (an overwrite with such a value keeps the entity's buffer instead), and an overwrite with a new list returns the replaced one. Return pools only its own lists and ignores a second return, and Rent clears a list a stale copy appended to while it was free. List owners take the hooked Add/Remove path; other pools pay nothing. A write straight into the pool's storage (through a ref from Get, or RawData) is not seen and is not supported for copies. World.Warmup does not fill the pool: its default EventBuffer<T> has a null list and rents nothing; EventBuffer<T>.Prewarm(lists, capacity) does: it prepares only the top lists free lists (the next ones Rent hands out). Each slot keeps a list that is already large enough or takes one from below; when none is, the largest remaining list is grown to capacity, so at most lists lists are grown and a pool left large by an earlier peak is not inflated wholesale. Missing lists are created on top, and the prepared lists end up at the top of the pool. ListPool<T> is static, so all worlds in the process share it. world.AddEvent appends to an existing buffer or adds a new one; for a buffer the entity has without a list (added empty, or set to default) it rents the list itself and records the entity as its holder, since EventBuffer<T>.Add cannot know the entity: a pool.Get(e).Add(v) on such a buffer rents a list without a holder, which a copy of the buffer can then give to a second entity.
  • Listeners<T> is a component holding a List<T> of interface implementations and nothing else: a system that dispatches walks Values itself, which costs no delegate, snapshot or allocation. Subscribe adds the component on first use, Unsubscribe keeps it even when empty so the entity is not auto-destroyed by unsubscribing, and HasListeners reports false for an empty list. A callback must not change the list it is called from: the documented for loop does not check it (a foreach would, through the list's version, but under IL2CPP its shared-generic enumerator costs 1.5 to 3 times as much as for). UnsubscribeLater appends the entity and the listener to a per-world, per-type PendingUnsubscribes<T> (found through a short list of IPendingUnsubscribes, an interface call rather than a delegate) and marks the world. SystemsRunner.Run(World) brackets its work with World.EnterRun/ExitRun in an outer try/finally; the call that brings the depth back to 0 (the outermost Run, including a phase driven on its own) applies the queues, and so does NextTick at depth 0 (the root runner calls it first, a world without a runner calls it itself); Apply looks the pool up without creating it: each removal checks the entity's generation, so one queued for a destroyed entity does not touch an entity that reused the slot, and the queue clears its listener slots so it holds no view alive. World.Clear drops the queues. The 2.2 Invoke overloads, obsolete since 2.3.0, still copy the list into an array rented from ArrayPool<T>.Shared and skip listeners unsubscribed mid-dispatch, until 3.0 removes them. The list itself is not pooled: AutoReset clears and drops it, because a struct copy of the component written back after a remove (Add or CommandBuffer.Set) would otherwise hand a pooled list to two entities.
  • SystemsRunner keeps separate lists of IInitSystem, IRunSystem and IDestroySystem so Run does no type checks. A phase (AddPhase, or the obsolete Add(runner, name)) is registered as an init/destroy participant but excluded from _runSystems, which is what makes it a separate phase; an inline group (AddGroup, or an unnamed Add(runner)) is an ordinary IRunSystem in the parent's list, and its slot flag and its own IsEnabled are kept in sync. A runner has at most one parent (_parent, with its entry there in _registration): AddCore rejects, in every build and before anything is registered, a runner that already has a parent and one that is the parent itself or one of its ancestors, and SetActive(bool) on an inline group writes the parent's slot through _registration.RunIndex. Only the root's parameterless Run() advances the world tick. OneFrameCleanup<T> calls pool.RemoveAll(), which walks pool.RawEntities from Count - 1 down to 0 calling pool.Remove, which is safe with swap-remove and with group swaps. When no callback can run during the removals (no hooks on the pool, no world listener, no filter of the type with listeners: World.RemovesQuietly), nothing else changes the pool, so every component to remove is the last one: RemoveAll then removes it in place (AutoReset, tail default, sparse entry, count, World.OnComponentRemoved), without the lookup, the hook check and the swap of Remove, in the same order. AutoReset is the only user code on that path; it should reset the value and nothing else, and one that changes the world anyway (moves the pool's count or entity array, subscribes a listener) is caught after its own removal, and the rest of the cleanup goes through Remove as the plain loop would. A removal that throws does not stop RemoveAll on either path: the components below it are removed through Remove, exceptions thrown there are dropped, and the first one leaves. The handler of the in-place loop is told the dense slot whose removal was running, because the pool's count does not say it: an AutoReset throws before anything of that removal was done (its component stays), while an exception from the world's side of it (under KENSEI_DEBUG the report of a removal that breaks a running loop over a filter) comes after the component left the pool. The loop of a type without AutoReset runs no user code and has a handler only under KENSEI_DEBUG; DelHere<T> registers the same object as a run system at that position. On Unity every run system is wrapped in a ProfilerMarker; under KENSEI_DEBUG Stopwatch timings are recorded per system.
  • The Unity layer (KenseiECS/Unity) is thin. EcsBootstrap is an abstract MonoBehaviour that creates the World (from CreateConfig()), a SharedData, a root SystemsRunner and two child runners, calls the subclass's Configure(update, fixedUpdate, lateUpdate, shared) in Awake, then registers the children as the named phases "fixed" and "late". Start calls Warmup() or Init(); Update, FixedUpdate and LateUpdate each run their runner only while that runner's IsInitialized is true, so a failed Init, or an Awake that failed before the phases were registered, does not turn into an exception (or a world tick) per frame; under KENSEI_DEBUG Awake checks right after Configure that nothing in the update runner was registered under "fixed" or "late"; OnDestroy destroys the systems, then the world. It implements IEcsWorldProvider and IEcsSystemsProvider, which the editor windows use to list its world first and label it with the GameObject. EcsComponentProvider<T> is a MonoBehaviour holding one serialized T; EcsEntityView.Spawn(world) collects the providers on the GameObject in component order, creates the entity from the first (CreateEntity) and adds the rest (Add), assigns the serialized EntityName as the debug name (a no-op without KENSEI_DEBUG), and binds the view.
  • The editor windows list worlds through Core/WorldRegistry.cs, compiled only under UNITY_EDITOR || KENSEI_DEBUG. The World constructor registers the world and Destroy unregisters it; SystemsRunner.Init registers a root runner (no parent) once it completes. Entries are WeakReferences, so the registry never keeps a world alive; collected entries are swept when a list reaches a size that then doubles from what is left, so creating many worlds costs amortized O(1). Reads return live entries in registration order under a lock. The registry is internal; [assembly: InternalsVisibleTo("KenseiECS.Editor")] in the same file opens it to the editor assembly, whose EcsWorldPicker puts the worlds of IEcsWorldProvider/IEcsSystemsProvider behaviours first and the registry's after them, and draws the toolbar dropdown of the three windows.

The source generator ​

KenseiECS.Generators/SystemInjectionGenerator.cs is a Roslyn ISourceGenerator compiled against Microsoft.CodeAnalysis.CSharp 3.8.0 for netstandard2.0, so it loads in the compiler shipped with Unity 2021.3 as well as newer ones. The built assembly is committed as KenseiECS/Plugins/KenseiECS.Generators.dll. Its .meta file carries the RoslynAnalyzer asset label and disables every platform: Unity's convention for an analyzer or generator, which is handed to the C# compiler rather than loaded as a runtime assembly. A .NET project uses it by referencing the generator project (or DLL) as an analyzer, as KenseiECS.Generators.Tests does.

The attributes it reads are ordinary runtime attributes in Core/InjectAttributes.cs: [Inc(params Type[])], [Exc(params Type[])], [Any(params Type[])] for Filter fields, [Pool] for ComponentPool<T> fields, [Group] for Group<...> fields, [Shared] / [Shared("key")] for anything registered in SharedData. All are field-only and single-use.

What it does, per compilation:

  1. A syntax receiver flags every class, struct or record that declares a field (or, for KECS015, an auto-property or a field-like event with a field: target) carrying an attribute whose short name is Inc, Exc, Any, Pool, Group or Shared (with or without the Attribute suffix), or the name of a using alias (also a global using in another file) that points to one of them. The receiver only collects candidates by name; step 2 decides by the attribute class.

  2. Each flagged class symbol is processed once (partial declarations are deduplicated). Its non-static fields are inspected and attributes are matched by full name (KenseiECS.IncAttribute and so on), so a same-named attribute from another namespace is ignored. A marked field that the generated Init cannot fill is reported and left out: a static field (warning KECS014), the backing field of an auto-property or of a field-like event marked [field: ...] (warning KECS015). A field with two kinds of injection attribute is injected by the first in the order filter, [Pool], [Group], [Shared], and the other is the warning KECS016.

  3. Each injected field yields one assignment line:

    • [Inc]/[Exc]/[Any] on a KenseiECS.Filter field: this.f = world.Filter().Inc<A>().Inc<B>().Exc<C>().Any<D>().End(); with fully qualified type names. KECS003 if the field is not a Filter (compared without the nullable annotation, so Filter? passes); KECS004 if there is [Exc] without [Inc] or [Any]; KECS007 if [Inc] and [Any] together list no type; KECS008 if a listed type is not a struct implementing IComponent (an unresolved type, for example one produced by another generator, is left to the compiler); KECS019 if a type is listed in [Exc] and also in [Inc] or [Any] (tuple element names do not make a different type); KECS017 if a listed type is file-local.
    • [Pool] on a ComponentPool<T> field: this.f = world.Pool<T>();. KECS003 otherwise.
    • [Group] on a Group<...> field: this.f = world.Group<...>();. KECS003 otherwise.
    • [Shared] on a field of any reference type T: this.f = shared.Get<T>();, or shared.Get<T>("key") when a key is given. KECS003 for a value type.
    • Any injected field declared readonly: KECS009.
  4. A class with no injected fields gets nothing. A record or struct with injected fields gets KECS010. Otherwise: KECS017 if the class is file-local or nested in a file-local type (the generated part is another file); KECS001 if the class is not partial; KECS002 if the class itself declares an Init(World, SharedData) in source (static or not, with any return type; an Init with other parameters is an overload) or implements IInitSystem.Init explicitly; KECS018 if the name Init is taken: the class is named Init, has a type parameter named Init, or declares a field, property, event or nested type named Init; KECS011 if a containing type is a record or an interface; KECS005 if a containing type is not partial.

  5. Inheritance is resolved after the per-class checks, base classes first (a base in the same compilation is analysed on demand when a derived class reaches it). A base that neither implements IInitSystem nor has injected fields anywhere up its chain is not a system: the generated Init is no override, and it is declared new exactly when it hides an accessible inherited member (an Init(World, SharedData) with any return type, static or not, or a non-method member named Init). Otherwise the walk up the base chain takes the first class that either has injected fields in this compilation (it must have generated successfully, otherwise the derived class is skipped silently, the base already reported its own error) or declares an Init(World, SharedData): an explicit IInitSystem.Init first, whatever the member order, since the interface maps to it; else an instance void Init(World, SharedData) with by-value parameters, not generic and accessible from the derived class. The generated Init is an override that fills its own fields, calls base.Init(world, shared) (not when the base Init is abstract) and then OnInit, so the base Init and the virtual hooks it calls see the derived fields. KECS006 if the found Init is explicit, not virtual, sealed, not public, or a non-override Init that is not what IInitSystem.Init maps to for its class (FindImplementationForInterfaceMember; a new virtual Init hiding the interface method is not). An explicit IInitSystem.Init that calls an ordinary Init(World, SharedData) of its class that can be overridden (Init(...) or this.Init(...) in its body, bound by the compiler; only a declaration in source can be read) is the one exception: that Init is chained instead, since the runner reaches it, and an override of it, through the explicit one. In that case only a class that lists IInitSystem, or an interface that extends it, among its own bases is accepted, with the 2.1 layout (its own Init re-implements the interface, declared new when it hides something, and its OnInit calls the base), and only when the class has a base.Init in its own declarations: without one the base Init would be skipped, which is KECS006 when the base has injected fields or the runner reaches the found Init through IInitSystem.Init: it is what the interface maps to, the Init an explicit implementation calls, or an override of either (the interface maps to the first declaration of the slot, not to a sealed or protected override in a class that does not list the interface). An explicit implementation itself cannot be reached through base.Init, so it is exempt. KECS006 also when a hand-written class between the derived class and the chained base declares a member named Init that is not a method (the override would fail with CS0505) or another accessible Init overload that a (World, SharedData) call could bind to (a method type parameter that neither argument can infer makes it inapplicable), since C# would pick it for the generated base.Init. When the generated Init calls the base, every base.Init member access in the class's own declarations (nested types excluded) is KECS012: a syntactic check with no overload resolution, because the user's call would run the base a second time. A hand-written class without injected fields whose override of Init(World, SharedData) ends up overriding a generated Init (directly or through hand-written overriding classes) and that has no base.Init in its own declarations gets the warning KECS013. So does one whose base.Init calls all go to another Init overload. The compilation the generator sees cannot say where such a call goes: it has no generated Init yet, so it binds the call to an overload that the generated Init will beat, or to a hand-written override that C# will later look up at the generated base, above an overload that then wins. When a base declares an Init overload at all, the calls are therefore bound in a copy of the compilation with the generated Init of every base added, which gives the compiler's own answer; the base.Init call of the 2.1 layout is judged the same way (KECS006). A generated base in a referenced assembly is recognised only by injected fields visible from here, because private members of referenced assemblies are not imported.

  6. OnInit is normally a partial void declaration plus a call at the end of Init: implement it in your part of the class to run code after the fields are filled (and after the base Init), or leave it out and the compiler drops the call. A class that declares an OnInit(World, SharedData) of its own that is not the implementation of that partial gets no declaration: a non-override one is called, and so is an override of a base hook, unless the generated Init calls a base Init (step 5) and the hook is first declared in that base or below it, where the base Init can call it. A hook declared between the two (in a hand-written class over a generated base), an abstract base Init or a base that is not a system leave nothing else to call it. In the 2.1 layout an override of a hook the replaced base Init can call is KECS006: the generated Init would call it, and the base.Init that OnInit must call would call it again. When a base has an accessible member named OnInit with that signature or a non-method one (the virtual hook of a hand-written base, or the private OnInit of a generated base that the class is nested in), the partial would hide it: it is declared, inside #pragma warning disable CS0108, CS0114 because a partial method without an access modifier cannot be new, only when the class implements it, and otherwise left out together with its call. A class named OnInit, or with a type parameter named OnInit, gets neither the declaration nor the call: no member of it can have that name (CS0542, CS0102).

  7. The emitted file, {FullName}.Injection.g.cs (namespaces, containing types and the type name without @, with _ for the type-parameter brackets), re-opens the namespace and every containing type as partial (keeping static, class/struct and type parameters; keywords in names are escaped as @name, and a [Shared] key is written as an escaped string literal), then declares:

    csharp
    partial class MovementSystem : global::KenseiECS.IInitSystem {
        partial void OnInit(global::KenseiECS.World world, global::KenseiECS.SharedData shared);
    
        public virtual void Init(global::KenseiECS.World world, global::KenseiECS.SharedData shared) {
            this._moving = world.Filter().Inc<global::Game.Position>().Inc<global::Game.Velocity>().End();
            this._positions = world.Pool<global::Game.Position>();
            OnInit(world, shared);
        }
    }

    The : IInitSystem base is omitted when the class already implements it or overrides an inherited Init; Init is public override when it chains (step 5), otherwise public virtual void in a class that is not sealed and public void in a sealed one, with new when it hides an inherited member. The form never depends on which subclasses the compilation contains, so a subclass in another assembly, or one compiled only under a define, sees the same Init. Roslyn compares hint names without case and drops the whole generator output when two are equal, so names that differ only in case (Game.AI.Move and Game.Ai.Move) get a numeric segment (Game.Ai.Move.2.Injection.g.cs), assigned in ordinal order of the full names so that it does not depend on the order of the files; a type or namespace name cannot start with a digit, so the segment cannot meet another name.

  8. Each class is analysed and written on its own. An exception on the way (a bug of the generator, or a Roslyn API that answers differently on code with compiler errors) is caught and reported on that class as the error KECS020, with the exception type and message; the class gets no Init, a class derived from it is skipped without a second error, and every other class is generated as usual. Uncaught, the exception would make Roslyn drop the whole generator output (warning CS8785), and no system of the assembly would get its Init.

The generated code calls the same public API a hand-written Init would. There is no reflection and nothing for IL2CPP to strip; filters built this way deduplicate against builder-made and spec-made filters like any other.

The KENSEI_DEBUG layer ​

Everything in this section is inside #if KENSEI_DEBUG and does not exist in a release build: no fields, no checks, no Dispose on the enumerator, no profiler hooks. The cost in release is zero.

What is checked:

WhereCheck
World.Add/Get/Has/Remove, AddEventValidateHandle: the handle must be alive, or be the entity whose DestroyEntity is running (its slot is on the stack of dying slots and the generation matches). Dead and stale handles throw, also while another entity is being destroyed.
World.CopyEntity, GetComponentTypes, GetComponentCount, SetNameThrow on a dead entity (release returns Entity.Null / reads the slot / ignores).
World.DrainComponentsThrows after 1000 passes when a listener keeps re-adding components to a dying entity.
ComponentPool<T>.Add(int)IsSlotAcceptingComponents: the slot must be below _nextIndex and alive, or be the slot being destroyed. An int carries no generation, so this is the strongest check possible.
ComponentPool<T>.Get(int), Modify(int), MarkChanged(int), ChangedVersion(int)Throw when the component is absent. (ChangedVersion throws in every build when the pool does not track.)
WorldListenerExtensions.Subscribe/UnsubscribeThrow on a dead entity.
FilterThe iteration guard below.
SystemsRunnerAdd after Init, Run before Init, Init/Run with a different World or explicitly different SharedData, nested runner with a different World/SharedData, unknown names in SetActive/IsActive/GetRunner. Per-system LastRunMs/PeakRunMs.
ToolingEcsProfiler hooks on create/destroy/add/remove (skipped while events are suppressed), WorldDebugView as DebuggerTypeProxy for World, GetRaw/SetRaw on pools, entity debug names (SetName, EcsEntityView.EntityName).

Groups, change tracking and snapshots add no debug-only checks beyond the pool ones listed; their consistency errors (double ownership, non-empty world on Load, missing formatter, bad magic) throw in every build.

The iteration guard ​

Each Filter keeps _debugCursors (one slot per live enumerator, innermost last) and _debugIterators (depth). The enumerator constructor records its depth and writes its starting cursor (_count + 1); every successful MoveNext writes the slot it is on; Dispose decrements the depth. foreach calls Dispose on a ref struct enumerator through pattern-based disposal, so the guard is released on normal exit, break and exceptions.

RemoveEntity checks every live cursor before the swap:

csharp
for (int d = 0; d < _debugIterators; d++) {
    int cursor = _debugCursors[d];
    if (denseIdx < cursor && lastIdx >= cursor) {
        throw new InvalidOperationException(
            $"Entity {entityIndex} left a filter that is being iterated before the loop reached it; " +
            "an already visited entity would be visited twice");
    }
}

Reverse iteration has visited every slot above the cursor and is currently on the cursor. Removing the entity at denseIdx moves the entity from lastIdx into denseIdx. The move is harmful only when the destination has not been visited yet (denseIdx < cursor) and the moved entity has (lastIdx >= cursor): it would be yielded again. Every other combination is safe: removing the current entity (denseIdx == cursor) or a visited one, or removing an unvisited entity when the last slot is also unvisited (lastIdx < cursor), which happens after the loop has already shrunk the live range. Nested loops over the same filter are covered because every enumerator's cursor is checked.

There is no equivalent guard for group loops; the reverse-iteration rule for destroying members is documented above and is the user's responsibility.

Performance characteristics ​

W is the number of mask words (ceil(types / 64)), C the number of components on the entity, F(t) the number of filters that constrain type t, P the number of pools an owning group owns, L the number of listeners that fire.

OperationCostNotes
CreateEntity<T>(T)O(1) amortized + one AddPops the free stack or bumps the high-water mark; may grow every slot array and mask word.
Add<T>O(1) + O(F(t) + L)One _addHooks branch. Each filter test is one or a few ulong operations. Sparse/dense growth is amortized.
Add<T> on an owned type+ O(P)P Has checks; P SwapDense when the entity becomes a member.
Add<T> on a tracking pool+ O(1)One counter increment, one store.
Remove<T>O(1) + O(F(t) + L)Swap-remove. Plus a DestroyEntity if it was the last component.
Remove<T> on an owned type+ O(P) when the entity was a memberOtherwise one Contains.
Get<T> / pool.Get(int)O(1)Sparse read, then dense read. Untracked.
pool.Modify(int), MarkChanged(int)O(1)Get plus a counter increment and a store.
pool.ChangedSince(int, int)O(1)Sparse read, version read, compare.
Has<T>O(1)One mask word read; no pool access.
pool.Has(int)O(1)Sparse read.
IsAlive, GetEntity(int), TryGetEntity(int), GetComponentCountO(1)
DestroyEntityO(W + C × (F + L + P))Mask walk over set bits only, up to the last word holding a component (none for an auto-destroy); each component pays its own Remove.
CopyEntityO(W + C) + C × AddSame walk; AutoCopy per component that implements it.
GetComponentTypesO(W + C)
foreach (int e in filter)O(matches)One span load per step, zero allocation; component access is a sparse lookup per pool.
Group loop over Data1..DataNO(members)Contiguous spans, no sparse lookups, no per-entity mask test.
Filter.Count/IsEmpty/Contains/First/SingleO(1)
Filter().…End(), Filter<Spec>()O(registered filters) + O(entity slots)Dedup scan, then PopulateFilter. Allocates. Build in Init.
Group<T1, T2>()O(registered groups) + O(smallest owned pool × P)Dedup scan, then Populate. Allocates. Build in Init.
pool.TrackChanges()O(pool count) onceAllocates the version array.
Pool<T>()O(1)Type check on _pools[typeIdx]; first call allocates the pool.
GetSingleton<T>O(1)pool.RawData[0] after a count check.
CommandBuffer.PlaybackO(commands)Each command pays the corresponding world operation.
SystemsRunner.RunO(systems) + Σ OneFrame pool counts
WorldSerializer.SaveO(high-water slots + registered types + Σ components)Plus stream I/O.
WorldSerializer.LoadO(entities + high-water slots) + Σ components × AddPlus one type resolution per stored pool.
WarmupO(registered pools) × (Add + Remove)
ClearO(high-water slots × W) + Σ pool counts + Σ filter counts + groupsPools and filters reset through their dense lists, not their sparse arrays.

What scales with the number of component types versus the entity's own components:

  • Per-frame operations (Add, Remove, Get, Has, iteration, DestroyEntity, CopyEntity) never loop over registered types. DestroyEntity and CopyEntity read word 0, then only the words whose bit is set in the slot's _usedWords (at most W), then only set bits; DestroyEntity also stops after the word that holds the entity's last component.
  • Warmup, World.Clear, World.Destroy, WorldSerializer.Save and ActivePools loop over _pools, i.e. over registered types. They are setup, teardown and save operations.
  • Filter matching is O(active words), at most W; single-word filters read three scalars.

Memory, per element:

  • Entity slot: 4 bytes generation, 1 byte alive, 4 bytes count, 8 × W bytes of masks, and 8 bytes of _usedWords when W > 1.
  • Pool: 4 bytes per sparse slot (grows to cover the highest entity index that ever held the component, starting at InitialPoolSparseCapacity), plus (4 + sizeof(T)) per dense slot (starts at InitialPoolDenseCapacity, doubles with the component count), plus 4 bytes per dense slot once TrackChanges was called. One boxed default(T) per implemented bridge interface (IAutoReset<T>, IAutoCopy<T>). ComponentPoolBase.AllocatedBytes reports the array sizes (without the version array).
  • Filter: 4 bytes per dense slot plus one, 4 KB per touched 1024-entity page, three mask arrays of ceil(maxConstrainedType / 64) words. Filter.AllocatedBytes reports it.
  • Group: P pool references and a count; no arrays of its own.
  • Registry: _pools and _filtersByType hold one reference per type index up to the highest seen; TypeFilters objects exist only for constrained types.

Known limitations and design decisions ​

No archetypes. Storage is one sparse set per component type. Structural changes are O(1) and never move an entity's other components; a ref T stays valid across adds of other types; filters are cheap sparse sets updated incrementally; there is no combinatorial explosion of archetypes when many optional components exist. The price is iteration: reading two components per entity through a filter is two sparse-to-dense indirections into two unrelated arrays, which loses to chunked archetype layouts on pure iteration (see the benchmarks) and wins on structural changes and mixed frames. Owning groups recover contiguous iteration for a chosen set of types at the cost of one extra swap per owned pool when an entity joins or leaves, and of the one-owner-per-pool rule.

CreateEntity requires a component. The invariant is "alive if and only if it has at least one component". An empty entity would be invisible to every filter, invisible to world listeners (OnEntityCreated fires after the first component), and would be destroyed by the first Remove anyway. Requiring the component at creation removes the empty state entirely, so there is no leak class to detect and no "empty entity" check to run. CommandBuffer.CreateEntity<T>(T) and EcsEntityView.Spawn (which throws without a provider) mirror the rule.

Auto-destroy on last component removal. Same invariant from the other side. It makes "event entities" free: an entity whose only component is a one-frame component disappears in the cleanup. The one place where this surprised users, unsubscribing the last listener, is handled by keeping the empty Listeners<T> component.

Single-threaded. World, pools, filters and groups have no synchronization. Reading RawData/RawEntities/group spans from several threads is safe only while no structural change happens anywhere. ComponentType<T>.Index registration is thread-safe. EventBuffer<T>'s list pool is a static per-type stack shared by all worlds in the process and is not thread-safe either.

Iteration order is unspecified and changes with every swap-remove and every group swap. Removing a not-yet-visited entity from the filter being iterated can double-visit an already visited entity (thrown under KENSEI_DEBUG); defer such changes with a CommandBuffer. Group loops that destroy members must run downward.

Exclude-only and empty filters are rejected. Membership is driven solely by add/remove of constrained types, and PopulateFilter needs at least one required or alternative type to reason about; add an Inc for a component the target entities always have. The generator reports the same rule as KECS004.

One owner per pool, order-sensitive. A component type can be owned by one group, groups take two to four types, Group<A, B> and Group<B, A> are different requests (the second throws), and groups cannot be unregistered. Owning a pool changes the order of its RawData, which was never guaranteed.

Change tracking is opt-in and write-side. Only Add, Modify and MarkChanged stamp versions; writes through Get, raw arrays or group spans do not.

Snapshots are layout-bound and partial. Raw components are the writing platform's struct layout; the format is version 2 since 2.5.0 (version 1 still loads, 2.4.0 cannot read version 2); listeners, debug names, filter and group registrations are not stored; component types are found by name, so renaming or moving a type breaks old files.

The generator injects fields only. It needs a partial class (and partial containers), forbids a hand-written two-parameter Init next to injected fields, and targets the Roslyn 3.8 API.

ref T invalidation. A ref from Get/Add/Modify is valid until the next Add or Remove of the same type on any entity (including those inside DestroyEntity, CopyEntity, one-frame cleanup, CommandBuffer.Playback and listeners), or, in a group-owned pool, the next Add or Remove of any of the group's types, or a Reserve that grows the pool's dense arrays, or Clear/Destroy of the world.

Type indices are process-wide and depend on first-touch order. They are not stable across runs and must not be serialized (snapshots use names). Every world's registries are sized by the highest type index it has seen, not by how many types it uses.

Entity carries no world id. Using a handle from one world on another is undefined; the generation check catches it only by accident.

Filters cannot be unregistered. They live until World.Destroy. Static specs are limited to Inc 1-6, Exc 1-4, Any 2-4 types; the builder and the attributes have no limit.