Skip to content

fix(ecs): entity generations so a stale entity never resolves to a reused slot; bump 0.10.37 - #286

Open
krisnye wants to merge 2 commits into
mainfrom
krisnye/entity-id-generations
Open

krisnye wants to merge 2 commits into
mainfrom
krisnye/entity-id-generations

Conversation

@krisnye

@krisnye krisnye commented Nov 23, 2026

Copy link
Copy Markdown
Collaborator

Problem

createLocalIndexEntityLocationTable reused a deleted slot immediately (LIFO free list) and returned the same entity id, with nothing to tell the old and new occupant apart. Any code holding an Entity across a delete silently resolved to a different row. Coediting hit this three times: a delta deletes A then inserts B, B gets A's id, and a stale GUID→entity map entry points at B.

Design

Entity ids carry a generation, in the style of @adobe/ecs, packed into the existing 32-bit Entity:

bit 31..24   generation (8 bits, wraps)
bit 23..2    per-quadrant local index (22 bits, ~4M slots per quadrant)
bit  1..0    quadrant
  • A slot's generation advances when the slot is freed. locate/read/delete compare the id's generation to the slot's, so an id from before the free no longer resolves. Slots are still reused through the free list, so table memory is bounded by the peak live count.
  • Generation 0 ids are bit-identical to ids before this change. Entity-reference components persisted in columns, journal entries and ELT slot numbers therefore stay valid.
  • Wrap-around is accepted: after 256 frees of one slot an old id could match again.
  • Entity.none (-1) is still never issued: the allocator caps local indices below the reserved maximum.
  • Entity.toEntity gains an optional generation; Entity.generationOf is new. The layout is still defined only in persistence-sharing.ts.

Undo/redo and reconciling replay

Generations change two behaviours that previously worked by accident of LIFO reuse:

  • Undo of a delete / redo of an insert must give back the same id. TransactionInsertOperation records an optional entity; replay restores it at its own generation via EntityLocationTable.restore (returns false, and falls back to a fresh insert, if that slot is live in the replay base).
  • Rolling back a speculative insert (reconciling replay, roll-forward, immediate concurrency, transaction error rollback) must leave the allocator as if it never happened, otherwise peers diverge on ids. Those paths free with rewind, which does not advance the generation.

Persistence compatibility

The generation is stored in the top byte of the first word of each 8-byte entity-location slot (low 24 bits stay archetypeId + 1). A deleted slot is written as an empty entry carrying the generation its next occupant gets.

  • Files written before this change have 0 there, so they load as generation 0. No format-version bump, same file names and stride; ECS_SNAPSHOT_VERSION is unchanged and a core snapshot without generations loads every slot at generation 0.
  • Persisting the generation is not optional: entity references in persisted columns carry the generation, so loading everything at generation 0 would dangle every reference to a reused slot.
  • Archetype ids must stay below 2^24 - 1 (the router throws otherwise); the journal already stores them as u16.

Tests

  • Table: stale id fails after reuse in every quadrant; no growth under delete/create churn; LIFO reuse; wrap at 256; stale delete is ignored; snapshot round-trip and copy detachment; snapshot without generations loads at generation 0; rebuilt snapshot that exactly fills its capacity; rewind; restore (free list unlinking, past the high-water mark, refuses a live slot).
  • Layout: generation packing, generation-0 ids unchanged, none.
  • Store: stale entity no longer reads after reuse, across a snapshot, and for non-persistent entities.
  • Persistence (entity-generation-persistence.test.ts, node + chromium): a reused slot keeps its generation, a deleted slot keeps its next generation, and an entity reference to a generation-1 entity resolves after reload, each from a checkpoint and from journal replay only; generation-0 entries are written in the pre-generation byte format; a pre-generation file loads at generation 0. The six generation tests fail without the persistence change; the two compatibility tests pass either way.
  • The existing reconciling-determinism and undo/redo suites pass unchanged.

Performance

Measured on the compiled output (native ESM), table-level, against main's table:

main this PR
200k create + locate + update + delete, x20 114 ms 112 ms
4M delete + create + locate on one slot 75 ms 84 ms (~+2 ns per iteration)

Each slot adds one byte of generation storage.

Verification

pnpm run lint, pnpm run typecheck, pnpm run build and pnpm run test (every workspace package) all pass.

krisnye and others added 2 commits November 23, 2026 10:24
…m; bump 0.10.37

Co-Authored-By: OpenCode <noreply@opencode.ai>
Entity ids carry an 8-bit generation (top byte); local index is 22 bits.
Freed slots are reused with an advanced generation, so stale ids fail locate
without unbounded table growth. Persistence stores the generation in the top
byte of each entity-location slot; files without it load as generation 0.

Co-Authored-By: OpenCode <noreply@opencode.ai>

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant