Skip to content

Keep the Modern router's line costs and routes for the step - #2279

Merged
maniator merged 1 commit into
mainfrom
claude/project-thread-u5g4vi-jamperf
Nov 15, 2026
Merged

maniator merged 1 commit into
mainfrom
claude/project-thread-u5g4vi-jamperf

Conversation

@maniator

@maniator maniator commented Nov 15, 2026 •

Copy link
Copy Markdown
Owner

What & why

#2272 (riders stuck an hour in one line look for another way) made a Modern game day slower: about +15% on maxed Modern and +27% to +32% on the split tower, from the pre-4.1 perf check. This keeps the router's work for the step, with every route coming out as before:

  • Line costs kept. A search read each boarding's line from three maps (the line, the shaft, the cars in service) at every visit. Each line's cost is now worked out once and kept until a rider joins that line or the step's lines change (expected_costs::line_cost).
  • Routes kept. A route found since the lines last changed is reused when the same trip is asked again, as happens when riders in one line pass an hour one after another. A rider joining any line, or the next step's count, forgets them (StepCosts::joined, all_changed).

The owner chose to ship #2272 in 4.1 with this cache, and to follow up with further fixes that bring the split tower to no slower than before #2272.

Closes: n/a (no issue)

Type of change

  • Bug fix (non-breaking change that fixes an issue)
  • Classic-canon fix (matches the 1994 original for all towers)
  • Modern-mode feature (opt-in, "what the original couldn't do")
  • New player-facing feature (mode-agnostic)
  • Refactor / internal-only (no player-facing behavior change)
  • Docs / tooling

Game mode impact

  • This change is classic-canon (affects all towers, stays faithful to the 1994 original)
  • This change is modern-only (gated behind Modern mode, does not alter Classic behavior)
  • This change is mode-agnostic (identical in both modes)
  • Any mode-divergent behavior is isolated in the mode rule-set (resolved once at founding), not smeared into the simulation with ad-hoc mode checks

Only the Modern router uses these costs, and no route changes, so neither mode plays differently.

Quality gates

  • npm run typecheck (no TypeScript change)
  • npm run lint (no TypeScript change)
  • npm test (CI runs it)
  • npm run build (CI runs it)

Engine: cargo fmt --check, cargo clippy --all-targets --features tooling,conformance -D warnings and cargo test --release --features conformance pass. The native conformance run passes every scenario, the fuzz corpus replays with no golden moved, and npm run engine:compat leaves the compat record unchanged.

New tests in crowd/expected_costs_tests.rs:

  • a_kept_line_cost_is_the_cost_its_line_gives: every link's kept cost equals a fresh cost_of, after the step's count and after each rider joins.
  • a_rider_joining_a_line_forgets_the_kept_routes: 400 riders join the express's line one by one; each route equals a fresh search, and the local wins in the end.
  • a_route_asked_again_is_the_route_a_search_finds: every trip between 15 floors, asked, asked again and searched fresh, gives the same routes.

The first two fail when joined stops pricing a line again or stops forgetting the routes.

Versioning

  • Added a .changes note with the right app: and crate: levels, or this PR changes no deployed path
  • Engine change: updated packages/verticopolis-engine/README.md and/or engine-rs/CRATE_README.md, or read both and put readmes: unchanged in the note, or this PR changes no engine source

app: patch (the game runs faster) and crate: patch (engine source changes, no route, save, layout or interface does). readmes: unchanged.

Complexity

  • Hot paths touched: expected_time::search (every Modern route search), cheapest, join_line (every rider routed in Modern), stand_down (once a step).
  • Before and after: a boarding link's cost goes from three map lookups to one array read while its line is unchanged; one map lookup per join to mark its line. A repeated trip with no join in between costs one map lookup instead of a search. Kept routes are capped at 1,024 and forgotten at the next join, so memory stays at a few hundred KiB. No new scan.
  • Measurements (one game day from midnight on day 2, release build, instruction counts from callgrind, steady from run to run; the save load is in both):
Tower main before #2272 main after #2272 This PR
maxed Modern 6.43 G n/a 6.42 G
split tower 2.80 G 3.40 G (+21%) 3.03 G (+8%)

The split tower's remaining cost is the hour-long-wait searches themselves (about 2,500 a day that this cannot reuse). Wall time on the container is noisy; best of 5 over three rounds it reads about +6% on maxed Modern and +8% on the split tower against main before #2272.

Hot-path record (section 6): this comes with the review.

Screenshots / recordings

None. No route changes, so nothing on screen moves.

Notes for reviewers

  • Same routes. The search is a function of the graph, the lines and the cars in service; a kept cost or route is used only while none of those changed. The bound is unaffected (it never read the lines).
  • Tried and dropped: a radix heap for the open nodes and 16-byte heap entries both ran more instructions than the binary heap here.

🤖 Generated with Claude Code

https://claude.ai/code/session_01Ms7trYcRHkvnh6N2YZqqjw

The router read each boarding's line from three maps at every visit of
every search, and riders weighing another way after an hour in one line
asked the same route one after another. Each line's cost is now worked
out once until a rider joins it or the step's lines change, and a route
found since the lines last changed is reused. Routes come out the same:
the conformance lock and the fuzz corpus do not move.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Ms7trYcRHkvnh6N2YZqqjw
@vercel

vercel Bot commented Nov 15, 2026 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated
verticopolis Ready Ready Preview Nov 15, 2026 6:52am UTC

Request Review

@github-actions github-actions Bot added this to the 4.x milestone Nov 15, 2026
@github-actions

github-actions Bot commented Nov 15, 2026 •

Copy link
Copy Markdown
Contributor

The docs preview for this pull request was removed when it closed.

@github-actions

Copy link
Copy Markdown
Contributor

📸 Screenshot drift check

No drift: the committed screenshots already match a fresh deterministic generation. ✅

@maniator
maniator marked this pull request as ready for review November 15, 2026 10:20
@chatgpt-codex-connector

Copy link
Copy Markdown
Codex Review Summary: ❌ Failed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ❌ Failed 2026-11-15T10:20:13.897803Z
We couldn’t complete this review. Comment @codex review to try again.
806ec2a Draft marked ready
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector

Copy link
Copy Markdown

You have reached your Codex usage limits for code reviews. You can see your limits in the Codex usage dashboard.
To continue using code reviews, you can upgrade your account or add credits to your account and enable them for code reviews in your settings.

@maniator
maniator merged commit 8020615 into main Nov 15, 2026
51 of 52 checks passed
@maniator
maniator deleted the claude/project-thread-u5g4vi-jamperf branch November 15, 2026 10:24

Copy link
Copy Markdown
Owner Author

Independent review at 806ec2a: GO (recorded after merge)

This PR merged while the review was running, at the same commit I reviewed (806ec2a). Nothing here asks for a change.

Review: stand-in-pr-review plus a lean /gds-code-review pass (Blind Hunter, Edge Case Hunter, Acceptance Auditor, Hot-Path Auditor, triage).

The claim: no route changes

Checked statically, then by a scratch probe (not committed).

Statically. A Board link's cost reads three things: waiting[(shaft,floor)], shafts[shaft] and running[shaft]. Each has one writer, and each writer now invalidates the cache:

  • fresh() on a tower revision builds a new router, so the costs, line numbers and routes all start empty.
  • fresh() on a step change swaps waiting and calls stand_down, which calls all_changed.
  • join_line changes one line's riders and calls joined, which re-prices that line and forgets every route.

I found no other path that writes those inputs. The changes counter starts at 1 and wraps with .max(1), so a fresh slot (0) is never read as current. The bound (expected_bound.rs) never read the lines, so the bounds stay exact.

The probe. I added a kill switch that makes line_cost call cost_of directly and skips the kept-route lookup. I then ran the owner's tower, the split-16 tower and the maxed Modern tower from 6 AM to 9 PM, cache on and cache off, and compared serialize() every hour.

Tower Saves identical every hour, 6 AM to 9 PM
owner yes
split-16 yes
maxed Modern yes

Timing, 6 AM to 6 PM. Release build, best of 5 per run, two runs each:

Tower Cache on Cache off
owner 508 / 510 ms 541 / 536 ms
split-16 350 / 361 ms 355 / 359 ms
maxed Modern 1183 / 1152 ms 1150 / 1248 ms

The owner's tower is about 5% faster. The other two are within the container's noise, and neither is consistently slower. This only compares the cache against no cache at the same commit. The remaining cost of #2272 against 4.0 on the split tower (+8% by the PR's callgrind counts) is the follow-up the owner already accepted for 4.1.

Gates at 806ec2a: cargo fmt --check passes, cargo clippy --release --all-targets -D warnings passes, and cargo test --release --lib passes 1894 of 1894.

Findings

  • patch: none.
  • note, no action: joined forgets every kept route on any rider's join, so a kept route only helps when the same trip is asked again with no join in between (riders who pass an hour in the same line, one after another). That is the conservative choice, and it is what makes the equivalence easy to prove. A per-line route index could keep more routes later, but it is not needed for correctness.
  • note, no action: the tests in expected_costs_tests.rs compare against a fresh cost_of and a fresh search, so they fail if joined stops invalidating. They assert what they name.

Hot-path record (docs/performance/hot-path-review.md section 6), 806ec2a

Item Record
HP-1 Callers and tier line_cost: per link visited in search (step tier, per route asked). The kept-route lookup in cheapest: once per ask. joined: once per join_line. all_changed: once per step from stand_down. number_lines: once per router build (command tier, a tower edit).
HP-2 Scans added None per step. number_lines makes one pass over the links when the router is built.
HP-3 Nested or quadratic work None.
HP-4 Allocation in the loop A kept route is cloned on a hit and stored on a miss. The map is capped at MOST_ROUTES (1024) and cleared when full.
HP-5 Per-join cost forget_routes clears the map on every join. The cost is proportional to its size, at most 1024 entries, and usually small because a join empties it.
HP-6 Memory One (u32, i64) per Board line plus at most 1024 routes, a few hundred KiB at worst. Nothing is saved.
HP-7 Determinism FastMap is used only for lookups and never iterated into output. Saves are identical cache on and off on three towers, checked hourly.
HP-8 Caches Adds a cache that is equivalent to a fresh search. The module doc names what invalidates it, the probe above shows the equivalence, and no cache was dropped or bypassed.
HP-9 Measured The table above shows the cache is no slower than no cache on any tower, and about 5% faster on the owner's. The PR's callgrind counts show split-16 going from +21% to +8% against main before #2272.
HP-10 Save, layout, binding Unchanged. The cache isn't saved and is rebuilt empty after a load.
HP-11 Reload continuity Holds. The cache starts empty after a load and the routes don't depend on it.
HP-12 Budgets No bundle or install-size change. This is engine-only and adds no dependency.

Verdict: GO at 806ec2a.


Generated by Claude Code

This branch was successfully deployed

1 active and 1 inactive deployments
github-pages — 806ec2ac Deployed Nov 15, 2026 by maniator via docs-preview (remove) / docs-pages (deploy) #679
Preview — 806ec2ac Deployed Nov 15, 2026 by vercel[bot]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants