Status: done
Priority: 2
Size: S
Arch: yardstick
Phase: implement
Design ref: World and scale (Walking and terrain). docs/roadmap.md, sections 5.3 (rule 8, bound every cache) and 5.4; VB-097’s first criterion, moved here.
Goal: st.pathCache keeps one route for every pair of tiles ever walked, and it is saved with the state. In populous small at seed 7 it holds 8,212 routes at year 5 and 25,723 at year 40 (114,874 tiles, 1 MB of the save). On every day a tile’s walking cost changes, refreshRoutes tests every route: 91 ms over 30 game days at year 40. Bound the cache, as rule 8 asks, and measure what that saves. Blocked until VB-193 (the fixtures) is done.
Acceptance criteria:
small-y40 and medium-y60 first: entries, MB in the save, routeHit, routeSearch and routeRefresh per game day (VB-192), and ms per game day in refreshRoutes.ROUTE.cacheMax entries (start at 20,000, VB-097’s figure; tune by the hit rate), dropping the least recently used: a hit moves its entry to the end of the Map, and an insert past the bound drops the first. The order is the Map’s, so it survives a save; checkState asserts the bound.npm run verify passes.
Cost: one Map delete and set per hit; refreshRoutes then tests at most cacheMax routes a day.
Notes:3 Nov 2026 (VB-181, the plan of slice 0): written; the route cache was brought into slice 0 in the session that ran VB-181 (roadmap, section 5.4). Measured for the plan on saves of populous small at seed 7.
fixtures branch.small-y40 and medium-y60 fixtures over 30 days:
entries and encoded cache MB, whole-save JSON/gzip MB, route hits/searches/refresh visits per day,
hit rate, refresh milliseconds/day and simulation milliseconds/day.sim/routes; add ROUTE.cacheMax = 20000 and use Map insertion order as LRU.
Touch direct and reverse hits; evict the oldest entry on insertion. Trim legacy oversized caches on load
through their owner so the bound holds immediately. Save/load preserves Map order.checkState, and update VB-097’s moved criterion.Impact: reads/writes the existing st.pathCache in sim/routes; sim/saves calls its owner to trim a legacy cache.
No new simulation state or module. Each hit adds a Map delete/set; the existing invalidation pass is bounded.
small-y40 differed from fresh A* results (the first had equal cost but different tiles).
A one-day empty-cache probe did not diverge, but this does not establish deterministic continuation in general.medium-y60: 96.189% to 83.824% (12.365 points).
At 40,000: 91.368% (4.821 points below baseline), but serial total time rose about 8%.
Final bound: 60,000 on both maps; 93.530% hits (2.659 points below baseline), with about 3% total time overhead.
This retains the memory bound with fewer repeat searches; it is not claimed as an overall simulation speedup.routeEvict work counter counts the bounded insertion and legacy-load trimming at the owner.
Its tiny-fixture baseline is 0; the focused route tests exercise nonzero eviction, while tiny scaling checks assert none.
All previous work-counter baselines stay unchanged.pathCache (Map order) in founders/heirs, seeds 7/42, years 1/2/3: 12 entries.
World, pawn, household, farm, job, RNG, event and other fingerprints are unchanged; no public Sim API change.Both reference saves are populous, seed 7, from fixture source 4df576663f12ecd23f432413f1709d4f260c6f6d.
Their golden stamps are stale; exactly the same saved states were used before and after. Each window advances 30 days in daily calls.
The baseline is unchanged source 1fbac6b, extracted separately under /tmp; the production checkout was not reset.
The cache stays in format-1 saves. Old saves lack access history, so their existing Map order supplies the initial eviction order.
| Measure | small-y40 before | small-y40 after | medium-y60 before | medium-y60 after |
|---|---|---|---|---|
| Entries on load | 25,723 | 25,723 | 95,909 | 60,000 |
| Entries after 30 days | 27,650 | 27,650 | 103,324 | 59,717 |
| Encoded cache MB on load | 1.017 | 1.017 | 4.130 | 2.750 |
| Encoded cache MB after 30 days | 1.110 | 1.110 | 4.510 | 2.688 |
| Whole-save JSON MB after 30 days | 31.054 | 31.054 | 135.877 | 134.086 |
| Whole-save gzip MB after 30 days | 5.585 | 5.595 | 24.436 | 23.950 |
| Hit rate | 96.525% | 96.525% | 96.189% | 93.530% |
| Hits/day | 3,736.73 | 3,736.73 | 12,311.13 | 11,952.80 |
| Searches/day | 134.53 | 134.53 | 487.73 | 826.80 |
| Refresh visits/day | 26,106.63 | 26,106.63 | 100,725.90 | 59,963.00 |
| Capacity evictions/day | 0 | 0 | 0 | 810.93 |
| Live households at end | 183 | 183 | 618 | 618 |
| Living people at end | 725 | 725 | 2,509 | 2,509 |
Timing is diagnostic, not a hard guarantee. Serial medium runs measured refresh 19.07 -> 13.34 ms/day and total simulation 654.13 -> 674.47 ms/day. Refresh work and serialized cache size fall about 40%; refresh time falls about 30%, but the extra searches add about 3% to total simulation time in this sample. Smaller trial caps of 20,000 and 40,000 caused 2,066.37 and 1,101.80 searches/day respectively; 60,000 was chosen to avoid most of that cost while still bounding history growth.
Households, not only averages: on small-y40 all 183 households’ hungry-day deltas match (163 household-days total). On medium-y60 the total is 134 -> 133, but benefits are mixed: 7 households improve by 10 days in total, 6 worsen by 9 days; the other households are unchanged. Worst increase is household 526, 0 -> 4 hungry days; these are changed routes and work timing, not a hunger-balancing improvement. The per-household differences are retained in the measurement artifact. A bounded cache can choose a different route after eviction; this was explicitly allowed by the task. Saving and loading a run made with the new bound still must match its unbroken continuation.
Final quiet small-y40 timing: refresh 2.41 -> 4.25 ms/day; total simulation 144.76 -> 168.57 ms/day. A repeated quiet baseline measured refresh 2.52 ms/day and total 136.22 ms/day. Thus the small-map total sample is 16-24% higher with LRU. Its working set is below the bound, so it gets no entry-count reduction and pays LRU bookkeeping overhead; this is a material tradeoff, retained in the report rather than hidden. Wall timings vary and are diagnostic; this is a bounded-memory change, not a claim of faster simulation.
Reproduce: fetch fixtures, then node tools/route-bench.js small-y40 and node tools/route-bench.js medium-y60.
An optional third positional argument overrides cacheMax for a trial, e.g. node tools/route-bench.js medium-y60 30 40000.
Full measurements, trial capacities, source/fixture provenance and the 13 household differences:
docs/bench/route-cache-vb198.json.
Validation (6 November 2026): npm test: 68 scripts passed (139.6 s); npm run verify: 81 scripts passed (236.6 s).
Verify includes all three real Chromium checks, unchanged non-cache golden fingerprints, five invariant runs,
save/JSON/load continuation against unbroken runs, all four late fixtures and the history/scaling cost checks.
The sandbox blocked child-process tests on the first attempt; the final suites ran with execution permissions,
VB_TEST_JOBS=2, and verify used VB_CHROMIUM=/usr/bin/chromium VB_SOFTWARE_RENDER=1.
Rebuilt index.html; lint, build freshness, backlog consistency and whitespace checks pass.