Skip to content

Source-generated archetype & component registry #514

Closed
7 of 7 issues completed
Closed

Description

@nockawa

Summary

Replace the runtime reflection + lazy Touch() archetype/component registry build with a Roslyn source generator driven by a [ModuleInitializer] registration barrier. Breaking change to engine startup internals; taken now (pre-alpha, no back-compat obligation).

  • Design: claude/design/Ecs/SourceGeneratedRegistry/ (README + 01–04 — 04 is authoritative).
  • Isolated POC: scratchpad/reggenpoc/ — all mechanics green (real incremental generator, module-init barrier, collectible-ALC unload/R1, AOT-clean generated path).

Why — one change, three value streams

  • Reliability (this Epic): fixes the ~33%/run flaky-test race — concurrent InitializeArchetypes on the unsynchronized process-global ArchetypeRegistry (IndexOutOfRangeException in RebuildEntityMapsFromPersistedData + a false "cascade diamond"). The CLR module initializer is a run-once, thread-safe registration barrier that kills the race by construction.
  • Durability (LOG-06): the logical WAL wire carries the order-dependent dense ComponentTypeId; a crash→reopen with shifted registration order can silently mis-map post-checkpoint records → data loss. Fixed by wiring (routingId, slot) — both durable.
  • AOT (Native AOT readiness for Typhon.Engine #409): delivers Native AOT readiness for Typhon.Engine #409 acceptance-criteria §1 — source-generate component registration, killing blocker B2 (MakeGenericType/Activator for ComponentCollection<T>) + the schema-reflection cluster.
  • Pluggability (new capability): an engine-assigned per-DB archetype routing id lets two mutually-unaware assemblies compose into one database collision-free — the basis for a plugin/marketplace model.

Model (settled)

  • Zero author-set ids. Archetype identity = CLR type name (or optional [Archetype("Name")]); component identity = [Component("Name")] (existing).
  • EntityId = 48-bit per-archetype EntityKey + 16-bit per-DB archetype routing id (routing id monotonic, persisted in ArchetypeR1, embedded in EntityId; NextEntityKey is already per-archetype).
  • In-memory dense component handle unchanged on the hot path (O(1) _typeIdToSlot).
  • GetMetadata(ushort) moves from global-static to a per-engine MetaByRouting[].

Phases → Tasks

The 6 phases from 04 §9 are tracked as sub-issues below (each independently testable; Phase 1–2 fix the flakiness and LOG-06 before any codegen).

Relations

Sub-issues

Sub-issues

7 of 7 Issues completed
Loading 7 sub-issues

Activity

  1. added theissue type on Jul 18, 2026
  2. self-assigned this
    on Jul 18, 2026
  3. updated Priority, Area and Estimate L on Jul 18, 2026
  4. nockawa commented on Jul 18, 2026

    @nockawa
    CollaboratorAuthor

    Accepted implementation plan (whole feature, one branch, one PR)

    Branch: feature/514-source-generated-registry · Spec: claude/design/Ecs/SourceGeneratedRegistry/04-solution-design.md (authoritative)

    Decisions taken (ratified by design 04)

    • D1 — Zero author-set ids: remove Id from [Archetype(Id=N)]; routing id engine-assigned. Ripples to every [Archetype(400..)] declaration.
    • D2 — Delete Touch()/RunClassConstructor startup calls (~230 sites), not no-op shims.
    • D3 — EntityId 52/12 → 48/16 is a clean break (pre-alpha, no migration).
    • D4 — Archetype identity = CLR type full name, optional [Archetype("Name")] override.

    Acceptance criteria by phase (completion = all met)

    Phase 1 — EntityId 48/16 + per-DB routing id (§2/§6/§7.1)

    • EntityId EntityKey(48)|routingId(16) round-trips; inline shift sites routed through EntityId.
    • ArchetypeR1.RoutingId (ushort) persisted; engine-assigned per-DB monotonic/dense.
    • Per-engine MetaByRouting[]; GetMetadata(ushort) moved off global static (~32 sites).
    • Reopen matches persisted rows by name, restores routing id.
    • Face A killed (no global MaxArchetypeId sizing coupling).
    • Two declaring types compose into one DB (pluggability seed).

    Phase 2 — WAL wire (routingId, slot) (§7.2; LOG-06/WR-01)

    • Codec encodes slot (byte), not dense ComponentTypeId (Slot + CollectionDelta).
    • Recovery maps (routingId, slot) directly; _typeIdToSlot off replay path.
    • LOG-06 regression: WAL written under one decl order replays correctly under a shifted order.

    Phase 3 — Registrar API + registration lock (§4)

    • ArchetypeRegistry.Registrar single registration path, lock-guarded, atomic catalog build.
    • Cascade graph built once (not per-engine), via existing collision guard.
    • Race killed: full Engine suite green ≥25 consecutive parallel runs, mitigations removed.

    Phase 4 — Generator: per-component reflection-free builders (§3; #409 §1)

    • [Component]-driven CreateComponentBuilder().WithField<T>(...) emission; offsets one-time Marshal.OffsetOf.
    • Reflection CreateFromAccessor off the build path.
    • Generator wired into MonitoringDemo, ARPG.Shell, Workbench.Tests; shipped via Generators.Consumer.

    Phase 5 — Per-archetype records + [ModuleInitializer] barrier (§3/§5)

    • Per-archetype ArchetypePrebuilt records + module-init registrar.
    • Touch() deleted (~230 sites); Workbench → RunModuleConstructor post-ALC-load.
    • R1: collectible-ALC unload test — teardown keys on owning assembly; no EntityLink<CollectibleArch> leak.

    Phase 6 — Type→delegate dispatch (B2) + cascade diagnostic + AOT lock-in (§3.2/§8)

    • Generated dispatch table replaces MakeGenericType/Activator for ComponentCollection<T> (B2 gone).
    • Cascade cycle/diamond → build-time generator diagnostic (TPHxxxx).
    • <IsAotCompatible>true</IsAotCompatible>; correct overview/04-data.md. Ticks Native AOT readiness for Typhon.Engine #409 §1.

    Verification

    Per-AC affected tests → full Engine suite at each phase boundary → scripted ≥25× parallel-suite race proof → shifted-order WAL oracle → full-solution build incl. generator. Mandatory self-review + /review before done.

  5. moved this to In Progress in Typhonon Jul 18, 2026
  6. added 3 commits that reference this issue on Jul 19, 2026
  7. moved this from In Progress to Done in Typhonon Jul 19, 2026
  8. added 2 commits that reference this issue on Aug 8, 2026
  9. added 2 commits that reference this issue on Oct 19, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Fields

Priority

P1-High

Estimate

L

Area

Schema

Start date

None yet

Target date

None yet

Product

Engine

Projects

  • Status
    Done

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions