views: borrow the world in egress instead of copying it (#28)

views.Context held copies of scene, tokens, characters, reveals, encountered
and visions, so every mutation deep-copied each player's explored HashSet under
the global lock; playerView then copied its own PlayerVision again and greyFor
copied one per encountered entry. Context now holds six pointers
(contextOf/borrow), playerView/gmView read a ptr PlayerVision and a view builds
each player's visible-id set once per frame, so grey is a set test.

Measured on this build's types (debug, 6 players, 40 encountered entries, 40
world tokens, 16384 / 262144 explored cells): context construction 0.227 ms /
4.87 ms -> 0.008 us; one getOrDefault of a PlayerVision 28.8 us / 591 us -> gone;
GM egress pass 8.0 ms / 142.8 ms -> 0.79 ms / 0.80 ms; player pass 1.45 ms /
25.7 ms -> 0.32 ms. Behaviour is unchanged (leak/fault suites and tviews green);
tviews pins the borrow by identity, the six-pointer size and both sizes inside
the M5 7.5 ms budget.
This commit is contained in:
2026-11-15 14:26:11 +01:00
parent 8e045b0ac8
commit e9d5f6815a
3 changed files with 193 additions and 39 deletions
+6 -4
View File
@@ -162,10 +162,12 @@ proc allIds(): seq[string] =
app.tokens.mapIt(it.id) & app.characters.mapIt(it.id)
proc egressContext(): views.Context =
## A read-only borrow of the world and every player's sight state. The seqs and tables are shared
## by reference and only read before egressFrames returns, so no copy is needed.
views.Context(scene: app.scene, tokens: app.tokens, characters: app.characters,
reveals: app.reveals, encountered: app.encountered, visions: app.visions)
## Six addresses, no copies (#28): the App owns the world and every player's sight state, and the
## egress borrows them for the pass. Building this used to copy `visions` (a whole explored
## `HashSet` per player) and the two-level `encountered` table on every mutation, under the global
## lock; now it cannot depend on how much a player has explored. Caller holds the lock (4.3), so
## the borrow is one consistent state for the whole fan-out.
views.contextOf(app.scene, app.tokens, app.characters, app.reveals, app.encountered, app.visions)
proc pruneKnown() =
## A closed connection's `known` table must not outlive it.