EasyEcs

Bulk changes

Set, remove, disable or enable one component on every entity a filter selects in one pass, for round resets, zone-wide statuses and waves, with 0 B allocated.

A round ends and every fighter is stunned; the stun lifts from everyone; a zone freezes; a wave of monsters spawns and is later cleared. Written entity by entity, each of these is a loop of structural changes. SetAll, RemoveAll, DisableAll and EnableAll change one component on every entity a filter selects in one pass through the structural gate, a whole 64-row word at a time where they can, with the same results as the loop: tracked components count as written, indexes and groups follow.

When to use it

Use it for

The same change on many entities at once: round resets, a status applied to or lifted from a whole team or zone, pausing a component everywhere (DisableAll), spawning and clearing waves (CreateEntities, DestroyEntities).

Skip it for

Changes whose value depends on each entity (damage from each unit's armor): that is a [ForEach] system writing through ref. A handful of entities you already hold: Set on each is as fast.

How to use it

Set and remove on every match

SetAll(value, filter) gives every matching entity the component, added or replaced; RemoveAll<T>(filter) removes it from every match that has it. Both return how many entities they changed.

public record struct Fighter(int Team) : IComponent;
public record struct Stun(int Ticks) : IComponent;

public static class Rounds
{
    public static readonly QueryFilter Fighters = QueryFilter.Create().With<Fighter>();
}
var fighters = new Entity[1_000];
world.CreateEntities(fighters, new Fighter(1), new Health(100));

int stunned = world.SetAll(new Stun(3), Rounds.Fighters);   // the round ends: everyone is stunned
int lifted = world.RemoveAll<Stun>();                        // the stun lifts from everyone

Without a filter, RemoveAll<T>() takes T from every entity that has it, and SetAll gives it to every alive entity, so name what to select (With<Fighter>()). Any QueryFilter works, including Without, WithAny, Changed and InGroup.

Disable and enable on every match

DisableAll<T>(filter) and EnableAll<T>(filter) are Disable<T>() and Enable<T>() on every match: values are kept and given back.

int paused = world.DisableAll<Health>(Rounds.Fighters);     // nobody regenerates during the cutscene
int resumed = world.EnableAll<Health>();                     // every kept value comes back

Spawn and clear waves

The structural counterparts for whole entities create a wave with one set of values and destroy every match:

public record struct Shot(int Damage) : IComponent;
var volley = new Entity[10_000];
world.CreateEntities(volley, new Shot(5), new Position(0, 0));   // one table, rows placed at once
int cleared = world.DestroyEntities<Shot>();                     // every shot, in one pass

world.Instantiate(prefab, span) spawns a wave from a prefab.

What the bulk calls keep

SetAllRemoveAllDisableAllEnableAll
Change trackinga write per rowa removal per row (tracked T)a removal per rowa write per row
Indexesre-indexedunlinkedunlinkedre-linked
Groupsmoved to the value's group
On a disabled Treplaced: enabled, with the new value
Cleanup componentsthe last one frees a retired entityrefused (throws)

All four are structural changes of the owner thread: call them between updates, from a system's OnUpdate, or from a [ForEach] method that takes an Entity or the World (a method that takes neither promises no structural change).

Full example

A round of a fight: 1,000 fighters are stunned and released, their regeneration paused for a cutscene and resumed, then a volley of shots is spawned and cleared, each with one call. A system heals the fighters that are neither stunned nor paused. Put the files in a console project that references EasyEcs (dotnet add package EasyEcs) and run it with dotnet run.

It prints:

stunned 1000: health 100
lifted 1000: health 102
paused 1000: health 102
resumed 1000: health 104
set while paused: health 109, still paused 0
shots cleared: 10000, entities left: 1000

The stunned update heals nobody (100), the paused one neither (102 stays 102). SetAll on the paused Regen gives it back with the new value at once: the next update heals 5, and no Disabled<Regen> is left.

How to use it fast

  • Let the column stay. After the first round, the stun's column stays in the fighters' table: SetAll and RemoveAll then flip presence bits, 64 rows per word, and no row moves.
  • Keep the filter in a static readonly field, like any hot filter.
  • One call instead of a loop. The bulk call enters the structural gate once and works table by table.

A round reset of 100,000 fighters (stun everyone, lift it) and a status disabled and enabled for all of them, in bulk and entity by entity (BulkBenchmark):

CaseMeanvs baselineAllocated
StunAndLift87.9 μs1.00×0 B
StunAndLiftEach1.68 ms19×0 B
DisableAndEnable22.9 μs0.26×0 B
DisableAndEnableEach2.27 ms26×0 B
BenchmarkDotNet 0.15.2, .NET 10.0.13, Intel Xeon Processor 2.10GHz, 4 cores, Ubuntu 24.04.5 LTS.

What it costs

  • Time: O(matching tables + rows). Rows that already have the column are a presence-bit flip; an entity that lacks the column moves once, to the table that has it.
  • Allocation: 0 B once the target tables exist.
  • SetAll counts first: a moved row can be visited again in its new table, so the count is taken before the change.

Pitfalls

MistakeWhat happensCaught by
A bulk call inside a [ForEach] method without Entity or WorldA structural change the pass did not plan forEECS013
A bulk call from another threadRaces with the owner threadEECS014
DisableAll of a cleanup component or ChildOfInvalidOperationExceptionAt run time
SetAll with a value that should differ per entityEvery entity gets the same valueUse a [ForEach] system
Set / Remove entity by entity over a queryThe same result, one structural change per entityEECS030
CreateEntity or Instantiate(prefab) in a counted loopOne creation per entity instead of one per waveEECS031: CreateEntities, Instantiate(prefab, span)

Compared with other libraries

Friflo applies an EntityBatch to a query's entities, Arch adds or removes by query (whole tables move), and DefaultEcs changes entities one by one. The same round reset of 100,000 fighters:

Stun 100,000 fighters, then lift the stun from everyone

  • EasyEcs
  • Other libraries
EasyEcs: SetAllRemoveAll0 B allocated78.7 μsFriflo.Engine.ECS: ApplyBatch154× · 4.00 MB allocated12.1 msArch: AddRemoveByQuery15× · 0 B allocated1.21 msDefaultEcs: PerEntity103× · 0 B allocated8.12 ms0.00 ns2.00 ms4.00 ms6.00 ms8.00 ms10.0 ms12.0 msTime (shorter is faster)

BenchmarkDotNet 0.15.2, .NET 10.0.13, Intel Xeon Processor 2.10GHz, 4 cores, Ubuntu 24.04.5 LTS.

Data table
LibraryCaseMeanvs EasyEcsAllocated
EasyEcsSetAllRemoveAll78.7 μs1.00×0 B
Friflo.Engine.ECSApplyBatch12.1 ms154×4.00 MB
ArchAddRemoveByQuery1.21 ms15×0 B
DefaultEcsPerEntity8.12 ms103×0 B

And a volley of 10,000 shots spawned beside 100,000 fighters, then cleared:

Spawn 10,000 shots, then destroy them all

  • EasyEcs
  • Other libraries
EasyEcs: CreateAndDestroyAll0 B allocated138 μsFriflo.Engine.ECS: PerEntity2.75× · 0 B allocated380 μsArch: CreateAndDestroyByQuery2.96× · 0 B allocated409 μsDefaultEcs: PerEntity9.7× · 313 KB allocated1.33 ms0.00 ns200 μs400 μs600 μs800 μs1.00 ms1.20 ms1.40 msTime (shorter is faster)

BenchmarkDotNet 0.15.2, .NET 10.0.13, Intel Xeon Processor 2.10GHz, 4 cores, Ubuntu 24.04.5 LTS.

Data table
LibraryCaseMeanvs EasyEcsAllocated
EasyEcsCreateAndDestroyAll138 μs1.00×0 B
Friflo.Engine.ECSPerEntity380 μs2.75×0 B
ArchCreateAndDestroyByQuery409 μs2.96×0 B
DefaultEcsPerEntity1.33 ms9.7×313 KB
Edit on GitHub

Last updated on

How is this guide?

On this page