village-builder

VB-198: The route cache bounded

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:

Plan (5 November 2026)

  1. Measure the existing cache on the unchanged 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.
  2. Keep route ownership in 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.
  3. Test eviction, direct and reverse recency, re-search after eviction, legacy saves and continued determinism; assert the bound in checkState, and update VB-097’s moved criterion.
  4. Repeat the same measurements; keep hit-rate loss within five percentage points, rebuild the page, check goldens and work counters, then run test and verify and record results here.

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.

Implementation decisions (6 November 2026)

Measured effects (6 November 2026)

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.