EasyEcs

Why EasyEcs is fast

The mechanisms behind EasyEcs' speed, each tied to a benchmark: aligned archetype columns, 16-byte entity records, presence bits, generated jobs and SIMD.

EasyEcs is fast because a server tick does the least work the data allows: it walks dense, aligned columns, skips 64 rows with one bit test, calls your code without a delegate, changes structure without a command buffer and allocates nothing. None of this needs you to write unusual code: a plain [ForEach] method gets all of it. Each section below names one mechanism, where it lives in the source and the benchmark that measures it.

A [ForEach] write, per entity

0.91ns
two plain arrays take 1.28 ns

[Changed<T>] over a full scan

73×
10 of 100,000 rows written since the last run

Allocated per server tick

0B
100,000 entities, after warm-up

The short answer

MechanismWhat it savesMeasured
Archetype tables, 64-byte-aligned columnsPointer chasing and split cache lines0.91 ns per entity, below two plain arrays (1.28 ns)
16-byte entity recordsA second lookup per handle accessMicroBenchmarks.HandleAccess
Presence bitsA row copy per state change517 μs against 1.00 ms for Friflo, which moves rows
Generated struct jobsA delegate call per row33.7 μs against 145 μs for a Query loop
Lock-free query cacheLocks and table searches per call21.6 ns per query call
Change-tick summariesVisiting rows nobody wrote525 ns against 38.6 μs for a scan
Immediate structural changesRecording and replaying a command bufferStructuralChangeComparison
Zero allocationGC pauses0 B per tick
SIMDOne instruction per row or per word1.20 μs to count 100,000 rows bit by bit
The parallel runnerWaking threads that do not pay884 μs against 1.68 ms

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

Archetype tables with aligned columns

Entities with the same set of components share a table (an archetype): one column per component type, one row per entity, structure of arrays. A system walks the columns it needs front to back, which is the access pattern hardware prefetchers and vector units handle best.

Table {Position, Velocity, Burning}

table + row

Meta[id]
16 B per entity

Slots: EntityId per row (8 B)

Position column
64-byte aligned

Velocity column
64-byte aligned

Burning column
+ presence bits

  • Unmanaged columns are pinned and aligned. A column of a type without references is a byte array on the pinned object heap whose row 0 starts on the first 64-byte boundary (Archetype.Aligned). A row of a power-of-two-sized component never straddles two cache lines, vector loads stay aligned, and the GC never scans or moves the column.
  • Columns are contiguous, not pages: a handle access is one indexed load. Paging would only save the copy when a table grows, which InitialEntityCapacity removes (D7).
  • Each row's entity is one 8-byte EntityId (version and index side by side), so a handle is built with one load.
  • Moves follow cached edges. Gaining a component the table has no column for copies the row to the target table along a transition cached per component bit (and per generic signature for several components), with a copy plan computed once per edge.

The aligned columns are why the generated job beats the same loop over two plain arrays:

Add Velocity to Position for 10,000 entities

RawArrayWrite is two plain arrays, the floor without an ECS. ForEachSystemWrite is a [ForEach] system, ForEachWrite a hand-written struct job.

RawArrayWrite1.01× · 0 B allocated12.8 μsForEachSystemWrite0.72× · 0 B allocated9.13 μsForEachWrite0.75× · 0 B allocated9.55 μs0.00 ns2.00 μs4.00 μs6.00 μs8.00 μs10.0 μs12.0 μsTime (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
CaseMeanvs baselineAllocated
RawArrayWrite12.8 μs1.01×0 B
ForEachSystemWrite9.13 μs0.72×0 B
ForEachWrite9.55 μs0.75×0 B

16-byte entity records

Everything the world keeps per entity id is one EntityMeta: its table, its version and its row, 16 bytes, so four fit in a 64-byte cache line (EntityMeta.cs). The component set is the table's mask, not a per-entity copy. entity.Get<T>() is a version check on that record, a column lookup in the table (bit to column, one small array whose 16-byte entries hold the column and its presence bits) and one indexed load.

  • Ids are recycled last in, first out: a freed id's cache lines are still warm when it comes back, and the records stay bounded by the peak number of live entities.
  • The records never shrink, which lets handles index them without bounds checks.
CaseMeanvs baselineAllocated
HandleAccess3.33 μs0.26×0 B
TryGetEntity3.60 μs0.28×0 B
TrackedHandleWrite8.37 μs0.66×0 B
SingletonAccess1.01 μs0.08×0 B
BenchmarkDotNet 0.15.2, .NET 10.0.13, Intel Xeon Processor 2.10GHz, 4 cores, Ubuntu 24.04.5 LTS.

Each row repeats its call 1,000 times.

Presence bits

Removing a component does not move the entity. The row keeps its place and clears one bit in the column's presence bits (64 rows per ulong); adding the component back sets the bit again (D6). Only gaining a component the table has no column for moves the row, so a state that comes and goes (stunned, burning, dead) costs a bit flip after the first time.

yes

no, first time

Remove<Stunned>()

clear the row's bit

the entity keeps its row

Set(new Stunned())

table has a
Stunned column?

set the row's bit

copy the row to
the table with one

Queries read the bits too. Each column counts its rows lacking the component, so per table a pass knows whether to skip it, walk every row with no test, or test bits, 64 rows per load. A rare state in a large table costs a pass over the words where it is set:

10 burning units among 100,000 in one table

Only presence bits tell them apart. The pass over the few skips the words without a burning row; the pass over the rest spans whole words.

PassOverTheFew0 B allocated386 nsQueryTheFew0 B allocated365 nsCountTheFew0 B allocated13.6 nsPassOverTheRest0 B allocated52.3 μs0.00 ns10.0 μs20.0 μs30.0 μs40.0 μs50.0 μsTime (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
CaseMeanvs baselineAllocated
PassOverTheFew386 ns—0 B
QueryTheFew365 ns—0 B
CountTheFew13.6 ns—0 B
PassOverTheRest52.3 μs—0 B

Archetype libraries without presence bits move the entity to another table and back:

Stun 5% of 100,000 fighters, then recover them

  • EasyEcs
  • Friflo.Engine.ECS
ToggleFriflo.Engine.ECS 1.94×517 μs · 0 B1.00 ms · 0 B0.00 ns200 μs400 μs600 μs800 μs1.00 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
EasyEcsToggle517 μs1.00×0 B
Friflo.Engine.ECSToggle1.00 ms1.94×0 B

Generated struct jobs instead of delegates

A [ForEach] method is compiled into a struct job by the source generator, and the world runs it through a generic method constrained to that struct: world.ForEach<Position, Velocity, MoveJob>(ref job, in filter). The JIT specializes the row loop for each job type and inlines your method into it. There is no delegate, no virtual call per row and no closure.

[ForEach] static void Move(ref Position p, in Velocity v)

generator

struct MoveJob : IForEach<Position, Velocity>
+ INoStructuralChanges

static readonly QueryFilter

access: writes Position, reads Velocity

world.ForEach<Position, Velocity, MoveJob>
specialized and inlined by the JIT

What the generator also decides for you:

  • INoStructuralChanges when the method is static, takes no World and uses its Entity only to read: the pass then skips the bookkeeping that keeps structural changes safe. A method that does change structure gets the safe loop.
  • Writes, from the ref parameters, so a structural change mid-pass carries exactly those writes to the entity's new row, and tracked components count as written only through ref.
  • The system's access (reads and writes per component and singleton), which decides which systems may share a parallel stage.
  • The filter as a static readonly field, built once.

The generated code reaches private methods through UnsafeAccessor, so systems need no partial and the call compiles to a direct call. A static [ForEach] method outside a system gets an entry point that takes the world: it runs as fast as the struct job it replaces, written by hand.

Count the wounded of 100,000 fighters outside a system

Runner: a static [ForEach] method's generated entry point. HandWrittenJob: the struct job it replaces. Query: a foreach loop.

Runner0 B allocated33.7 μsHandWrittenJob1.69× · 0 B allocated56.7 μsQuery4.3× · 0 B allocated145 μs0.00 ns20.0 μs40.0 μs60.0 μs80.0 μs100 μs120 μs140 μs160 μsTime (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
CaseMeanvs baselineAllocated
Runner33.7 μs1.00×0 B
HandWrittenJob56.7 μs1.69×0 B
Query145 μs4.3×0 B

Lock-free query cache snapshots

A query call does not search tables. The world keeps a cache keyed by the query's components and filter sets (required, excluded, any, changed, removed); each entry is an immutable snapshot of the matching tables and one row filter per table. When a new table appears, every matching entry is replaced copy-on-write and published with Volatile.Write, so readers never lock and never see a list being changed. In front of the cache, each call signature keeps a small memo of its first four filters, and a QueryFilter holds no object reference, so building one is a plain struct copy (D18).

hit

miss

copy-on-write,
Volatile.Write

world.Count<Npc>(filter)

memo of this
signature

entry: tables + row filters

cache dictionary

new table

The cost of a query call: counting a few entities

FilterAtCall builds the filter at the call; FilterOnce keeps it in a static field; NoFilter passes none; PastTheMemo uses a fifth filter, past the memo.

FilterAtCall0 B allocated74.8 nsFilterOnce0.29× · 0 B allocated21.6 nsNoFilter0.57× · 0 B allocated42.9 nsPastTheMemo1.18× · 0 B allocated88.0 ns0.00 ns20.0 ns40.0 ns60.0 ns80.0 nsTime (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
CaseMeanvs baselineAllocated
FilterAtCall74.8 ns1.00×0 B
FilterOnce21.6 ns0.29×0 B
NoFilter42.9 ns0.57×0 B
PastTheMemo88.0 ns1.18×0 B

Change-tick summaries per word and per block

A component marked ITrackChanges gets a hidden column of 8-byte ticks: each row's tick of its last write. On top of it the table keeps summaries (D23):

older than the pass

older

rest older

mixed

column: latest tick

skip the table

block of 4,096 rows:
latest tick + written words

skip the block

word of 64 rows: 32-byte summary
latest tick, its rows, bound of the rest

take the written rows from the summary

compare the row ticks, 4 per SIMD instruction

A [Changed<T>] pass skips a table nobody wrote with one load, then unwritten blocks, then unwritten words (four summaries per SIMD compare), and finds the next written word of a block with one bit scan. Removing a tracked component stamps the row's tick while the row clears its presence bit, so [Removed<T>] needs no extra storage, and its own block summaries keep writes of T from making it visit their words. Components without ITrackChanges have no column and no stamping code: the checks are JIT-time constants.

Replicate the 10 of 100,000 entities written since the last run

Changed=True: a [Changed<Health>] system or a Changed filter. Changed=False: visiting every row. Tick is a system, QueryTick an ad-hoc Query.

Tick (Changed=False)0 B allocated38.6 μsQueryTick (Changed=False)0 B allocated476 μsTick (Changed=True)0 B allocated525 nsQueryTick (Changed=True)0 B allocated720 ns0.00 ns100 μs200 μs300 μs400 μs500 μsTime (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
CaseChangedMeanvs baselineAllocated
TickFalse38.6 μs—0 B
QueryTickFalse476 μs—0 B
TickTrue525 ns—0 B
QueryTickTrue720 ns—0 B

Immediate structural changes

Adding, removing, creating and destroying apply at once, also in the middle of a pass over the same table (D2). Other libraries record such changes in a command buffer and replay them later, which costs a record per change and a second walk. EasyEcs pays only inside passes:

  • While a pass runs, a row that loses its entity becomes a tombstone (a negative id) instead of being filled, so rows never shift under the pass. The last pass to end closes the tombstones with vectorized scans.
  • A pass records each table's row count before it walks, so entities created or moved in during the pass are not visited: each entity is visited at most once.
  • Every change that can reallocate storage, move rows or flip presence bits bumps StorageVersion. The row loop checks it after each entity and re-reads its column refs only when it changed. Tables without tombstones run a row loop compiled without the tombstone check (D25).

Mark the dead and revive them during two passes over 100,000 units

  • EasyEcs
  • Other libraries
EasyEcs: Direct0 B allocated78.2 μsFriflo.Engine.ECS: CommandBuffer0.96× · 0 B allocated75.4 μsArch: CommandBuffer2.36× · 5.30 KB allocated184 μsDefaultEcs: CommandRecorder6.3× · 0 B allocated496 μs0.00 ns100 μs200 μs300 μs400 μs500 μsTime (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
EasyEcsDirect78.2 μs1.00×0 B
Friflo.Engine.ECSCommandBuffer75.4 μs0.96×0 B
ArchCommandBuffer184 μs2.36×5.30 KB
DefaultEcsCommandRecorder496 μs6.3×0 B

Zero allocation

A busy world allocates nothing per tick, so a long-running server has no GC pauses from the ECS. The rule is enforced, not hoped for: AllocationTests run a frame of updates, queries, structural changes, tracked removals, index churn, prefab waves, relations, cascades and parallel passes 100 times after warm-up and require GC.GetAllocatedBytesForCurrentThread() to be unchanged, on every dotnet test.

What makes it hold:

  • Struct jobs and struct enumerators: no delegate, no closure, no boxing on any iteration path.
  • Pooled cold state: a Query walk's per-table bookkeeping lives in a QueryWalk pooled per thread and nesting level.
  • Caches built once: transition edges, query entries, prefab target tables and overflow masks are allocated when first needed, never on the steady path.
  • Bounded growth: tables double when full and never shrink, ids are recycled, buffers are reused.
  • The parallel runner uses reusable thread-pool work items, not Parallel.For (a closure and state per call).

The same tick in other libraries, with what each allocates per tick:

One server tick of 100,000 entities: EasyEcs vs Friflo, Arch and DefaultEcs

The same simulation ported to each library's fast path, with identical results checked in setup. Shorter is faster; the badge is the time relative to EasyEcs and the memory allocated per tick.

  • EasyEcs
  • Other libraries
EasyEcs 4.60 B per tick1.68 msFriflo.Engine.ECS 3.61.08× · 0 B per tick1.83 msArch 2.13.4× · 535 KB per tick5.76 msDefaultEcs 0.173.7× · 60.9 KB per tick6.24 ms0.00 ns1.00 ms2.00 ms3.00 ms4.00 ms5.00 ms6.00 msTime per tick

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

Data table
LibraryTimevs EasyEcsAllocated
EasyEcs 4.61.68 ms1.00×0 B
Friflo.Engine.ECS 3.61.83 ms1.08×0 B
Arch 2.15.76 ms3.4×535 KB
DefaultEcs 0.176.24 ms3.7×60.9 KB

AllocationTests once caught a lambda that captured locals in World.Update: the closure was allocated at method entry even when its branch was not taken. That is the kind of regression the test exists for.

SIMD

Vector instructions appear where they measure (D21):

WhereWhat is vectorized
Table matchingComponent masks are 256-bit Vector256 bitsets: one test per table for the first 256 component types
Count / Any on tables whose rows must be testedThe set bits of the row words, 8 words per step with AVX-512 (Vector512), else Vector<T>
Change filtersWord summaries 4 per compare; row ticks of mixed words 4 per instruction
Writes of a run of rowsOne vectorized fill of their ticks
Closing tombstonesRows read as longs and searched for negative ids
Your systemsBatch [ForEach] methods take spans of aligned columns, ready for Vector<T>
CaseMeanvs baselineAllocated
Count1.20 μs—0 B
BenchmarkDotNet 0.15.2, .NET 10.0.13, Intel Xeon Processor 2.10GHz, 4 cores, Ubuntu 24.04.5 LTS.

Movement over 100,000 entities: per entity and as a vectorized batch method

  • EasyEcs
  • Friflo.Engine.ECS
Iterate2Friflo.Engine.ECS 1.02×253 μs · 0 B257 μs · 0 BIterate2SimdFriflo.Engine.ECS 1.16×69.5 μs · 0 B80.7 μs · 0 B0.00 ns50.0 μs100 μs150 μs200 μs250 μsTime (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
EasyEcsIterate2253 μs1.00×0 B
Friflo.Engine.ECSIterate2257 μs1.02×0 B
EasyEcsIterate2Simd69.5 μs1.00×0 B
Friflo.Engine.ECSIterate2Simd80.7 μs1.16×0 B

The parallel runner

Parallel work runs on the calling thread plus up to MaxDegreeOfParallelism - 1 reusable IThreadPoolWorkItem helpers (D22). Two kinds of work use it:

  • Parallel stages: with WorldOptions.ParallelSystems, consecutive systems with no order between them and no conflicting access (from the generated access sets) form one stage and run at once.
  • [ForEach(Parallel = true)]: a method's rows run in chunks, a multiple of 64 rows (so presence-bit words never straddle chunks), about four per thread, between 1,024 and 16,384 rows.

yes

no

probe after 16..1,024 runs,
or when 1.5x slower

a fifth faster than the work

Update

helpers paid off
last runs?

caller + pool work items
take chunks

caller runs alone

Each job and each stage measures its own runs: helpers stay while they make a run at least a fifth faster than its work, and a job that runs alone probes again after a period that doubles from 16 to 1,024 runs. Waking a parked pool thread costs from a few to a few hundred microseconds, so a small pass between ticks runs alone instead of burning CPU. There is no spinning and no dedicated thread.

Steering 100,000 entities, one thread and parallel

Tick (Parallel=False)0 B allocated1.68 msEntityJob (Parallel=False)0 B allocated1.92 msTick (Parallel=True)0 B allocated884 μsEntityJob (Parallel=True)0 B allocated896 μs0.00 ns500 μs1.00 ms1.50 ms2.00 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
CaseParallelMeanvs baselineAllocated
TickFalse1.68 ms—0 B
EntityJobFalse1.92 ms—0 B
TickTrue884 μs—0 B
EntityJobTrue896 μs—0 B

Four independent systems, sequential and as one parallel stage

Tick (Parallel=False)0 B allocated420 μsTick (Parallel=True)0 B allocated295 μs0.00 ns100 μs200 μs300 μs400 μsTime (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
CaseParallelMeanvs baselineAllocated
TickFalse420 μs—0 B
TickTrue295 μs—0 B

The reference machine has 4 cores, and the parallel rows of this run carry a large error: read them as "faster on several cores", not as a precise speed-up.

What it costs

The same choices have prices, and they are deliberate:

  • Gaining a new component copies the row to another table. The first time a state appears on an entity it moves; after that it flips a bit.
  • Tables never shrink. Memory is bounded by the peak number of entities each table held.
  • One owner thread per world. Structural changes and component writes belong to it (D13); parallelism comes from parallel stages, parallel methods and one world per thread.
  • .NET 10 only (D15): no Unity, no older runtimes.
Edit on GitHub

Last updated on

How is this guide?

On this page