Skip to content

Make big towers cheaper to run, with every game the same - #2287

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

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

Conversation

@maniator

Copy link
Copy Markdown
Owner

What & why

The fast follow to #2272 and #2279 the owner asked for when 4.1 shipped with the Metro jam fix: bring the split tower back to no slower than before #2272. The hour-long-wait re-route still costs its searches, so this pays for them with exact speed-ups elsewhere in the step. Every game comes out the same.

  • Parking check, room by room. functional_parking_set flood-filled tile by tile with a SipHash set, about 105 times a day. It now steps from room to room (a ramp still reaches the floors above and below each of its tiles) and counts with the fast hasher (Tower::reached_parking).
  • Fast hasher for lookup-only maps. The demand pool (DemandMap), the housekeeping crew maps, the commute averages, the lobby reach sets (seg_served, lobby_reach) and the noise memo move from std maps to FastMap/FastSet. None of them is walked in hash order; the two that are listed (the commute averages in a save, a test sum) go through sorted().
  • Lobby distance walks the tall halls once. nearest_lobby_floor_distance did one ordered range lookup per lobby floor; both maps run up the tower, so one merged walk finds each floor's tallest hall.
  • Landing lines. landing_slots reads each shaft once per line instead of once per rider, sorts (wait, id, place) keys instead of chasing indices, and keeps each rider's slot by their place in Crowd::people (nobody leaves the list until the motion pass ends, and ids are unique) instead of in a map by id.
  • Router. A link whose own share already cannot beat the node's best skips pricing its line, the heap's push order is a u32, and line_cost and Toward::ahead are marked #[inline] so they stay inlined into the search however the crate's codegen units fall. cost_of moves to expected_costs.rs beside line_cost to keep expected_time.rs under the 500-line ceiling.

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

Nothing plays differently in either mode; both get faster.

Quality gates

  • npm run typecheck (no TypeScript change)
  • npm run lint (no TypeScript change)
  • npm test (CI runs it; the file-size and Rust prose guards pass locally)
  • npm run build (CI runs it)

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

Tests that hold the rewrites to the old behavior:

  • tower_reads_tests::the_parking_flood_seeds_from_the_ramps_alone compares the room-by-room fill against a tile-by-tile reference walk through ramps on two floors, every unit state, a same-count swap and a reload.
  • rule_tests::{lobby_distance, tall_lobby_distance, walking_penalties} cover the lobby distance with one-story and tall halls.
  • landing_tests (updated to read slots by place) cover the line order and the back of the line.
  • crowd/expected_costs_tests.rs (from Keep the Modern router's line costs and routes for the step #2279) checks kept costs and routes against fresh searches.

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), crowd::landing::landing_slots and motion::step (every crowd step, every waiting rider), Tower::functional_parking_set/functional_parking_spots (service checks, about 105 a day), Tower::nearest_lobby_floor_distance (every unit's satisfaction pass), demand::compute_demand_map (hourly traffic income), housekeeping dispatch, graph::segment_connected/seg_reachable, noise_afflicted, record_commute.
  • Before and after: parking goes from O(parking tiles) hash inserts to O(parking rooms) with a cheap hash; lobby distance from O(lobby floors x log tall halls) to O(lobby floors + tall halls); landing slots from one transport lookup per waiting rider plus a map insert per rider to one lookup per line plus an array write per rider. Map lookups keep their complexity with a hash that costs a few instructions instead of SipHash. No new scan.
  • Caches (HP-8): every memo stays (served set, lobby reach, noise memo, landing lines, segment runs, the router's line costs and kept routes). The landing slot map becomes a vector by place, rebuilt each step as before.
  • Invalidation: unchanged; no memo's key or reset moved.
  • Measurements (one game day from midnight on day 2, instructions in Simulation::tick only, from callgrind):
Tower main before #2272 This PR Change
split tower, default release build 2.262 G 2.225 G -1.6%
split tower, one codegen unit 2.240 G 2.224 G -0.7%
maxed Modern, one codegen unit 5.408 G 4.411 G -18%
maxed Classic, one codegen unit 2.705 G 2.049 G -24%

The default build splits the crate into 16 codegen units, and where they fall moves the router's inlining by up to about 1.5% between builds, so the one-codegen-unit rows compare the code itself.

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

Screenshots / recordings

None. No route, position or count changes, so nothing on screen moves.

Notes for reviewers

  • Same game. Each rewrite reads the same inputs and gives the same answer: the parking fill reaches the rooms a tile walk would, the merged lobby walk takes each floor's tallest hall as the range lookup did, the landing sort uses the same comparator over the same order (ids are unique, so the stable sort's result is fixed), and a skipped line price is one whose link could not have won (best <= cost + own share <= cost + full cost).
  • Tried and dropped: a radix queue for the router's open list, dropping the cost from heap entries, kept routes forgotten only when a search had read the joined line, and #[inline] on the transport lookup each ran more instructions here.

Generated by Claude Code

The Metro jam fix (#2272) costs a tower in a long elevator jam extra route
searches. This takes that back, and more, with exact speed-ups elsewhere in
the step: the parking check fills room by room, lookup-only maps (demand
pool, crew floors, commute averages, lobby reach, noise memo) use the fast
hasher, the lobby distance walks the tall halls once, the landing lines
read each shaft once per line and keep slots by place in the crowd, and the
Modern router skips pricing a line when the link cannot win.

Per game day (callgrind instructions, ticks only, one codegen unit), main
before #2272 against this branch: split tower 2.240 G to 2.224 G, maxed
Modern 5.408 G to 4.411 G, maxed Classic 2.705 G to 2.049 G. Conformance
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 11:26am UTC

Request Review

@github-actions github-actions Bot added this to the 4.x milestone Nov 15, 2026
@maniator
maniator marked this pull request as ready for review November 15, 2026 11:25
@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-15T11:25:14.108579Z
We couldn’t complete this review. Comment @codex review to try again.
2bd4b3a 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.

@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. ✅

Copy link
Copy Markdown
Owner Author

Independent review at 2bd4b3a: GO

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 under test: every change is exact, and nothing is slower than before #2272.

Is every change exact? Read change by change

  • Search prune (expected_time::search). line_cost is l.tenths + tenths(expected.max(oldest)). Its inputs are all non-negative: running is at least 1, and round_trip, riders and oldest are never below 0. So the cost is at least l.tenths, and when best[l.to] <= cost + l.tenths the old code would also have hit continue. Skipping line_cost there only skips filling the cache, which nothing observes. ahead is still called for every link, as before, so the lazy bound grows the same way.

  • Heap order u64 to u32. The counter resets for each search and goes up once per relaxation, which the link count bounds (far below 2^32). The tuple compares the same way.

  • cost_of moved to expected_costs.rs. It moved word for word.

  • landing_slots by place. advance builds the landings once, loops over the people the slice started with, and removes people only with the retain after the loop. Spawns are pushed at the end, past slots.len(), so slot(i) returns None for them, just as their ids were missing from the old map. The grouping now drops non-elevator shafts per group instead of per rider, and FastIndexMap keeps the elevator groups in the same order. The sort key (wait descending, then id) and the stable sort are unchanged.

  • Parking fill room by room. Parking and ramps are one story (floors: None), so room_at(f, x) always returns a room on floor f. A tile fill crossed each room tile by tile, so the room-level fill gives the same set:

    • it spreads sideways from u.x - 1 and u.x + u.width;
    • a ramp spreads up and down at every one of its columns;
    • it seeds one tile per ramp.

    The ids are unique by visited, so len() and the HashSet built from the list match the old set.

  • Lobby distance merge walk. lobby_tiles and tall_lobby_tiles are both BTreeMaps, sorted by floor and then (floor, height). The walk sets top from the last hall on lf, which is the tallest and the same entry range(..).next_back() gave. Halls on floors with no lobby tile are skipped, as before.

  • The std-to-Fast* swaps (demand pool, crew floors, commute averages, seg_served and lobby_reach, noise_memo). FastMap and FastSet hand out no iterator, so the build proves nothing reads their order. The one ordered read, commute_stress_json, now uses sorted(), which gives the same rows. floors_busy as a (crew, floor) set answers contains exactly as the map of sets did. These modules are #[doc(hidden)] tier 3 (API.md Stability), so changing their field types is not a crate API break.

Is every change exact? The probe (scratch, not committed)

The probe loads each tower and runs 30 game hours one second at a time. Every hour it prints:

  • a hash of serialize(),
  • functional_parking_spots(),
  • a hash of nearest_lobby_floor_distance for every floor from -10 to 110,
  • the head count.

I ran it on main (d0c287b, 4.1.0) and on this PR and compared the output:

Tower Hours compared Identical
split-24 30 all 30
maxed Modern 30 all 30
maxed Classic 30 all 30

The towers had 271, 534 and 534 parking spaces and up to 5,109 people, so the parking fill and the landing lines ran under load.

Nothing slower: timing

One game day on each tower, load excluded. Each cell is the best of 3 within a run, with two runs per commit, interleaved.

Tower Before #2272 (67b06a1) main 4.1.0 This PR
split-24 477 / 497 ms 526 ms 470 / 479 ms (465 in the first run)
maxed Modern 1762 / 1789 ms 1792 ms 1236 / 1289 ms (1251)
maxed Classic 843 / 810 ms 806 ms 538 / 573 ms (546)

The split tower is back to its speed before #2272, or slightly faster. Maxed Modern is about 30% faster and maxed Classic about 33% faster. This agrees with the PR's callgrind counts.

Gates and CI

cargo fmt --check passes, cargo clippy --release --all-targets --features tooling,conformance -D warnings passes, and cargo test --release --lib passes 1894 of 1894. CI on 2bd4b3a is green (rust, rust-lint, fuzz-corpus, wasm-binding, consumer, the e2e shards and screenshot drift). tower_query.rs ends at 499 lines, under the 500-line guard.

The test edits only follow the API: .sorted() replaces .iter(), and the slot is looked up by place. Every assertion is unchanged, and the existing tests for parking, lobby distance and tall-lobby distance cover the rewritten paths.

Findings

  • patch: none.
  • note, no action: Landings::slots is now a Vec<Option<f64>> the length of the crowd, filled once per slice. That is O(people) where the old map was O(waiting riders). The slice already walks every person, and the measured time dropped, so this is fine.
  • note, no action: slot(i) depends on nothing reordering crowd.people before the end-of-slice retain. The doc comment says so. If something ever removes people mid-slice, this would need to go back to keying by id.

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

Item Record
HP-1 Callers and tier Step and slice tier: search (every Modern route), landing_slots (once a slice), cost_of and line_cost, the housekeeping dispatch lookups, the demand map build, the lobby-reach sets (on a tower revision) and nearest_lobby_floor_distance (demand and satisfaction reads). Hour or day tier: functional_parking_set and functional_parking_spots.
HP-2 Scans added None. The lobby distance goes from one ordered lookup per lobby floor to one merged walk of both sorted lists. The parking fill visits each room once instead of each tile.
HP-3 Nested or quadratic work None added. A ramp's vertical pushes are per column, as before.
HP-4 Allocation in the loop landing_slots allocates one Vec<Option<f64>> per slice (O(people)) in place of a map of waiting riders, and reuses one line_up buffer across lines. The search heap entry shrinks from 32 to 24 bytes.
HP-5 Hashing std SipHash maps and sets become FastMap and FastSet on lookup-only paths. Neither hands out an iterator, so the order can't leak.
HP-6 Memory Smaller or equal. floors_busy is flattened to one set, and the heap entry is smaller.
HP-7 Determinism Saves are identical every hour for 30 hours on three towers against main. Nothing reads an iteration order.
HP-8 Caches None dropped or bypassed. The prune skips filling the #2279 line cost only where the link cannot win.
HP-9 Measured The table above shows no tower slower than main or than before #2272.
HP-10 Save, layout, binding Unchanged. No saved field moved, and the hashes are equal.
HP-11 Reload continuity Not affected. Nothing new is saved or rebuilt differently after a load.
HP-12 Budgets Engine-only. No dependency added and no bundle change.

Verdict: GO at 2bd4b3a.


Generated by Claude Code

@maniator
maniator merged commit 0546694 into main Nov 15, 2026
49 of 50 checks passed
@maniator
maniator deleted the claude/project-thread-u5g4vi-fastfollow branch November 15, 2026 11:41

This branch was successfully deployed

1 active and 1 inactive deployments
github-pages — 2bd4b3a1 Deployed Nov 15, 2026 by maniator via docs-preview (remove) / docs-pages (deploy) #687
Preview — 2bd4b3a1 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