EasyEcs

API and complexity

Every public type and call of EasyEcs on one page, grouped by task, with its time complexity and allocations. The member reference comes from the XML docs.

Every public type and call of EasyEcs, grouped by what you do with it, with the cost of each. Everything is in namespace EasyEcs. T1..T9 overloads come from one template each (Architecture). For signatures, parameters and the XML documentation of each member, see the generated API reference; its pages quote the complexities below.

Notation:

  • n: components in the call (1 to 9).
  • c: components of the entity.
  • M: tables (archetypes) matching a query.
  • S: rows of those tables: living entities, plus tombstones while a pass that makes structural changes runs.
  • k: entities of one key of an index; L: links of one entity.
  • "Amortized": tables and the entity array occasionally double.

Unless a row says otherwise, a call allocates nothing once the world is warmed up, and only the world's owner thread may make it (Threading).

Types

TypeKindPurpose
Worldsealed class, IDisposableOwns entities, components, singletons, indexes and systems.
WorldOptionssealed classParallelSystems, MaxDegreeOfParallelism, InitialEntityCapacity, MeasureSystems, SystemErrors (init-only).
Entityreadonly structHandle: Id, World, IsAlive. A destroyed or default handle answers questions (IsAlive, Has, TryGet, TryGetRef) with false, throws on accesses and changes, and ignores Destroy. Converts to its EntityId.
EntityIdreadonly struct, unmanagedIndex and Version, 8 bytes: what a component stores to refer to an entity. EntityId.Null names none.
IComponent, ISingletonComponentmarker interfacesComponent and singleton structs. An empty component is a tag.
ITrackChangesmarker interfaceRows record the change tick of their last write or removal (change tracking).
IIndexed<TKey>interfaceA component the world indexes by its Key; written with Set (EECS015). An EntityId key makes it a link to an entity.
IRelationinterfaceAn element of a DynamicBuffer<T> that links its entity to Target (indexes and relations).
ChildOfrecord structChildOf(EntityId Parent): the hierarchy; destroying a parent destroys its children (hierarchy).
IGroupinterfaceA component whose Group value splits tables (world.GroupBy<T>(), groups).
ICleanupComponent, Destroyedmarker interface, tagAn entity destroyed while it holds a cleanup component keeps it, tagged Destroyed, until it is removed (cleanup and disable).
Disabled<T>tagPresent while T is disabled: the value kept, the component hidden.
Prefabsealed classA recipe of component values that belongs to no world (prefabs).
DynamicBuffer<T>struct, IComponent, ITrackChangesA list per entity; writes through DynamicBufferExtensions on a ref (dynamic buffers).
ValueIndex<T, TKey>sealed classworld.Index<T, TKey>(): Find, TryFind, Count; ordered walks for a comparable key (OrderedIndexExtensions).
RelationIndex<T>sealed classworld.Relations<T>(): the entities linking to a target.
TargetPolicy, TargetRulesenum, extensionsWhat destroying a target does to the links to it: Keep, Remove, Destroy.
EcsSystemabstract classWorld, UpdateInterval, UpdatePhase, Enabled, Error, Stats, LastRunTick; OnCreate / OnUpdate / OnDestroy (systems).
ForEachAttribute and its filtersattributes[ForEach] (Parallel = true), [With<T>], [Without<T>], [WithAny<T>], [Changed<T>], [Removed<T>].
Maybe<T>ref structAn optional component parameter of a [ForEach] method: HasValue, Value.
ISystemGroup, IGroupWith<T>, IGroupWithout<T>, [UpdateInGroup<T>], [UpdateBefore<T>], [UpdateAfter<T>]interfaces, attributesSystem groups (with filters through interfaces) and ordering.
SystemErrorPolicyenumThrow (default) or Disable: what an exception in a system does.
SystemStats, SystemListreadonly structsA system's run count and times; world.Systems, allocation-free.
SystemAccessstructWhat a system reads and writes; filled only by generated code.
QueryFilterreadonly structWith, Without, WithAny, Changed, Removed, InGroup, Since; holds no object reference.
QueryEnumerator<T1..Tn>, QueryItem<T1..Tn>ref structsThe result of Query: item.Entity and refs into the row (item.Position).
IForEach<…>, IForEachEntity<…>, IForEachBatch<…>, INoStructuralChangesinterfacesStruct jobs for ad-hoc passes; EntitySpan holds a batch run's entities.
SingletonRef<T>readonly structA stable singleton handle; .Value is ref T.
WorldSnapshotsealed classA copy of a world in memory (world.Capture()).
TableInfo, ColumnInfo, ComponentValuerecord structsWhat world.Describe() and entity.Describe() return.

Entities

APIComplexityNotes
new World(options)O(InitialEntityCapacity)options may be null (defaults); allocates the entity array and the empty table
world.CreateEntity()O(1) amortizedreuses recycled ids
world.CreateEntity(in T1 … in Tn)O(n) amortizedplaced directly in its final table
world.CreateEntities(Span<Entity> entities, in T1 … in Tn)O(count × n) amortizedone gate pass and at most one table growth for the wave; writes the handles
entity.Destroy()O(c)the table's last row fills the hole (a tombstone during a pass); recycles the id; a stale handle is ignored; unlinks indexed components; cascades to ChildOf children and applies target policies
world.DestroyEntities<T1..Tn>(filter)O(M + S)destroys every match in one gate pass and returns how many; safe during a pass
entity.IsAlive / world.IsAlive(id)O(1)
world.TryGetEntity(id, out e) / GetEntity(id)O(1)false (or a throw) when that incarnation is not alive
world.EntityCountO(1)
world.EntitiesO(capacity)living entities in id order, for diagnostics
world.EnsureEntityCapacity(capacity)O(capacity)pre-size the entity array; a no-op when large enough
world.EnsureCapacity<T1..Tn>(count)O(n), plus one growthpre-sizes the table of exactly T1..Tn and the entity array, so the next count creations with those components never grow them

Components

APIComplexityNotes
entity.Get<T>()O(1)ref T: a version check, a column lookup, one presence-bit test, one load; throws if missing or not alive; a write of a tracked component
entity.Read<T>()O(1)ref readonly T; never a write
entity.TryGetRef<T>(out bool found)O(1)ref T, a null ref when missing; a write of a tracked component when found
entity.Has<T>() / TryGet<T>(out T)O(1)false when missing or not alive; TryGet returns a copy (not a write)
entity.Has<T1..Tn>()O(n)all of them, one record lookup
entity.Set(in T1 … in Tn)O(n), or O(c) amortized when it movesadds or overwrites, at most one row copy, returns the entity; a component the table has a column for only sets its bit; re-indexes a changed key (O(1))
entity.GetOrAdd<T>()O(1); when missing, O(1) if the table has a column for T, else O(c) amortizedref T; adds a default value only when missing; a write of a tracked component
entity.Remove<T1..Tn>()O(n)clears presence bits, the row stays; missing components are ignored; a tracked component's removal is stamped ([Removed<T>]); returns the entity
entity.Disable<T>() / Enable<T>()O(1); the first disable in a table moves the row oncekeeps the value while queries, Has and Get see it as gone; a disable counts as a removal, an enable as a write; returns false when there was nothing to do
entity.IsDisabled<T>()O(1)
generated: entity.X, HasX, TryGetX, GetOrAddX(), SetX(…), RemoveX(), WriteX(), DisableX(), EnableX(), IsXDisabled; tags IsX, SetX(); world.XIndexas the generic callC# 14 extension members per component (generated members); entity.X is Get, or Read for a tracked or indexed component, whose writes use WriteX() or SetX(…)

Prefabs and cloning

A copy gets its own list for each DynamicBuffer<T>, is indexed at once, and counts as a write of every tracked component it has; components the source lacks stay absent and are not removals.

APIComplexityNotes
new Prefab()O(1)an empty recipe; allocates its private store once
new Prefab(basePrefab)O(c)a variant: a copy of every value (and member), changed on its own afterwards
prefab.Set(in T1 … in Tn)O(n), or O(c) amortized when it gains a componentadds or replaces values; returns the prefab
prefab.Remove<T1..Tn>()O(n)missing ones are ignored; returns the prefab
prefab.Has<T>() / Read<T>()O(1)Read throws if the prefab has no T
prefab.SetBuffer<T>(params ReadOnlySpan<T>)O(n)a DynamicBuffer<T> of these elements; every instance gets a list of its own
prefab.AddMember()O(1)a member of the prefab's group: instantiating one creates all; EntityId fields naming members are rewritten per instance
generated: prefab.X, HasX, SetX(…), RemoveX(); tags IsX, SetX(); buffers SetLoot(items…)as the generic callwriters return the prefab; the property is read-only; not structural changes of any world
world.Instantiate(prefab)O(c)one row copy; the target table is found once per prefab table and cached; safe during a pass (which does not visit the new entity); O(members × c) for a group
world.Instantiate(prefab, Span<Entity> entities)O(count × c)one gate pass and at most one table growth for the wave
world.EnsureCapacity(prefab, count)O(c), plus one growthpre-sizes the tables the next count instances go to, so a spawn wave never grows them during a tick
entity.Clone()O(c)a new entity of the same world and table with a copy of every component; throws if not alive

Dynamic buffers

new DynamicBuffer<T>() holds no list until its first write. Reads work on any reference (in, Read<T>()); writes need a ref and are tracked writes.

APIComplexityNotes
bag.Count, bag.Capacity, bag[i], AsReadOnlySpan(), GetEnumerator()O(1)read-only; out-of-range indexes throw
bag.Add(item) / AddRange(span)O(1) amortized / O(items)allocates only when the list grows
bag.Insert(i, item) / RemoveAt(i)O(count)keep the order
bag.RemoveAtSwapBack(i)O(1)the last element moves into i
bag.Clear()O(1), O(count) for elements with referenceskeeps the capacity
bag.EnsureCapacity(n) / AsSpan()O(count) when it grows / O(1)AsSpan() is writable
bag.TrimExcess()O(count)reallocates to the count; an empty list holds no array

A snapshot, a save or a clone of a buffer column copies the elements: O(elements), a new list per entity.

Queries

APIComplexityNotes
[ForEach] method / world.ForEach<T1..Tn, TJob>(ref job, filter)O(M + S)fastest: a struct job, no delegate
world.ForEachEntity<T1..Tn, TJob>(ref job, filter)O(M + S)the job also receives the Entity
entity.Run<T1..Tn, TJob>(ref job) / Run(ref job, filter)O(n), plus O(1) per filter componentone entity; false when stale, missing a component or rejected by the filter; refs survive the job's structural changes
world.ForEachBatch<T1..Tn, TJob>(ref job, filter)O(M + S / 64 + runs)spans of consecutive rows; no structural changes
world.ForEachParallel<T1..Tn, TJob>(ref job, filter) / ForEachEntityParallelO(M + S), divided over up to MaxDegreeOfParallelism threadsINoStructuralChanges jobs; chunks of 1,024 to World.ParallelChunkRows rows, one job copy per chunk; helpers are woken only when they pay
foreach (var item in world.Query<T1..Tn>(filter))O(M + S)refs into the row: every visited row of a tracked component counts as written; no await in the loop
world.Count<T1..Tn>(filter)O(M), + S / 64 for tables whose rows must be testedsums table counts, or counts presence bits (and ticks), 8 words per step with AVX-512
world.Any<T1..Tn>(filter)O(M + S / 64) worststops at the first match
QueryFilter.Create().With<T>().Without<U>().WithAny<V>()O(1)build once, keep in a static readonly field; WithAny calls form one "at least one of" group
filter.Changed<T>(since) / [Changed<T>]O(1); a pass skips tables, 4,096-row blocks and 64-row words written before sincerows whose tracked T was written at or after since (change tracking); one tick per filter
filter.Removed<T>(since) / [Removed<T>]as Changedentities that lost a tracked T at or after since and still lack it; destroyed entities are not reported
filter.InGroup<T>(group)O(1); a pass walks only that group's tablesgroups
filter.Since(since)O(1)the filter with its Changed and Removed ticks moved, without rebuilding it

The first use of a query shape builds its cache entry, O(all tables), under the shared gate. Later lookups are a memo hit or one hash probe. Creating a new table (the first entity with a new component set) costs O(tables + cached query shapes), as it joins every cached query it matches. Both are cold paths.

Disable and bulk changes

APIComplexityNotes
world.SetAll(in T value, filter)O(M + S)Set on every match, a word of rows at a time; an entity lacking T moves once; returns how many
world.RemoveAll<T>(filter)O(M + S)rows stay, presence bits clear; returns how many
world.DisableAll<T>(filter) / EnableAll<T>(filter)O(M + S)a table's rows move once, to the table with the Disabled<T> column; returns how many

Indexes

APIComplexityNotes
world.Index<T, TKey>()O(1); the first call creates itone per world and indexed component; a second key type throws
index.Count(key) / TryFind(key, out Entity)O(1)a hash probe (an array access for a key that is an entity); the first lookup indexes every row once
index.Find(key)O(1), then O(1) per entitya ref struct for foreach; destroying entities during the loop is allowed, changing T is not
index.Ascending() / Descending()O(keys log keys) for the first walk, then O(1) per entityordered walks for a comparable key (leaderboards, timers); after the first walk the index keeps its keys sorted: O(log keys) plus a block move per key that appears or disappears
index.Range(min, max) / RangeDescending(min, max)O(keys log keys) for the first walk, then O(log keys) to find min and O(1) per entityinclusive bounds
index.WhenTargetDestroyed(policy)O(1); then O(1) per destroyed entityan EntityId key: Keep (default), Remove or Destroy the components pointing at a destroyed entity

Hierarchy and relations

APIComplexityNotes
world.Children(parent)O(1), then O(1) per childthe entities whose ChildOf points at parent; destroying a parent destroys them, and theirs
world.Descendants(root)O(1) per entity of the subtreedepth first, every level below root; 0 B once warm
entity.AddRelation(in T relation)O(1) amortizedappends to the entity's DynamicBuffer<T> (added first if missing) and to the relation index
entity.RemoveRelation<T>(target) / HasRelation<T>(target)O(L)RemoveRelation returns how many links went
world.Relations<T>()O(1); the first call creates itthe relation index; generated as world.XIndex for a relation X
relations.Count(target)O(1)links to target
relations.Find(target)O(1), then O(1) per linkthe entities linking to target
relations.WhenTargetDestroyed(policy)O(1); then O(1) per destroyed entity and O(L) per linkRemove drops the links, Destroy destroys the linking entities

Groups

APIComplexityNotes
world.GroupBy<T>()O(entities with T), oncesplits tables by T.Group; setting a new value moves the entity to its group's tables (one row copy); call it at setup

Snapshots, saves and moves

APIComplexityNotes
world.Capture() / Capture(into)O(entities × components + tables + singletons)copies by value; Capture(into) reuses an earlier snapshot's arrays and allocates nothing once they are large enough; not while a pass iterates
world.Restore(snapshot)O(entities × components + tables + singletons)snapshot handles are valid again, later ones stale; every restored row counts as written (or removed)
world.Save(output)O(entities × components)the world as bytes: unmanaged rows raw, managed values through Nino; allocates nothing once its buffers are large enough
world.Load(input, markWritten)O(entities × components)entities keep their ids; a changed component layout throws InvalidDataException and leaves the world as it was; markWritten: false: the loaded rows count as never written
world.SaveEntities(output, ids)O(entities given × components)a group of entities, for another world or process
world.SaveEntities(output, ids, set)O(entities given × components)only the components of a ComponentSet (built once: ComponentSet.Create().With<T>(), O(1) per type)
world.LoadEntities(input, entities)O(entities × components)new ids; EntityId fields naming the group are rewritten, others become EntityId.Null
world.LoadEntitiesInto(input, entities, markWritten)O(entities × components)onto entities that exist: each moves table at most once, the components the save lacks stay; markWritten: false: the loaded components count as never written
world.MoveEntities(target, ids, moved)O(entities × components)to another world of this thread, without encoding; the originals are destroyed

Systems, singletons, maintenance

APIComplexityNotes
world.AddSystem<T>(changesFromNow) / AddSystem(system, changesFromNow)O(s²), s = systems of its groupcalls OnCreate; rebuilds the order and stages (cold); throws if the system already belongs to a world, or on an ordering cycle; changesFromNow: true: its first run sees only what is written after the call
world.RemoveSystem<T>()O(s²)calls OnDestroy; returns whether a system was removed
world.GetSystem<T>()O(systems)null when absent
world.Update() / world.Update<TGroup>()Σ systems of the groupsynchronous, allocation-free; throws ObjectDisposedException after Dispose
world.ChangeTickO(1)grows by one per stage of systems and once after each update
world.AddSingleton<T>() / GetSingleton<T>() / GetOrAddSingleton<T>()O(1)ref T; AddSingleton resets an existing singleton in place; GetSingleton throws when missing
world.SingletonRef<T>()O(1)a stable handle; throws when the singleton was not added; .Value is one load
world.TryGetSingleton<T>(out T) / HasSingleton<T>() / RemoveSingleton<T>()O(1)RemoveSingleton returns false when it was not added
generated: world.X, HasX, GetOrAddX(), SetX(…), RemoveX() for a singleton Xas the generic call
world.SystemsO(systems) to walk, Count O(groups)every group's systems in execution order, allocation-free
world.UpdateCountO(1)default-group updates so far
world.RemoveTypes(assembly) / RemoveTypes(context)O(entities with such components + tables + cached queries)takes a plugin's systems, singletons, indexes and components out of the world so its AssemblyLoadContext can unload; allocates (cold)
world.Describe()O(tables + columns)every table with its columns, rows, capacity and bytes; allocates (cold)
entity.Describe()O(c)boxed copies of the entity's components, disabled ones marked; allocates (cold)
world.Dispose()O(systems + entities + tables)OnDestroy on every system in reverse order, then releases storage

WorldOptions.SystemErrors: SystemErrorPolicy.Throw (default: the exception propagates out of Update(), and of Dispose for OnDestroy) or Disable (the system is switched off and keeps the exception in system.Error until it is enabled again; a failed run still moves LastRunTick on). WorldOptions.MeasureSystems times every run into system.Stats (Runs, Last, Total, Average) for two timestamp reads per run.

Values and handles, all O(1): entity.Id / World, id.Index / Version, == / Equals / GetHashCode (id and version) / ToString; snapshot.EntityCount; system.LastRunTick, UpdateInterval, UpdatePhase, Enabled, Error, Stats, World.

Edit on GitHub

Last updated on

How is this guide?

On this page