test: load-insensitive tviews perf gate; env note

This commit is contained in:
2026-11-20 22:44:55 +01:00
parent 414bb0326c
commit 0448002cc5
4 changed files with 106 additions and 40 deletions
+16 -1
View File
@@ -24,7 +24,12 @@ Evidence roots (relative to repo root):
- `prototype-tile-leak/` (tile size vs leak; README tables and the 2026-11-08 phone run)
How to run: `devbox run test` = Nim in-process tests first, then the Node black-box harnesses;
`devbox run mutation` = the mutation suite.
`devbox run mutation` = the mutation suite. Run everything from the repo root through `devbox run`.
**Do not export devbox's `LD_LIBRARY_PATH` into host tools or stale binaries.** The devbox store is
built against glibc 2.44 while this host (NixOS) runs 2.42, so a 2.44-built `libsqlite3` loaded by a
2.42 loader fails at load time (or loads a mismatched libc). Rebuild after a `devbox.lock` change
(`devbox run test`/`bench` do it on first use); never reuse a binary built before the change.
---
@@ -188,6 +193,16 @@ How to run: `devbox run test` = Nim in-process tests first, then the Node black-
caveat]. A real-network run (laptop over the intended HTTPS path, phone on LAN) is an M7 item.
- Load/perf regression gate: `bench` + concurrency numbers (this section and §4.3) compared with
the committed baselines; the 2x threshold and its [PROPOSED] status are in DESIGN §5.3.
- **In-process egress gate** (#28, `tests/tviews.nim`): wall-clock gates are load-sensitive, so every
asserted number is the *fastest* of 20 timed passes after a warm-up (min-of-N), never a mean: a pass
the OS descheduled is not the minimum, while a regression raises every pass. Stated budgets are
10 us for one `Context` build and 2 ms for one egress pass (M5's 7.5 ms is a whole-mutation p99, not
a per-pass mean; the #28 shape measured 0.79 ms GM / 0.32 ms player on the reference build, 0.5-1.7 ms
in the debug suite depending on CPU clocks). The gate allows a documented 2x around each budget
(20 us, 4 ms) for a saturated box. The load-insensitive explored-size invariant
`smallMs <= gmMs*3 + 0.5` (16384 vs 262144 cells) is kept on minima. Absolute averages are
*recorded*, not asserted, by `devbox run bench` (`results/bench.log`: GM egress pass 0.24 ms,
player 0.08 ms, release build).
### 5.4 Known limitations
+19 -14
View File
@@ -1,17 +1,22 @@
/home/davide/Projects/GDR/FFF_vtt-chore/server/worldmap.nim(18, 44) Warning: imported and not used: 'faults' [UnusedImport]
/home/davide/Projects/GDR/FFF_vtt-chore/server/vision.nim(31, 43) Warning: imported and not used: 'faults' [UnusedImport]
== prototype scene (60 walls, 8 tokens, radius 400, cell 32) ==
1 poly (jittered start positions): n=2000 avg=0.037ms worst=0.082ms
6 polys (one 6-token party): n=500 avg=0.211ms worst=1.462ms
1 poly + cellsTouched (scanline): n=2000 avg=0.048ms worst=0.100ms
6 polys + cellsTouched (scanline): n=300 avg=0.267ms worst=0.402ms
1 poly + cellsTouchedOld (prototype reference): n=500 avg=0.720ms worst=1.433ms
TARGET polygon+cells, prototype scene (prototype baseline 0.451): 0.048 ms vs <= 0.451 ms: PASS
1 poly (jittered start positions): n=2000 avg=0.030ms worst=0.546ms
6 polys (one 6-token party): n=500 avg=0.180ms worst=2.611ms
1 poly + cellsTouched (scanline): n=2000 avg=0.039ms worst=0.553ms
6 polys + cellsTouched (scanline): n=300 avg=0.214ms worst=0.908ms
1 poly + cellsTouchedOld (prototype reference): n=500 avg=0.537ms worst=1.708ms
TARGET polygon+cells, prototype scene (prototype baseline 0.451): 0.039 ms vs <= 0.451 ms: PASS
== 5k sparse: genScene(world=16384., n=5000, seed=1), avg walls in vision bbox 15.6 ==
1 poly, 5k sparse: n=2000 avg=0.083ms worst=0.144ms
1 poly + cellsTouched, 5k sparse: n=2000 avg=0.097ms worst=0.749ms
1 move check (pathClear, cap-length step), 5k sparse: n=20000 avg=0.009ms worst=0.231ms
TARGET polygon, 5k-wall scene: 0.083 ms vs <= 1.0 ms: PASS
1 poly, 5k sparse: n=2000 avg=0.064ms worst=0.524ms
1 poly + cellsTouched, 5k sparse: n=2000 avg=0.074ms worst=0.158ms
1 move check (pathClear, cap-length step), 5k sparse: n=20000 avg=0.008ms worst=0.578ms
TARGET polygon, 5k-wall scene: 0.064 ms vs <= 1.0 ms: PASS
== 5k dense: genScene(world=4096., n=5000, seed=1), avg walls in vision bbox 206.9 ==
1 poly, 5k dense: n=2000 avg=1.282ms worst=2.756ms
1 poly + cellsTouched, 5k dense: n=2000 avg=1.324ms worst=3.268ms
1 move check (pathClear, cap-length step), 5k dense: n=20000 avg=0.009ms worst=0.077ms
(sink 6023672)
1 poly, 5k dense: n=2000 avg=1.036ms worst=2.647ms
1 poly + cellsTouched, 5k dense: n=2000 avg=1.882ms worst=6.806ms
1 move check (pathClear, cap-length step), 5k dense: n=20000 avg=0.008ms worst=0.109ms
== egress (#28: 6 players x 40 encountered entries, 262144 explored cells) ==
GM egress pass (fresh diff base): n=200 avg=0.242ms worst=0.306ms
player egress pass (fresh diff base): n=200 avg=0.082ms worst=0.103ms
(sink 6038922)
+36 -23
View File
@@ -534,29 +534,42 @@ suite "egress borrows the world instead of copying it (#28)":
test "building a Context and egressing does not scale with the explored sets":
var small = bigStore(16384) # 128 x 128 cells: the v1 map at cell 32
var large = bigStore(262144) # 512 x 512 cells: a 16384 px map at cell 32
# A build is six addresses. Before the fix, one Context construction cost ~0.19 ms at 16384
# cells and ~4.44 ms at 262144 (the issue's measurement on this build's types).
var sink = 0
var t0 = getMonoTime()
for i in 0 ..< 1000: sink += large.borrow.pTokens[].len
let buildMs = float((getMonoTime() - t0).inNanoseconds) / 1e6 / 1000.0
echo &"#28 context build, 262144 cells x 6 players: {buildMs * 1000.0:.3f} us"
check buildMs < 0.01 # 10 us; a copy of one explored set is already ~0.55 ms
# Egress for 6 x 40 entries: the M5 p99 budget is 7.5 ms per mutation (DESIGN 13 M5).
t0 = getMonoTime()
for i in 0 ..< 20: sink += gmPass(large)
let gmMs = float((getMonoTime() - t0).inNanoseconds) / 1e6 / 20.0
t0 = getMonoTime()
for i in 0 ..< 20: sink += playerPass(large, Pid)
let playerMs = float((getMonoTime() - t0).inNanoseconds) / 1e6 / 20.0
echo &"#28 egress, 262144 cells x 6 players x 40 entries: GM {gmMs:.3f} ms, player {playerMs:.3f} ms"
check gmMs <= 7.5
check playerMs <= 7.5
# The same pass at the v1 size must be cheaper still, never a cliff: the cost follows the
# frames, not the explored cells (before the fix the two sizes differed by ~20x).
t0 = getMonoTime()
for i in 0 ..< 20: sink += gmPass(small)
let smallMs = float((getMonoTime() - t0).inNanoseconds) / 1e6 / 20.0
echo &"#28 egress, 16384 cells x 6 players x 40 entries: GM {smallMs:.3f} ms"
# Wall-clock gates are load-sensitive, so every number below is the *fastest* timed sample
# after a warm-up, never a mean: a sample the OS descheduled (or a parallel build on the same
# box) is slow and simply is not the minimum, while a real regression raises every sample. The
# absolute averages are recorded by `devbox run bench` (TESTING 5.3), not asserted here.
proc minPassMs(rounds, reps: int, f: proc()): float =
## `f` `reps` times per sample, `rounds` samples; returns the fastest sample per pass.
for i in 0 ..< rounds: f() # warm-up
result = 1e9
for r in 0 ..< rounds:
let t0 = getMonoTime()
for i in 0 ..< reps: f()
result = min(result, float((getMonoTime() - t0).inNanoseconds) / 1e6 / float(reps))
const BuildBudgetMs = 0.010 # 10 us for one Context construction
const PassBudgetMs = 2.0 # one egress pass, see below
const LoadTolerance = 2.0 # documented slack: the minimum still rises ~1.7x on a saturated box
# A build is six addresses. Before the fix, one Context construction cost ~0.19 ms at 16384
# cells and ~4.44 ms at 262144 (the issue's measurement on this build's types), so 2x the
# 10 us budget (20 us) still fails a copy regression by an order of magnitude.
let buildMs = minPassMs(5, 100, proc() = sink += large.borrow.pTokens[].len)
echo &"#28 context build, 262144 cells x 6 players: {buildMs * 1000.0:.3f} us (min of 5 x 100)"
check buildMs <= LoadTolerance * BuildBudgetMs
# Egress for 6 x 40 entries. Stated per-pass budget: 2 ms of the M5 7.5 ms whole-mutation p99
# (DESIGN 13 M5; the #28 shape measured GM 0.79 ms / player 0.32 ms on the reference run and
# ~1.7 ms in a debug build on a dev box). The gate allows 2x that (4 ms) because the *minimum*
# still rises on a saturated box; a 20x regression (tens of ms) fails by an order of magnitude.
# The absolute averages are recorded in results/bench.log (`devbox run bench`), not here.
let gmMs = minPassMs(20, 1, proc() = sink += gmPass(large))
let playerMs = minPassMs(20, 1, proc() = sink += playerPass(large, Pid))
echo &"#28 egress, 262144 cells x 6 players x 40 entries: GM {gmMs:.3f} ms, player {playerMs:.3f} ms (min of 20)"
check gmMs <= LoadTolerance * PassBudgetMs
check playerMs <= LoadTolerance * PassBudgetMs
# The same pass at the v1 size must be no dearer, never a cliff: the cost follows the frames,
# not the explored cells (before the fix the two sizes differed by ~20x). Both sides are
# minima, so the invariant holds under external load too.
let smallMs = minPassMs(20, 1, proc() = sink += gmPass(small))
echo &"#28 egress, 16384 cells x 6 players x 40 entries: GM {smallMs:.3f} ms (min of 20)"
check smallMs <= gmMs * 3.0 + 0.5
check sink >= 0
+35 -2
View File
@@ -1,8 +1,10 @@
## Vision microbench: devbox run bench (nim c -d:release -r tools/bench.nim; log: results/bench.log).
## Vision and egress microbench: devbox run bench (nim c -d:release -r tools/bench.nim; log: results/bench.log).
## No timing code lives in server/: this harness times from outside with O(1) accumulators (sum, worst), no sample arrays.
## Targets (DESIGN 5.3 / 13 M2, PROPOSED): polygon + cells <= 0.451 ms avg on the prototype scene; polygon < 1 ms avg on 5k walls.
import std/[json, monotimes, times, strformat, os, math]
## The egress rows record the absolute averages the in-process gate (tests/tviews.nim, TESTING 5.3) deliberately does not assert.
import std/[json, monotimes, times, strformat, os, math, sets, tables]
import geometry, moverules
import characters, props, tokens, views, vision, world
import scenegen
import ../tests/[refold, testutil]
@@ -72,5 +74,36 @@ proc big(label: string, world: float) =
if label == "5k sparse": target("polygon, 5k-wall scene", po, 1.0)
big("5k sparse", 16384)
big("5k dense", 4096)
echo "== egress (#28: 6 players x 40 encountered entries, 262144 explored cells) =="
proc egressStore(cells: int): ref ContextStore =
var tokens: seq[TokenRec]
for i in 0 ..< 6:
tokens.add TokenRec(id: "t" & $(i + 2), scene: 1, kind: "npc", descriptiveName: "t",
actualName: "T", pos: (400.0 + float(i) * 10, 400), owner: i + 2,
flags: %*{"vision": true}, props: newProps())
for e in 0 ..< 40:
tokens.add TokenRec(id: "e" & $e, scene: 1, kind: "npc", descriptiveName: "a thing",
actualName: "Thing", pos: (3000.0 + float(e) * 30, 3000), owner: 0,
flags: %*{}, props: newProps())
result = (ref ContextStore)(scene: SceneRec(id: 1, name: "Scene", w: 4096, h: 4096, tileSize: 32,
cell: 32, visionRadiusDefault: 400,
tokenRadiusDefault: 20, artVersion: 1, nextWallId: 1,
extra: newJObject()), tokens: tokens)
for pid in 2 .. 7:
var pv: PlayerVision
pv.refresh(result.scene, result.tokens, pid)
result.visions[pid] = pv
for k in 0 ..< cells: result.visions[pid].explored.incl k
result.encountered[pid] = initTable[string, EncounteredEntry]()
for e in 0 ..< 40:
result.encountered[pid]["e" & $e] =
EncounteredEntry(entityId: "e" & $e, descriptiveName: "a thing")
let egressLarge = egressStore(262144)
discard bench("GM egress pass (fresh diff base)", 200, proc(i: int) =
var known: Known
sink += egressFrames(Recipient(isGm: true, playerId: 1), egressLarge.borrow, known).len)
discard bench("player egress pass (fresh diff base)", 200, proc(i: int) =
var known: Known
sink += egressFrames(Recipient(isGm: false, playerId: 2), egressLarge.borrow, known).len)
echo "(sink ", sink, ")"
quit(if misses == 0: 0 else: 1)