village-builder

VB-226: Save within 0.5 s of main thread at medium-ref, after the folds

Status: done (2026-11-10) Priority: 2 Size: M Arch: yardstick Phase: implement Design ref: Balance loops and targets (Performance budget). docs/roadmap.md, sections 5.2 and 5.3 (rule 8); docs/bench/closure.md (VB-203). Goal: Kim’s save line (9 November, VB-203): under 0.5 s of the page’s main thread at medium-ref, counting encode and stringify; gzip may run in a worker and is reported beside it. On 9 November at 9f130e6 the save blocks the main thread 1.51 s at medium-ref (encode 0.95, stringify 0.56; 39.3 MB of JSON; gzip 0.58 s more), three times the line. Of the state’s 36.8 MB: the households’ ledgers 10.6 (VB-204 folds the routine 95% of their entries), the pawns 14.5 (their copied events 5.3, which VB-199 makes indexes; ancestry 1.4; attitudes 1.4), the record 7.9 (VB-199 folds the routine kinds), the path cache 1.2. By size alone the two folds should leave about 17 MB and about 0.65 s. Measure what is left once they land, and cut it to the line. Acceptance criteria:

Implementation, 10 November 2026

Impact note: only the save’s encoding changes (src/sim/saves.js, the codec); the live state’s shape, the simulation and the golden file do not. It reads every field of the state, as before, and writes none; no new coupling. tests/lib/fixtures.js reads a fixture of an older format the build still reads, as stale (so the format-1 fixtures stay usable until the nightly workflow remakes them).

Validation, 10 November 2026

Timing on this machine was noisy (load average 6 to 22 on 4 cores, other agents running): each figure is paired with the base (aea615a, the same fixtures, alternated runs) and given as a median with its range. “CPU” is this thread’s CPU time (process.threadCpuUsage), the main thread’s own work; “wall” includes waiting for a core.

part MB before encode stringify MB after encode stringify
whole state 18.60 313 140 16.55 127 124
st.pawns 10.32 177 69 8.34 46 53
pawn.rel 6.63 125 38 4.80 23 28
pawn.ties 1.70 17 9 1.56 8 12
pawn.events 0.39 8 0 0.39 4 0
pawn.anc, pawn.att 0.34, 0.34 2, 1 0, 0 same 1, 1 0, 0
st.record 3.69 31 16 3.69 16 14
st.pathCache 1.41 42 8 1.34 16 4
st.households 1.14 12 6 1.14 12 4
st.agents 0.93 13 5 0.93 8 4
st.jobs, st.jobsBy 0.58, 0.56 8, 4 4, 4 0.57, 0.56 7, 8 4, 0
st.villages 0.31 5 1 0.31 4 3
every other field under 0.3 each under 2 under 1      
  medium-ref before after small-y40 before after
JSON, MB 19.57 17.52 13.16 12.23
encode 287 (251-370) 114 (95-150) 167 (146-211) 66 (63-110)
stringify 143 (139-160) 136 (123-150) 84 (76-105) 72 (66-95)
encode + stringify 430 (391-514) [517] 252 (220-292) [275] 249 (230-306) [260] 137 (132-196) [141]
parse + decode 237 (214-293) [349] 161 (152-182) [227] 144 (138-170) [162] 106 (101-124) [115]