Archetype ECS #19

Open
UnflavoredMeson/Primal_Plus_Plus
opened 2026-11-24 20:15:53 +00:00 by UnflavoredMeson · 0 comments
Owner

Why

Arash's entity system stores components in per-type arrays indexed by entity. Iterating many entities with several components, running systems in parallel, and knowing what changed all need an ECS. Actors (S8) and the renderer (S6) are built on it.

Decided

  • An own archetype ECS, no library: EnTT and flecs were measured against an own prototype (Ecs sections 1-5).
  • 16 KB chunks with one array per component; queries; per-chunk, per-component change versions; per-thread command queues applied at the sync point.
  • Systems: world.add_system<const A, B>(name, group, fn), scheduled on primal.jobs by declared read/write access; const T means read-only.
  • Components are plain data (trivially copyable, alignment <= 16). At most 64 component types (u64 masks). Entity ids stay 32 bits (24-bit index, 8-bit generation).
  • The typed API is a header (Engine/ECS/Ecs.h), the type-erased core a .cpp (World.cpp), because game code and headers use the templates.
  • Scripts stay objects next to the ECS and reach components through the entity (the Unreal Mass model).

Read first

  • Ecs, sections 3, 5 and 7.
  • Engine/Components/Entity.cpp, Transform.cpp on master.

Steps

  • 1. Write UnitTests/EcsTests.cpp: create/remove, add/remove a component (entity moves archetype), queries over two archetypes, change versions, queued changes applied at the sync point.
  • 2. Write the chunk storage and the archetype graph until they pass.
  • 3. Add systems and their scheduling by access; test that two writers of the same component never run at once.
  • 4. Make game_entity::world() the game world, its ids the entity ids.
  • 5. Write a benchmark: case iterating 100,000 entities with three components.

Done when

  • EcsTests pass, also under TSan and ASan; the benchmark number is in this issue.
  • 0 warnings both OSes; tidy 0.

Claude can help with

  • Reviewing the archetype move (the bug-prone part: swap-remove inside a chunk).
  • Reading the fork's benchmark harness with you, if you want to compare.

Learn

  • Why archetypes iterate faster than sparse sets and pay for it on structural changes.
## Why Arash's entity system stores components in per-type arrays indexed by entity. Iterating many entities with several components, running systems in parallel, and knowing what changed all need an ECS. Actors (S8) and the renderer (S6) are built on it. ## Decided - An own archetype ECS, no library: EnTT and flecs were measured against an own prototype ([Ecs](https://forgejo.unflavoredmeson.com/UnflavoredMeson/Primal_Plus_Plus/wiki/Ecs) sections 1-5). - 16 KB chunks with one array per component; queries; per-chunk, per-component change versions; per-thread command queues applied at the sync point. - Systems: `world.add_system<const A, B>(name, group, fn)`, scheduled on `primal.jobs` by declared read/write access; `const T` means read-only. - Components are plain data (trivially copyable, alignment <= 16). At most 64 component types (u64 masks). Entity ids stay 32 bits (24-bit index, 8-bit generation). - The typed API is a header (`Engine/ECS/Ecs.h`), the type-erased core a `.cpp` (`World.cpp`), because game code and headers use the templates. - Scripts stay objects next to the ECS and reach components through the entity (the Unreal Mass model). ## Read first - [Ecs](https://forgejo.unflavoredmeson.com/UnflavoredMeson/Primal_Plus_Plus/wiki/Ecs), sections 3, 5 and 7. - `Engine/Components/Entity.cpp`, `Transform.cpp` on `master`. ## Steps - [ ] 1. Write `UnitTests/EcsTests.cpp`: create/remove, add/remove a component (entity moves archetype), queries over two archetypes, change versions, queued changes applied at the sync point. - [ ] 2. Write the chunk storage and the archetype graph until they pass. - [ ] 3. Add systems and their scheduling by access; test that two writers of the same component never run at once. - [ ] 4. Make `game_entity::world()` the game world, its ids the entity ids. - [ ] 5. Write a `benchmark:` case iterating 100,000 entities with three components. ## Done when - [ ] EcsTests pass, also under TSan and ASan; the benchmark number is in this issue. - [ ] 0 warnings both OSes; tidy 0. ## Claude can help with - Reviewing the archetype move (the bug-prone part: swap-remove inside a chunk). - Reading the fork's benchmark harness with you, if you want to compare. ## Learn - Why archetypes iterate faster than sparse sets and pay for it on structural changes.
Sign in to join this conversation.
No assignees
No project
Blocks
#20 Transforms as components with a hierarchy
UnflavoredMeson/Primal_Plus_Plus
#23 Render components
UnflavoredMeson/Primal_Plus_Plus
Depends on
#14 utl::free_list with stable storage
UnflavoredMeson/Primal_Plus_Plus
Notifications
1 participant
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
UnflavoredMeson/Primal_Plus_Plus#19
No description provided.