Migration guide
Upgrade EasyEcs one version step at a time: what to change in your code from v2 to v3, v3 to v4 and between 4.x releases, with code before and after.
What to change in your code when you upgrade EasyEcs, one version step per section, newest first. Each section lists every rename, removal and behavior change of its step, with the code to write instead. Upgrading across several versions? Apply the sections in order, oldest first. The changelog lists everything each release added; the API reference is the current API.
v4.3 → v4.4
The save format changed, and EasyEcs builds on Nino 4.0.0-rc.6. Code written for 4.3 compiles unchanged; what needs attention is saved data and the Nino package reference.
Saves of 4.3 and earlier do not load
A save now records each type by its Nino type id instead of its name, and writes unmanaged [NinoType] components
through their formatter. Load, LoadEntities and LoadEntitiesInto refuse the bytes of an earlier version with
InvalidDataException: Not an EasyEcs save (or one of another format version), and leave the world as it was.
Write again what must outlive the upgrade: load it with the 4.3 build, move it into a format of your own (or keep the 4.3 build as the tool that reads it), and save it with 4.4. Saves that only live as long as a process (hot reload, handing a player to another server of the same build) need nothing.
Nino 4.0.0-rc.6
EasyEcs references Nino.Serialization 4.0.0-rc.6. A project that generates formatters references the same version
of Nino.Generator:
4.3
<PackageReference Include="Nino.Generator" Version="4.0.0-rc.4" />4.4
<PackageReference Include="Nino.Generator" Version="4.0.0-rc.6" />Unmanaged [NinoType] components are written by Nino
An unmanaged component marked [NinoType] was saved as raw rows, like any unmanaged component. It is now written by
its Nino formatter, value by value, so its format decides how it may change between builds
(when the code changed). Keep the attribute where the shape will change; remove it
where raw rows (one block per column) are what you want:
4.3
[NinoType]
public partial record struct Speed(float Value) : IComponent; // saved as raw rows4.4
using Nino.Core;
public record struct Speed(float Value) : IComponent; // raw rows
[NinoType(format: NinoFormat.VersionTolerant)]
public partial record struct Rank(int Tier, int Points) : IComponent; // by its formatter: may gain membersv4.2 → v4.3
The analyzer gained performance rules (EECS025 to EECS034). Code written for 4.2
compiles unchanged unless a system calls World.Update(), or the build treats warnings as errors.
New warnings: EECS025 to EECS032
Each names code that works but costs, with the fix in its section of the analyzer rules
and, where the change is mechanical, a code fix in the IDE. Apply the fix, or suppress the rule where the code is
deliberate, with the reason next to it. With a tracked Armor and a Seen counter:
public record struct Armor(int Value) : IComponent, ITrackChanges;
public record struct Seen(int Count) : IComponent;#pragma warning disable EECS026 // the write access is the point: it stamps the rows a [Changed<T>] system reacts to
[ForEach] static void Touch(ref Armor armor, ref Seen seen) => seen.Count++;
#pragma warning restore EECS026Or turn a rule down for a whole project in .editorconfig:
dotnet_diagnostic.EECS028.severity = suggestionWorld.Update() in a system is an error: EECS034
It ran the system again inside its own run. Run updates and groups from the code that hosts the world.
4.2
public class CatchUp : EcsSystem
{
protected override void OnUpdate() => World.Update();
}4.3
while (running) world.Update(); // in the host loopThe suggestion EECS033
EECS033 shows in the IDE only and never fails a build: the generated member is the same call at the same cost, only
shorter. dotnet_diagnostic.EECS033.severity = warning in .editorconfig turns it into a warning.
v4.1 → v4.2
One behavior changed; code written for 4.1 compiles unchanged.
A missing singleton throws in [ForEach] methods
A [ForEach] singleton parameter resolves through world.SingletonRef<T>(), which used to add the singleton with
its default value. Both now throw InvalidOperationException, like world.X and GetSingleton<T>(), on the
system's first run, even when no entity matches. The message names the fix:
Singleton Clock was not added to this world: set it before the systems that read it run
(world.SetClock(...) or world.AddSingleton<Clock>()), or use GetOrAddClock() where a default is meantAdd every singleton your systems read where the world is built, before the first Update(), also in tests. With
this singleton and system:
public record struct Clock(long Tick, float DeltaTime) : ISingletonComponent;
public class Ticker : EcsSystem
{
[ForEach] static void Advance(in Clock clock, ref Position position) => position.X += clock.DeltaTime;
}4.1: the first update added a default Clock, with no step.
var world = new World();
world.AddSystem<Ticker>();
world.Update();4.2: set it first, or call world.GetOrAddClock() where the default is right.
var world = new World();
world.SetClock(Tick: 0, DeltaTime: 1f / 30);
world.AddSystem<Ticker>();
world.Update();A world restored with Load already holds the singletons of its save; add only the ones the save did not hold. See
singletons in systems.
v4.0 → v4.1
A few APIs changed (listed first). Other code written for 4.0 compiles unchanged unless it stores Query results in
fields, uses them across await, captures them in lambdas, uses them in iterators or LINQ, or passes them as generic
arguments: they are ref structs now. The storage moved from one column per component type (indexed by entity id) to
archetype tables (one row per entity, columns 64-byte aligned), which changes a few behaviors.
Renamed and removed APIs
WorldOptions.ParallelisWorldOptions.ParallelSystems. The option runs systems side by side; the new name keeps it apart from[ForEach(Parallel = true)], which splits one method's rows over threads. Rename the initializer:new WorldOptions { ParallelSystems = true }.Entity.Idis anEntityId(index + incarnation, 8 bytes, unmanaged) instead of anint, and theEntity.Versionfield is gone: readentity.Id.Indexandentity.Id.Version.world.GetEntity,TryGetEntityandIsAlivetake anEntityId; a handle converts to one implicitly, soworld.GetEntity(entity)compiles. StoreEntityIds, notEntityhandles, in components: a handle holds its world, which makes the column managed (scanned by the GC, not copied as a block, not serializable), and the new warning EECS017 points at such fields.- The
World.SystemErrorevent is gone. A system's exception propagates out ofUpdate(the default), or, withnew WorldOptions { SystemErrors = SystemErrorPolicy.Disable }, switches the system off and stays insystem.Errorfor the application to read fromworld.Systems. Enable the system again to retry. - Systems need no
partial. The generated code lives outside the system's type, so EECS001 is gone;partialstill compiles. UpdateIntervaland the newUpdatePhaseshould be constants: they are read every update, and the warning EECS018 points at overrides that compute them. To stop and resume a system, setEnabled.world.DestroyEntity(entity)is gone: it did whatentity.Destroy()does. Callentity.Destroy(), orworld.DestroyEntities<T…>(filter)for every match at once.IForEachjobs receive only the components.Execute(int entityId, ref T1 c1, …)becameExecute(ref T1 c1, …), so a pass never loads the entity rows for them. A job that needs the entity implementsIForEachEntity<…>(Execute(Entity entity, ref T1 c1, …)) and runs withworld.ForEachEntity.CompactArchetypes()andGetFragmentationStats()are gone. Tables stay dense on their own, and passes now end even when a job throws, so there is nothing to compact and nothing to monitor (the ratio was always 0 between updates). Delete the calls.entity.Add<T>()is gone. It did whatSetandGetOrAdddo, and reset a component the entity already had, unlike adding in other ECS libraries. Write the generatedentity.SetX(…)(SetDead()for a tag) orentity.Set(new T(…))to add or replace a value, andref var c = ref entity.GetOrAdd<T>()to add a default value only when it is missing and fill it through the ref.world.Systemsis aSystemList(allocation-freeforeachandCount) instead of a newIReadOnlyList;foreachloops compile unchanged.QueryEnumerator.Count/IsEmptyare gone: useworld.Count/world.Anywith the same filter.entity.Remove<…>()returns the entity for chaining; calls that ignore it compile unchanged.RemoveSingleton<T>()returnsbool(false when the singleton was not added). Calls that ignore the result compile unchanged.
Behavior changes
- Stale and
defaulthandles answer questions with false.Has<T>()andTryGet<T>()used to throw for a destroyed entity (anddefault(Entity).Has<T>()threw aNullReferenceException); they now return false, likeIsAlive.Get,GetOrAdd,SetandRemovestill throw, nowInvalidOperationExceptionfordefaulthandles too. - Structural changes belong to the world's owner thread. 4.0 serialized creates, destroys,
Add/Set/Removeand singleton or system changes from any thread with a compare-and-swap lock. 4.1 checks a plain owner flag instead: a change made while another runs, or inside a parallel stage of systems or a parallel[ForEach], throwsInvalidOperationException; two threads of your own changing one world at once are not caught reliably. Make structural changes on the thread that callsUpdate(other threads hand them over as data through a queue a system drains: input from other threads), or give each thread its own world. Two analyzer errors point at such code: EECS013, a structural change in a[ForEach]method that takes noEntityorWorld(for example through a handle a component holds: take theEntityas a parameter), and EECS014, a structural change in a lambda handed toTask.Run,Parallel.*,ThreadPoolor aThreadthat races with other changes of the same world. - EECS007 and EECS008 are errors. Mutable state in a system (EECS007) and a system referencing a system (EECS008) were warnings in 4.0; code that triggers them no longer builds. Move the state into a component or singleton, and communicate through data. Every rule of 4.1 is an error except the warnings EECS017, EECS018 and EECS020 named above.
- Iteration semantics are stricter. A pass visits each entity at most once: entities created or moved into a
matching archetype during the pass are not visited by it (one that only gets a queried component back keeps its
row and is visited if the pass has not reached it); entities destroyed or losing a queried component before their
turn are skipped.
Without<T>filters that only existed to stop a job from revisiting what it produced are no longer needed (they still work). - Refs move more often. A
reffromentity.Get<T>()or aQueryitem can now be invalidated by a structural change of another entity (a row filled by a table's last row, a table that grows), not only by entity-capacity growth. The documented rule was already "a ref is valid until the next structural change"; code that relied on refs surviving changes must re-read throughentity.Get<T>(). Therefparameters of[ForEach]methods andForEachjobs stay writable when the body changes the visited entity's archetype. See refs and await. - Removing a component keeps the entity's row.
Has<T>(),Get<T>()and every query treat the component as absent; adding it back is a bit flip. Queryresults are ref structs.QueryEnumerator<…>andQueryItem<…>hold refs into the current table, so aforeachover a query cannot contain anawaitand an item cannot be stored. Read what you need, or keepitem.Entity, then await outside the loop.- Iteration order follows table rows, not entity ids, and can change when rows are moved or filled. Never rely on it.
EnsureEntityCapacity/InitialEntityCapacitypre-size the entity records only; archetype tables grow on their own.ParallelSystemskeeps subclasses it cannot see alone. A system class that derives from a generated system without adding[ForEach]methods of its own (for example to overrideOnUpdate) now runs alone (it never shares a stage with another system), as itsOnUpdatemay touch anything. Give the subclass a[ForEach]method (it is then generated) to run it beside others again.AddSingleton<T>()on an existing singleton resets it in place. It used to replace the storage, which leftSingletonRef<T>handles taken before pointing at a detached copy; they now see the reset value.Entity.GetHashCode()is deterministic (Index ^ (Version << 20)) instead ofHashCode.Combine(randomized per process). Equality is unchanged; only code that persisted hash codes across processes could notice.
New in 4.1
[ForEach]methods can takeSpan<T>/ReadOnlySpan<T>components (plus singletons) and receive whole runs of rows, for SIMD (EECS009 reports mixed signatures). The ad-hoc form isworld.ForEachBatchwith anIForEachBatch<T1..Tn>job.world.ForEachEntitywith anIForEachEntity<T1..Tn>job receives theEntityhandle instead of the id.INoStructuralChangesmarks a job that makes no structural change; the generator adds it to[ForEach]methods that cannot change structure, which then iterate without per-entity bookkeeping.QueryFilter.WithAny<T>()and[WithAny<T>]: the entity needs at least one of the named components.world.CreateEntities(span, components…)andworld.DestroyEntities<T…>(filter)spawn and clear waves with one pass through the structural gate.WorldOptions.MeasureSystemstimes every system run intosystem.Stats(SystemStats).[ForEach(Parallel = true)](andworld.ForEachParallel/ForEachEntityParallel) runs chunks ofWorld.ParallelChunkRowsrows on several threads; EECS010 reports methods that could race.world.Capture()/world.Restore(snapshot): in-memory snapshots of entities, components and singletons (snapshots).ITrackChanges,[Changed<T>],QueryFilter.Changed<T>(since)/Since(since),world.ChangeTick,system.LastRunTick: change tracking (change tracking). The job interfaces gained an optional staticWritesmember (with a default).[Removed<T>]andQueryFilter.Removed<T>(since): the entities that lost a tracked component since a tick (replication of what went away), with EECS016 for filters no row can pass.Prefabandworld.Instantiate(prefab)/Instantiate(prefab, span): entities from a recipe of components that belongs to no world, built withSet/Removeor the generatedprefab.SetX(…)/RemoveX()(chainable), andSetBuffer(…)/SetLoot(…)for the elements of aDynamicBuffer<T>;entity.Clone()copies a living entity.IIndexed<TKey>,world.Index<T, TKey>(),ValueIndex<T, TKey>: find entities by a component value; relations are indexedEntitykeys (indexes and relations).entity.Read<T>()returns aref readonly Tthat never counts as a write of a tracked component, andentity.TryGetRef<T>(out bool found)returns aref T(a null ref when missing): optional access without a copy.entity.Has<T1..Tn>()tests several components with one lookup.DynamicBuffer<T>: a list per entity as a component, written only throughref(each write is a tracked change), copied by snapshots (dynamic buffers).- The library is marked
IsAotCompatible: trimming and Native AOT analyzers run on every build, and it publishes and runs with Native AOT. entity.Disable<T>()/Enable<T>()(generatedDisableX()/EnableX()/IsXDisabled): switch a component off and keep its value; queries seeDisabled<T>.world.SetAll/RemoveAll/DisableAll/EnableAllchange every entity a filter matches, 64 rows at a time.ICleanupComponent: destroying an entity that holds one keeps it, taggedDestroyed, until a system releases what it holds and removes the last cleanup component.ChildOf(EntityId Parent)andworld.Children(parent): a hierarchy whose parents destroy their children;IRelationelements of aDynamicBuffer<T>(entity.AddRelation,world.Relations<T>()) link an entity to any number of targets, andTargetRulessay what happens to links when a target is destroyed.- Ordered walks of a value index with a comparable key (
Range,RangeDescending,Ascending,Descending), andworld.GroupBy<T>(), which splits tables by the value of anIGroupcomponent. Maybe<T>parameters of[ForEach]methods: an optional component without a second method.world.Save/world.Load(a world as bytes, through Nino),SaveEntities/LoadEntities/MoveEntities(a group of entities to another world or process,EntityIdfields rewritten),world.Capture(into)(reuses an earlier snapshot's arrays).world.RemoveTypes(assembly): unload game logic loaded into a collectibleAssemblyLoadContext; its systems, singletons, indexes and components leave the world, and the process forgets its types once it unloads.world.Describe()(tables, columns, sizes),entity.Describe()and a debugger view of entities.- Static
[ForEach]methods outside systems (a generated entry point takes theWorldand the method's other arguments: one pass for tools and tests), generic systems, filters on system groups ([With<T>]on a group struct applies to all its systems),UpdatePhaseto spread systems with the sameUpdateIntervalover updates,world.GetOrAddX()for singletons, named query items (item.Positionforitem.Component1), code fixes for the analyzer's diagnostics, and EECS024 for[ForEach]on a local function.
v3.x → v4.0
v4 targets .NET 10 only, moves the whole API into the EasyEcs namespace with standard ECS vocabulary, makes
systems synchronous and generates the repetitive parts (arity overloads, per-entity jobs).
Namespaces
EasyEcs.Core, EasyEcs.Core.Components and EasyEcs.Core.Systems become one namespace: using EasyEcs;.
Entities and components
v3 had three handle types (Entity value, EntityRef, ComponentRef<T>). v4 has one: Entity, a version-checked
16-byte handle. Component access returns ref T directly. Since 4.1 the generator also writes a named member for
each component (e.Position, e.SetPosition(…), e.HasPosition), which current code prefers over the generic
calls in this table (generated members).
| v3 | v4 |
|---|---|
Context | World |
EntityRef e = ctx.CreateEntity() | Entity e = world.CreateEntity() or world.CreateEntity(new Position(..), new Velocity(..)) |
e.Value.AddComponent<T>().Value.X = 1 | e.GetOrAdd<T>().X = 1 (ref T), or e.Set(new T(..)) |
e.Value.AddComponents<A, B>() | e.Set(new A(..), new B(..)) (add or overwrite values, one transition) |
e.Value.GetComponent<T>().Value | e.Get<T>() (ref T) |
e.Value.TryGetComponent<T>(out ComponentRef<T> r) | e.TryGet<T>(out T value) / ref var x = ref e.GetOrAdd<T>() |
e.Value.HasComponent<T>() | e.Has<T>() |
e.Value.RemoveComponent(s)<..>() | e.Remove<..>() |
ctx.DestroyEntity(e) | e.Destroy() |
ctx.GetEntityById(id) / TryGetEntityById | world.GetEntity(id) / TryGetEntity |
ctx.AllEntities | world.Entities |
ctx.GroupOf<T..>() → GroupResult | world.Query<T..>(filter) → QueryItem (item.Entity, item.Component1..n) |
ctx.CountOf<T..>() / AnyOf<T..>() | world.Count<T..>(filter) / world.Any<T..>(filter) |
new Context(new Context.Options(parallel, levels, capacity)) | new World(new WorldOptions { ParallelSystems = .., MaxDegreeOfParallelism = .., InitialEntityCapacity = .. }) (parallel is off by default) |
Singletons
| v3 | v4 |
|---|---|
AddSingletonComponent<T>() / GetSingletonComponent<T>().Value | AddSingleton<T>() / GetSingleton<T>() / GetOrAddSingleton<T>() (all ref T) |
TryGetSingletonComponent<T>(out SingletonComponentRef<T>) | TryGetSingleton<T>(out T value) |
Singletons are now per world (v3 shared them process-wide). A [ForEach] method takes a singleton as a
ref / in parameter (resolved once per pass), so systems rarely call GetSingleton themselves.
Systems
v3 systems implemented IInitSystem / IExecuteSystem / IEndSystem with UniTask methods. v4 systems derive
from EcsSystem and override synchronous hooks; the UniTask dependency is gone.
v3
public class S : SystemBase, IInitSystem, IExecuteSystem
{
public UniTask OnInit(Context ctx) { ...; return UniTask.CompletedTask; }
public UniTask OnExecute(Context ctx) { ...; return UniTask.CompletedTask; }
}v4
public partial class S : EcsSystem
{
// when added to the world
protected override void OnCreate() { }
// every update (World property available)
protected override void OnUpdate() { }
// per entity, after OnUpdate
[ForEach] static void Step(ref Position p, in Velocity v) => p = new Position(p.X + v.X, p.Y + v.Y);
}ctx.Init()is gone:OnCreateruns when the system is added.await ctx.Update()becomesworld.Update();await ctx.DisposeAsync()becomesworld.Dispose()(OnDestroyin reverse order).IExecuteSystem.ExecuteFrequencybecomesEcsSystem.UpdateInterval;AddSystem<T>()returns the system;GetSystem<T>()andEnabledwere added;RemoveSystem<T>()now returnsbool(whether a system was removed).- Exceptions:
world.SystemError(system, exception); without a subscriber the exception propagates. (4.1 replaced the event, see v4.0 → v4.1.)
Behavioral changes
- Entity ids are recycled.
Entity.Idis unique only among live entities; keep theEntityhandle (id + version). Equality includes the version. - Stale handles are rejected: access and structural changes throw;
Destroyof a stale handle is ignored. - Removing a component or destroying an entity releases managed references held by the component.
- Components and singletons must be structs: the analyzer reports EECS006 for classes.
- Never call
CompactArchetypes()while iterating (it rewrites rows). 4.1 removed it. - Systems should be stateless and independent: the analyzer warns about mutable fields on systems (EECS007: move the state into a component or singleton) and about systems referencing other systems (EECS008: communicate through data). See ECS principles.
IEvent,IEventHandler<T>andPublishare removed. Write state changes as data (a tag or a field) and let later systems query them.ParallelSystemsis safe by construction. v3 ran every system of one priority at once and left data races to you. v4 runs systems of one priority together only when their generated access does not conflict (ref= write,in= read); systems with anEntity/Worldparameter, instance[ForEach]methods, anOnUpdateoverride or no[ForEach]method run alone. The result equals a sequential update. See threading.
Source generator and analyzer
Both ship in the package (analyzers/dotnet/cs). A project that references EasyEcs.Core by ProjectReference
must also reference EasyEcs.Generator.csproj with OutputItemType="Analyzer" ReferenceOutputAssembly="false":
<ProjectReference Include="..\EasyEcs\src\EasyEcs.Generator\EasyEcs.Generator.csproj"
OutputItemType="Analyzer" ReferenceOutputAssembly="false" />v2.x → v3.0
v3.0 was a complete redesign around zero-allocation immediate execution for high-performance servers: callbacks and the command buffer are gone, every operation runs at once and returns its result. The code in this section is v2 and v3 code; none of it compiles against v4.
Breaking changes
No more callbacks. v2 deferred every operation and handed the result to a callback; v3 executes at once and returns it.
v2
context.CreateEntity(entity => {
entity.AddComponent<Position>(pos => {
pos.Value.X = 10;
entity.AddComponent<Velocity>(vel => {
vel.Value.X = 5;
});
});
});v3
var entity = context.CreateEntity();
var pos = entity.AddComponent<Position>();
pos.Value.X = 10;
var vel = entity.AddComponent<Velocity>();
vel.Value.X = 5;| v2 | v3 | Notes |
|---|---|---|
void CreateEntity(Action<EntityRef>) | EntityRef CreateEntity() | Returns the entity |
void AddComponent<T>(Entity, Action<ComponentRef<T>>) | ComponentRef<T> AddComponent<T>(Entity) | Returns the component ref |
void AddComponent<T>(Action<ComponentRef<T>>) | ComponentRef<T> AddComponent<T>() | On the Entity struct |
void AddSingletonComponent<T>(Action<...>) | SingletonComponentRef<T> AddSingletonComponent<T>() | Returns the ref |
void AddSystem<T>() | void AddSystem<T>() | Unchanged (immediate) |
void RemoveSystem<T>() | void RemoveSystem<T>() | Unchanged (immediate) |
Execution model. v2 queued operations into a command buffer, executed them at the end of the frame and invoked
the callbacks after. v3 executes every operation at once, thread-safe through the .NET 8 Lock, with no command
buffer and no callbacks.
Version tracking. v3 adds a version to the entity, incremented on destroy. EntityRef and ComponentRef
validate it on access and throw if the entity was destroyed.
public struct Entity
{
public readonly int Id;
public readonly int Version; // new: increments on destroy
// ...
}Migration steps
Target .NET 8 and C# 13
C# 13 is required for ref fields and other performance optimizations.
<TargetFramework>net6.0</TargetFramework>
<LangVersion>10</LangVersion><TargetFramework>net8.0</TargetFramework>
<LangVersion>13</LangVersion>Replace callbacks with return values
Entity creation, component addition, nested callbacks and the same in systems all become plain statements:
// v2
context.CreateEntity(entity => {
entity.AddComponent<Position>();
});
entity.AddComponent<Health>(health => {
health.Value.HP = 100;
});
// v3
var entity = context.CreateEntity();
entity.AddComponent<Position>();
var health = entity.AddComponent<Health>();
health.Value.HP = 100;Update singleton components
// v2
context.AddSingletonComponent<GameConfig>(config => {
config.Value.MaxPlayers = 100;
});
// v3
var config = context.AddSingletonComponent<GameConfig>();
config.Value.MaxPlayers = 100;Keep entity destruction, iteration and systems
context.DestroyEntity(entity) is the same in both versions, and still immediate. GroupOf iteration is
unchanged. Systems work the same way, with the immediate API inside:
public class MovementSystem : SystemBase, IExecuteSystem
{
public async UniTask OnExecute(Context context)
{
foreach (var result in context.GroupOf<Position, Velocity>())
{
ref var pos = ref result.Component1.Value;
ref var vel = ref result.Component2.Value;
pos.X += vel.X;
}
}
}Add and remove several components at once
New in v3.0: one archetype transition for several components, instead of one per call (two separate calls cost twice as much). Use them when you add or remove 2 or 3 components on one entity, build entities with several components, or change components often on a hot path.
var (pos, vel) = entity.AddComponents<Position, Velocity>();
pos.Value.X = 10;
vel.Value.X = 5;
var (pos, vel, health) = entity.AddComponents<Position, Velocity, Health>();
entity.RemoveComponents<Position, Velocity>();Modifying components and holding refs
Components are structs. Write through the ComponentRef (health.Value.HP = 100) or through
ref var h = ref health.Value; var h = health.Value copies the component, and the changes to the copy are lost.
A ref stays valid only until the component arrays are resized. Component data lives in
Context.Components[typeIndex][entityId], and only CreateEntity (when the capacity is exceeded) resizes those
arrays, so a ref held across CreateEntity may dangle and crash or read the wrong data. AddComponent,
RemoveComponent and DestroyEntity only change which archetype list an entity belongs to, and
CompactArchetypes only removes tombstones from the archetype lists: none of them moves component data, so refs
survive them (destroying the entity whose component a ref points at is still a logic error). Singletons are
stored apart and are not affected by CreateEntity or CompactArchetypes.
var health = entity.AddComponent<Health>();
ref var h = ref health.Value;
h.HP = 100; // safe
context.CreateEntity(); // may resize every component array
h.HP = 50; // unsafe: h may point to the old array
health.Value.HP = 50; // safe: the ComponentRef finds the current locationDuring GroupOf iteration, DestroyEntity, AddComponent and RemoveComponent are safe (tombstones handle
them); CreateEntity leaves the iterator working but invalidates ref locals, so read through
result.Component1.Value when the loop creates entities; CompactArchetypes breaks the iterator itself and must
run outside the loop. Rule of thumb: when unsure, write compRef.Value.field, and keep ref for short local code
that creates no entity.
Adding a component or a singleton the entity or context already has erases its data (the component is reinitialized to default). Check first:
if (!entity.TryGetComponent<Health>(out var health))
{
health = entity.AddComponent<Health>();
}
health.Value.HP = 100;Common patterns
// a test: no callback to wait for
var entity = context.CreateEntity();
Assert.GreaterOrEqual(entity.Value.Id, 0);
// an entity builder returns the entity
public EntityRef SpawnEnemy(Vector2 pos)
{
var entity = context.CreateEntity();
entity.AddComponent<Position>().Value = pos;
entity.AddComponent<Enemy>().Value.HP = 100;
entity.AddComponent<AI>();
return entity;
}
// initialization and conditions are plain statements
var inv = entity.AddComponent<Inventory>();
inv.Value.Capacity = 20;
inv.Value.Items = new List<Item>();
if (isPlayer) entity.AddComponent<PlayerControlled>();Thread safety
v2's command queue was thread-safe and the callbacks ran on the main thread at the end of the frame. In v3 every
operation is thread-safe through the .NET 8 Lock, entity id allocation is lock-free (Interlocked), and several
threads can create and modify entities at once:
Parallel.For(0, 1000, i => {
var entity = context.CreateEntity();
entity.AddComponent<Position>();
});v4.1 changed this: structural changes belong to the world's owner thread (see v4.0 → v4.1).
What changed for performance
v2 allocated a closure on every CreateEntity call and four on every AddComponent call, so a world that kept
creating entities kept producing garbage. v3 allocates nothing after warm-up: its arrays are allocated up front and
grow only once, with no closures. The rest of the hot path changed too:
| Operation | v2 | v3 |
|---|---|---|
| Tag bitwise operations | scalar operations | one SIMD instruction |
| Archetype queries | a scan of every archetype | a cached match |
| Bounds checks | on every access | removed behind invariants (Unsafe) |
For what EasyEcs measures today, see performance.
Iteration became safe to modify: v2 had undefined behavior when an entity changed during iteration (entities could be skipped or visited twice); v3 marks removed slots with a tombstone (-1) and reads a fresh span after a resize, so the behavior is predictable.
New in v3.0
- Cross-platform SIMD: AVX2 on x86 / x64, AdvSimd (NEON) on ARM, a scalar fallback elsewhere.
- Up to 65,536 component types (the component index became a
ushortinstead of abyte): the first 256 inline (32 bytes, twoVector128), the rest in an overflow array. - Archetype query cache: the first
GroupOfof a shape builds the cache in O(N), later ones look it up in O(1). - Manual defragmentation:
GetFragmentationStats()returns(totalSlots, alive, ratio), andCompactArchetypes()removes the tombstones (during loading screens, for example when the ratio passes 0.5). - Pre-allocation:
new Context(new Context.Options(parallel: true, levelOfParallelism: -1, initialEntityCapacity: 10_000))(-1uses all cores), andcontext.EnsureEntityCapacity(100_000)later.
Performance tips for v3
- Pre-allocate the entity capacity:
context.EnsureEntityCapacity(expected). - Check fragmentation and compact between levels, outside any
GroupOfloop:CompactArchetypes()invalidatesGroupOfiterators but not component refs. - Add and remove components with
AddComponents/RemoveComponents: one archetype transition instead of three. - Run systems in parallel:
new Context(new Context.Options(parallel: true)). - Create entities in a plain loop: the lock is taken once per operation.
- Modify components in place:
result.Component1.Value.X += 1, or areflocal when the loop creates no entity.
Troubleshooting v3
| Symptom | Cause | Fix |
|---|---|---|
| "Entity has been destroyed" exception | A stale EntityRef / ComponentRef used after the entity was destroyed; versioning detects it | Do not cache refs across frames; look the entity up again with context.TryGetEntityById(id, out var entity) |
| Tests fail after the upgrade | Assertions inside callbacks | Create the entity, then assert: var entity = context.CreateEntity(); Assert.NotNull(entity); |
| Component changes do not persist | The component value was copied to a local (structs) | Write health.Value.HP = 100, or through ref var h = ref health.Value |
Crash or corrupted data with ref | A ref held across CreateEntity, which may resize the arrays | Keep the ComponentRef and write through .Value; AddComponent, RemoveComponent and CompactArchetypes are safe with refs |
| Iteration behaves differently | v3 skips tombstones of entities added or removed during iteration | Usually nothing: iteration is safe now |
In short: remove every callback, use the returned values, and expect every operation to run at once, thread-safe and without allocations after warm-up. For questions, open an issue on GitHub.
Related
Last updated on
How is this guide?
Changelog
What changed in each EasyEcs release, newest first: new APIs, performance, fixes and the changes that need code changes.
Getting started
Build a small ECS simulation in C# step by step, from dotnet new console to components, tags, queries, systems, singletons, system groups and the update loop.