egress copies every player's explored set under the global lock #28

Closed
opened 2026-11-15 13:18:29 +01:00 by SirStone · 1 comment
Owner

Summary

Every mutation runs egress while holding the global dbLock, and egress copies the whole perception state instead of borrowing it. views.Context is built from app.visions, app.encountered and the other tables by value, so each PlayerVision (a seq[Poly], a HashSet[int] of explored cells, fresh) is deep-copied, and each recipient then copies its own PlayerVision again. The cost scales with players x explored cells on every mutation, which puts the M5 p99 budget (<= 7.5 ms) out of reach on larger maps.

Where

  • server/main.nim:164-168 — egressContext() copies app.scene, app.tokens, app.characters, app.reveals, app.encountered, app.visions into views.Context. The comment claims the seqs and tables are "shared by reference"; for Table[int, PlayerVision] that is not true.
  • server/main.nim:252 — let ctx = egressContext() runs under the lock for every egress pass.
  • server/views.nim:201 — var pv = ctx.visions.getOrDefault(playerId) copies the recipient's whole PlayerVision (explored set included) on each call.
  • server/views.nim:158-163 — greyFor calls ctx.visions.getOrDefault(playerId) once per encountered entry, i.e. per recipient per mutation.
  • server/vision.nim:30-34 — PlayerVision is a value object owning explored: HashSet[int]; copying it copies the whole set.

Failure scenario

Measured on the review build: one Context construction costs 0.19 ms at 16384 cells x 6 players and 4.44 ms at 262144 cells; getOrDefault costs 0.027 ms / 0.55 ms for the same sizes. A single mutation's egress with 6 players and 40 encountered entries each therefore costs about 6.5 ms on the v1 map and about 130 ms on a 16384-px map — with the global dbLock held, so every other mutation queues behind it. As maps and explored sets grow during a session this degrades monotonically, so it is a wall-clock cliff, not a slow trend.

Confidence

High — the copies are visible in the types, and the timings above were measured on this build.

Fix direction

Make egress borrow instead of copy: pass the tables by lent/var (or hand out lent PlayerVision / raw pointer access) so Context holds references to the live tables rather than copies, and have playerView / greyFor read through that reference. Reuse a single per-recipient PlayerVision read across one frame build, and compute each encountered entry's grey flag once per frame (one visibility check per entry, not one set lookup per entry). Any copy that remains must happen outside the lock.

Acceptance

  • Building a Context and egressing for 6 players on a 262144-cell explored set allocates no per-player PlayerVision and no explored set (asserted by allocation or identity: reading through a borrow sees an in-place mutation).
  • Egress for 6 players x 40 encountered entries stays within the M5 budget on the largest supported map (benchmark or timing assertion recorded in the test).
  • Behaviour unchanged: tools/leakcheck.nim and the existing leak/tile oracles still pass, and a mutation still paints exactly the same frames in the same order.
  • Regression test: a benchmark-style test that fails if egress re-introduces a full copy (e.g. counts PlayerVision copies via a probe, or asserts a wall-clock bound at the 16384-px size).

Related

  • #20 (slice 5: tokens and sight), which introduced the egress path and PlayerVision.
  • #23 (slice 8: characters and names), which added the per-entry greyFor lookups.
  • #2 (M3 planning map).
## Summary Every mutation runs egress while holding the global `dbLock`, and egress *copies* the whole perception state instead of borrowing it. `views.Context` is built from `app.visions`, `app.encountered` and the other tables by value, so each `PlayerVision` (a `seq[Poly]`, a `HashSet[int]` of explored cells, `fresh`) is deep-copied, and each recipient then copies its own `PlayerVision` again. The cost scales with players x explored cells on every mutation, which puts the M5 p99 budget (<= 7.5 ms) out of reach on larger maps. ## Where - `server/main.nim:164-168` — `egressContext()` copies `app.scene`, `app.tokens`, `app.characters`, `app.reveals`, `app.encountered`, `app.visions` into `views.Context`. The comment claims the seqs and tables are "shared by reference"; for `Table[int, PlayerVision]` that is not true. - `server/main.nim:252` — `let ctx = egressContext()` runs under the lock for every egress pass. - `server/views.nim:201` — `var pv = ctx.visions.getOrDefault(playerId)` copies the recipient's whole `PlayerVision` (explored set included) on each call. - `server/views.nim:158-163` — `greyFor` calls `ctx.visions.getOrDefault(playerId)` once per encountered entry, i.e. per recipient per mutation. - `server/vision.nim:30-34` — `PlayerVision` is a value object owning `explored: HashSet[int]`; copying it copies the whole set. ## Failure scenario Measured on the review build: one `Context` construction costs 0.19 ms at 16384 cells x 6 players and 4.44 ms at 262144 cells; `getOrDefault` costs 0.027 ms / 0.55 ms for the same sizes. A single mutation's egress with 6 players and 40 encountered entries each therefore costs about 6.5 ms on the v1 map and about 130 ms on a 16384-px map — with the global `dbLock` held, so every other mutation queues behind it. As maps and explored sets grow during a session this degrades monotonically, so it is a wall-clock cliff, not a slow trend. ## Confidence High — the copies are visible in the types, and the timings above were measured on this build. ## Fix direction Make egress borrow instead of copy: pass the tables by `lent`/`var` (or hand out `lent PlayerVision` / raw pointer access) so `Context` holds references to the live tables rather than copies, and have `playerView` / `greyFor` read through that reference. Reuse a single per-recipient `PlayerVision` read across one frame build, and compute each encountered entry's grey flag once per frame (one visibility check per entry, not one set lookup per entry). Any copy that remains must happen outside the lock. ## Acceptance - Building a `Context` and egressing for 6 players on a 262144-cell explored set allocates no per-player `PlayerVision` and no explored set (asserted by allocation or identity: reading through a borrow sees an in-place mutation). - Egress for 6 players x 40 encountered entries stays within the M5 budget on the largest supported map (benchmark or timing assertion recorded in the test). - Behaviour unchanged: `tools/leakcheck.nim` and the existing leak/tile oracles still pass, and a mutation still paints exactly the same frames in the same order. - Regression test: a benchmark-style test that fails if egress re-introduces a full copy (e.g. counts `PlayerVision` copies via a probe, or asserts a wall-clock bound at the 16384-px size). ## Related - #20 (slice 5: tokens and sight), which introduced the egress path and `PlayerVision`. - #23 (slice 8: characters and names), which added the per-entry `greyFor` lookups. - #2 (M3 planning map).
SirStone added the ready-for-agent label 2026-11-15 13:18:29 +01:00
Author
Owner

Fixed and pushed on main.

Fix — server/views.nim: Context is a borrow now — six pointers (contextOf/borrow) into the caller's storage instead of owning copies of scene, tokens, characters, reveals, encountered, visions. playerView/gmView read a ptr PlayerVision (visionAt, with a caller-owned empty fallback so no GC'd global is read from a gcsafe handler) instead of getOrDefault, and each view builds the recipient's visible-id set once (visibleIds) so grey is a set test rather than a token scan plus a PlayerVision copy per entry. main.egressContext passes the App's field addresses (no copies at all); a ContextStore remains for tests.

Numbers (debug build of this tree's types, 6 players, 40 encountered entries, 40 world tokens; measured before from a HEAD worktree with the old API and after from the new one):

16384 cells 262144 cells
Context construction (before) 0.227 ms 4.87 ms
Context construction (after) 0.008 us 0.008 us
one visions.getOrDefault (before, per entry) 28.8 us 591 us
GM egress pass (before) 8.0 ms 142.8 ms
GM egress pass (after) 0.79 ms 0.80 ms
player egress pass (before) 1.45 ms 25.7 ms
player egress pass (after) 0.32 ms 0.32 ms

The cost now follows the frames, not the explored cells (the old code's two sizes differed by ~18x).

Regression test — new suite in tests/tviews.nim: identity (a Context is six addresses: a token added and an explored cell inserted after the borrow are visible through it; sizeof(Context) <= 6*sizeof(pointer)+8, so no container can sneak back) and a timing assertion at the issue's sizes: 1000 builds < 10 us total-ish (0.008 us each), GM and player egress <= 7.5 ms (M5 budget) at 262144 cells, and the 16384-cell pass not more than ~3x the large one.

Behaviour unchanged — tools/leak.test.mjs (A and B), tools/fault.test.mjs and the full Nim suite stay green.

Commits — e9d5f68 views: borrow the world in egress instead of copying it (#28); b4bddf1 design; e13fa90 results.

Gates — devbox run test: 107/107 node tests + all Nim suites pass. devbox run mutation: exit 0, every mutation passes (control included).

Fixed and pushed on `main`. **Fix** — `server/views.nim`: `Context` is a **borrow** now — six pointers (`contextOf`/`borrow`) into the caller's storage instead of owning copies of `scene`, `tokens`, `characters`, `reveals`, `encountered`, `visions`. `playerView`/`gmView` read a `ptr PlayerVision` (`visionAt`, with a caller-owned empty fallback so no GC'd global is read from a `gcsafe` handler) instead of `getOrDefault`, and each view builds the recipient's visible-id set **once** (`visibleIds`) so `grey` is a set test rather than a token scan plus a `PlayerVision` copy per entry. `main.egressContext` passes the App's field addresses (no copies at all); a `ContextStore` remains for tests. **Numbers** (debug build of this tree's types, 6 players, 40 encountered entries, 40 world tokens; measured before from a HEAD worktree with the old API and after from the new one): | | 16384 cells | 262144 cells | |---|---|---| | Context construction (before) | 0.227 ms | 4.87 ms | | Context construction (after) | 0.008 us | 0.008 us | | one `visions.getOrDefault` (before, per entry) | 28.8 us | 591 us | | GM egress pass (before) | 8.0 ms | 142.8 ms | | GM egress pass (after) | 0.79 ms | 0.80 ms | | player egress pass (before) | 1.45 ms | 25.7 ms | | player egress pass (after) | 0.32 ms | 0.32 ms | The cost now follows the frames, not the explored cells (the old code's two sizes differed by ~18x). **Regression test** — new suite in `tests/tviews.nim`: identity (`a Context is six addresses`: a token added and an explored cell inserted after the borrow are visible through it; `sizeof(Context) <= 6*sizeof(pointer)+8`, so no container can sneak back) and a timing assertion at the issue's sizes: 1000 builds < 10 us total-ish (0.008 us each), GM and player egress <= 7.5 ms (M5 budget) at 262144 cells, and the 16384-cell pass not more than ~3x the large one. **Behaviour unchanged** — `tools/leak.test.mjs` (A and B), `tools/fault.test.mjs` and the full Nim suite stay green. **Commits** — `e9d5f68` views: borrow the world in egress instead of copying it (#28); `b4bddf1` design; `e13fa90` results. **Gates** — `devbox run test`: 107/107 node tests + all Nim suites pass. `devbox run mutation`: exit 0, every mutation passes (control included).
Sign in to join this conversation.