Skip to content

More speed improvements to Alien City - #524

Merged
Perlkonig merged 2 commits into
AbstractPlay:developfrom
mcdemarco:acity-cache
Nov 1, 2026
Merged

Perlkonig merged 2 commits into
AbstractPlay:developfrom
mcdemarco:acity-cache

Conversation

@mcdemarco

Copy link
Copy Markdown
Contributor

So my agent still liked its version better. Here is an actual PR in case you want to consider its improvements, probably after more of a shakedown of your current changes. The chatty details:

Results

Measured with upstream's scripts/bench-acity.ts, plus a small timing script for the roads display:

Before After
moves() on a fresh board ~120 ms ~12 ms
Rendering the roads display ~12 ms under 0.2 ms

What changed

src/games/acity-road.ts

  • New structuralConstraints(board) function. In one pass it finds every empty lot where building would break the road, and every lot where building would cut a structure off from it.
    • Road-breaking lots are the "cut points" of the empty-lot network: lots that are the only link between two parts of the road. They're found with Tarjan's articulation-point algorithm.
    • Isolating lots are lots that are the last empty neighbour of some building.
  • It assumes a board reached by legal play, where the road is always one connected network.

src/games/acity.ts

  • canPlace(), the off-colour dome check and the roads display all read one cached result instead of rebuilding the road graph for every candidate lot.
  • The cache refreshes whenever the board's contents change. It compares contents rather than relying on move(), because tests sometimes edit the board directly; upstream's distance cache works the same way.
  • Upstream's roadNetworkAfterPlacement is kept as the reference implementation.

test/games/acity.test.ts

  • New test group checking, lot by lot, that structuralConstraints() gives exactly the same answers as the reference:
    • on every golden fixture;
    • at every position of two complete, repeatable test games (about 6 seconds).

test/fixtures/acity-golden.ts and acity-golden-oracle.json

  • Removed the road-pinch-closed fixture, whose board has the road already split. Legal play can't produce that, so it's no longer supported.
  • Its entry was removed from the recorded results by hand rather than by re-recording the file, so every other fixture is still checked against the results upstream originally captured, not against the new code.

Checks

  • Upstream's golden-oracle tests, which compare against the original pre-refactor rules, still pass.
  • Full npm test passes (2518 tests); lint and typecheck are clean.

Not changed

  • test/fixtures/acity-bench-results.json, which records upstream's before/after numbers for their own refactor.
  • Two smaller possible improvements, neither of which showed up as slow:
    • the distance cache rebuilds a board signature on every lookup;
    • claimWouldEndConstruction builds a whole new game for each claim it checks.

@Perlkonig
Perlkonig merged commit a8e4fac into AbstractPlay:develop Nov 1, 2026
1 check passed
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