Status: done (2026-11-09)
Priority: 2
Size: M
Arch: yardstick
Phase: implement
Design ref: none (code structure). docs/roadmap.md, section 5.3 (rules 4, 5 and 6).
Goal: The largest line of the budget has no task. On 9 November, tools/bench.js reported “agents and walking (the rest of Sim.advance)” at 70 ms a game day at medium-ref (a whole game year, the mean of three runs, 68.5 to 71.9; 30% of the day), against its share of 60 ms (VB-203, docs/bench/closure.md); 57 ms at medium-y5. Spring and winter cost most (85 and 73 ms a day in one run). It is not one system: it is what is left of Sim.advance once the named systems are timed. It holds the 6-minute agent step (every living agent, every step: the walkStep counter) and whatever else is not wrapped. Name its parts, then cut the ones over the share VB-203 gives them.
Acceptance criteria:
medium-ref and small-y40, a whole game year each. A CPU profile (node --cpu-prof) splits the remainder into named parts. Each part over 5% of a game day gets a system name in tools/perf.js, so the bench reports it from then on, and the unnamed rest falls below 10% of a day.walkStep), asserted at tiny scale in tests/counters.json.medium-ref (tools/bench.js, whole year): for each part over it, a change by the rules for cost. For example: an agent with nothing to do until a known time is not stepped until then (rule 4); a per-tile or per-agent value read every step is compiled (rule 6); work that can wait is spread over steps (rule 5). An order-preserving change leaves the golden file unchanged. A change of behaviour re-records it and reports tools/sim.js before and after, at seeds 7 and 42.npm run verify passes.
Notes:medium-ref, the gap 10 ms. The 7 November numbers it was written from (55 at medium-y5, 223 at medium-y60) are history.node --cpu-prof, a whole year from each fixture at 1568441, with the fixtures of 9f130e6):
medium-ref, of a profiled day of 270 ms (advance 246): the remainder was 76 ms. Idle strolls (sim/jobs idleAbout and below) 38 ms,
14% of the day: companyFor about 13 (pair reads in opinionOf and standing for each idle pawn drawn), the walk’s route about 13
(route’s LRU touch, a delete and a set on every cache hit, was 70% of it), issue and walkTo the rest. The step loop’s own lines 16
ms (6%), its hottest line the died check that read every living pawn’s record every step; stepWork 10 (4%; withOxen built the
household’s team, with flatMap, filter and sort, every step a ploughman worked), moveAgent 8 (3%), onHour’s own loops 3 (1%).small-y40, of a profiled day of 125 ms (advance 116): remainder 37 ms; idle strolls 18 (15%), the loop 7 (6%), moveAgent 5, stepWork
5, onHour 2.%HasFastProperties): Sim.load built each object
one keyed store at a time, which V8 turns into a dictionary past a few dozen keys (a pawn has 35, the state 46). A seeded run’s objects
stay fast. So each read of st.pawns, st.agents or a pawn’s field in a loaded run (every fixture, every saved game) was a hash lookup.sim/agents owns the six-minute step (stepAgents, moved from sim/clock) and a cache, st.awake (non-enumerable like
st.idx, so never saved; src/owners.json): the ids, in order, of the agents that may act this hour, rebuilt at the hour’s first step
after onHour: those walking or working, and in working hours those with no task whose pause ends by the hour’s end. Between rebuilds only
walkTo and idle can make an agent act (VB-177 left every movement write in sim/agents), and both wake it; stillAtDeath takes it
out. An agent left out is one the old loop would have passed over, so the order of acts is the same. About half the living are awake.
The loop no longer reads the pawn record (no living pawn is ever dead there: deaths take the id out of st.living at once).withOxen asks herds.hasTeam (count the oxen, allocate nothing) instead of building the team; moveAgent indexes a path point
instead of destructuring it.fadeRelations copies the memories left to a new object instead of deleting keys, which left the record a dictionary that opinionOf
walks with for...in.sim/saves decode makes each object with all its keys at once ({...d}, or Object.fromEntries for a shared or escaped one) and then
fills in its values: loaded objects stay fast. Keys keep their order.tools/perf.js names agents: idle strolls (idleAbout) and agents: walking and working (stepAgents, timed once a step);
tools/bench.js splits the 60 ms share: 33, 23, and 4 for the rest of Sim.advance.VB.COUNTERS, tests/counters.json): walkAwake (agents walked by the hourly rebuild), idleStroll, companyWeighed (idle
pawns drawn as company), walkPoint (path points walked past); walkStep now counts the awake agents. Invariant: st.awake holds
increasing living ids and, for its hour, every agent that may act (checkState).tests/golden.json unchanged. The save after a whole year of small-y40, 30 days of small-y5 in 1.7-hour calls and
3 days of medium-ref is byte-identical to the base’s; at tiny scale every counter but walkStep is unchanged (240 to 114 per living
pawn-day).medium-ref, a whole year, tools/bench.js’s measure, three alternating paired runs (median, range): agents and walking 70.8 (67.0 to
71.4) ms a game day, now 51.6 (50.3 to 52.8): idle strolls 29.0 (27.7 to 29.2), walking and working 19.2 (19.1 to 20.2), the rest 3.4
(3.2 to 3.6), 1.9% of the day. The whole day 231.7 (220.3 to 235.2), now 182.9 (180.3 to 189.9): every system gained from the fast
objects. Longest step 96.2 (90.6 to 99.0), now 89.9 (89.1 to 97.5). Parse and load 385 ms (364 to 390), now 400 (397 to 406). One
node tools/bench.js run on a quiet machine: 169.7 ms a day, the agents’ three lines 25.8, 18.4 and 3.2.small-y40, a whole year, the same window and loader, three alternating pairs: agents and walking 42.1 (40.0 to 44.1), now 35.3 (33.8 to
35.7); the day 129.7 (123.4 to 133.9), now 112.8 (109.2 to 113.5).medium-ref walkStep 240 to 116.5, walkAwake 24.0, idleStroll 5.76, companyWeighed
10.49, walkPoint 11.54; small-y40 walkStep 240 to 109.3, walkAwake 24.0, idleStroll 5.74, companyWeighed 10.55, walkPoint
13.19. Every other counter identical to the base.companyFor and the
route cache’s LRU touch; dropping the touch would change which routes are evicted once the cache is full (60,000), so it is a change of
behaviour, not made here. The medium map’s route keys (from * N + to, N = 56,000) pass 2^31 and are heap numbers.npm test and npm run verify pass.