utl::free_list with stable storage #14

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

Why

The original free_list moved its items as bytes when it grew, which breaks any type that points into itself and invalidates references. The ECS, the renderer and the content system all keep references into free lists.

Decided

  • utl::vector is the engine's container (no std::vector switch).
  • utl::free_list keeps items in blocks that never move (8, 16, 32, ... slots), with a live bit per slot: any type can go in, references stay valid until removal, remove() of a dead id is refused in every build, contains(id) checks, the destructor and clear() destroy what's left.
  • Measured cost: lookups about 1.4 ns (0.2-0.5 before); a 6,400-item frame unchanged.

Read first

  • UnflavoredMeson/Primal#48.
  • Engine/Utilities/FreeList.h on master.

Steps

  • 1. Write tests: a type that points into itself survives growth; a reference stays valid across 1,000 adds; removing twice is refused; destructors run once.
  • 2. Watch them fail on the current free_list.
  • 3. Rewrite it with blocks and live bits until they pass.
  • 4. Run the tests under ASan.

Done when

  • The new tests pass, also under ASan/UBSan; 0 warnings both OSes; tidy 0.

Claude can help with

  • Reviewing the index-to-block arithmetic.

Learn

  • Why "trivially relocatable" isn't something you can assume for every type.
## Why The original `free_list` moved its items as bytes when it grew, which breaks any type that points into itself and invalidates references. The ECS, the renderer and the content system all keep references into free lists. ## Decided - `utl::vector` is the engine's container (no `std::vector` switch). - `utl::free_list` keeps items in blocks that never move (8, 16, 32, ... slots), with a live bit per slot: any type can go in, references stay valid until removal, `remove()` of a dead id is refused in every build, `contains(id)` checks, the destructor and `clear()` destroy what's left. - Measured cost: lookups about 1.4 ns (0.2-0.5 before); a 6,400-item frame unchanged. ## Read first - `UnflavoredMeson/Primal#48`. - `Engine/Utilities/FreeList.h` on `master`. ## Steps - [ ] 1. Write tests: a type that points into itself survives growth; a reference stays valid across 1,000 adds; removing twice is refused; destructors run once. - [ ] 2. Watch them fail on the current `free_list`. - [ ] 3. Rewrite it with blocks and live bits until they pass. - [ ] 4. Run the tests under ASan. ## Done when - [ ] The new tests pass, also under ASan/UBSan; 0 warnings both OSes; tidy 0. ## Claude can help with - Reviewing the index-to-block arithmetic. ## Learn - Why "trivially relocatable" isn't something you can assume for every type.
Sign in to join this conversation.
No assignees
No project
Blocks
#19 Archetype ECS
UnflavoredMeson/Primal_Plus_Plus
Depends on
#5 Unit tests with doctest
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#14
No description provided.