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:
medium-ref, after VB-204 and VB-199: the save’s MB and its encode and stringify ms by part (each top-level field of the state, and each pawn field), the median of three runs.src/sim/saves.js; tests/saves.js and tests/codec.js pass, and the fixtures are remade by the nightly workflow.medium-ref (tools/bench.js, the median of three runs), and parse + load still under 0.5 s; the MB before and after.CompressionStream); if VB-214 has not done so, this task does.npm run verify passes.
Notes:docs/bench/closure.md. Load at medium-ref measured 0.33 to 0.55 s over three runs: at its line, so it is watched here too.medium-ref from 39.3 to 27.8 MB of JSON (29% less), but its main-thread time only from about 904 to 777 ms (14%; paired runs, 576 to 1,053 ms in all). Encoding is not proportional to size: measure encode by part before assuming VB-199’s size cut halves it.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).
pawn.rel (relationship records, {mem: {kind: {v, t}}, band} in a Map per pawn: 48,820 records,
101,566 memories at medium-ref) was 200,000 of the 306,000 objects the save walked, and 0.16 of its 0.46 s.FORMAT = 2, READS = [1, 2]: format-1 saves and fixtures still load):
$) is returned as it is, not copied: the save shares those parts with the state, so it is written out
before the state changes (the page and every tool stringify it at once). Only the path to an odd value is copied.{"$kv": [k, v, k, v, ...]}, not {"$map": [[k, v], ...]} ($map is still read);encode(root, stop, pack) writes an object listed in pack as {"$as": name, "v": data}, and decode(data, revive) makes
it again; save packs each pawn’s rel into one flat array [other, band, n, kind, v, t, ...] (packRel, unpackRel). Packed
objects start at -1 in the walk’s counts, so neither pass looks them up.tests/saves.js); the record (3.7 MB, about 30 ms) is history the game shows.src/page/saves.js pipes the JSON through CompressionStream('gzip') (VB-214);
only encode and stringify run on it.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.
medium-ref (the format-1 fixture made at d196f68, loaded; median of three, CPU ms; each part encoded alone, so parts that hold
the pawns overlap: cenv holds people, which is pawns):| 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] |
tools/bench.js measures it (the fixture run a year in the same process, then one cold save and load; medium-ref, 18.2 MB of JSON
after, 20.3 before):
node tools/bench.js, three runs at 6a372f3: encode + stringify 389 ms (376 to 453), gzip 340 (in the page, CompressionStream, off
the main thread), parse + load 254 (217 to 467); medium-y5 393 (331 to 437). In docs/bench/history.jsonl and budget.md.measure('medium-ref'), three alternated pairs, a heavier load on the machine): before 1,323 (708 to
1,349) ms, after 639 (632 to 761) wall; with this thread’s CPU time beside it (a script doing what measure does): before 646 (644 to
665) CPU and 816 (769 to 954) wall, after 374 (367 to 429) CPU and 639 (568 to 704) wall; parse + load after 209 (195 to 220) CPU,
372 (365 to 462) wall.medium-ref is 0.25 s warm and 0.37 s cold (CPU), under the line; tools/bench.js gives
0.39 s on a lightly loaded machine, but 0.57 to 0.76 s of wall clock when the machine is oversubscribed (a residual of waiting, not work).node tools/save-times.js --fixtures medium-ref,small-y40, median of 3): medium-ref encode + stringify 315 + 169 = 484 ms
before, 133 + 145 = 278 after; gzip (CompressionStream, off the main thread) 976 before, 836 after; parse + decode 60 + 162 = 222 before,
45 + 95 = 140 after. small-y40: 174 + 104 = 278 before, 104 + 79 = 183 after.tests/codec.js adds: a plain tree comes back as it is, only the path to an odd value is copied, Maps are flat and format-1 Maps are read,
a packed object is made again and its sharing kept, an unknown packed name is refused, the error path is spelled out. tests/saves.js
passes (the runs saved with packed relationships go on as the unbroken ones), as do tests/browser-saves.js and the fixture tests on the
format-1 fixtures.npm test passes (86 scripts). npm run verify passes but for tests/history.js, which fails as at
aea615a (routeExpand, routeRefresh, walkTies, roundPairs, heirRank, recordFold, roundRank over their limits; not this task’s, baseline
not raised). Counters unchanged (the simulation does not change). No household is better or worse off: the run is the same.pawn.rel (4.8 MB) is mostly game-hour
floats such as 13813.000000032187, which VB-235’s integer clock should shorten; then the record’s texts (3.7 MB).