Skip to content

Play page: the engine thinks in a worker, so the page never freezes - #15

Merged
AdminGodZ merged 1 commit into
mainfrom
t3/improve-load-performance
Nov 27, 2026
Merged

AdminGodZ merged 1 commit into
mainfrom
t3/improve-load-performance

Conversation

@AdminGodZ

Copy link
Copy Markdown
Owner

The engine ran on the page's own thread. Most moves take under a millisecond, but a tenth take over 60 ms and the slowest several hundred (more on a phone). The page froze for each one: no hover, no clicks, no animation.

What changes

  • The engine runs in a Web Worker (site/src/components/game/engine.ts, engine.worker.ts). The page compiles the engine and hands the module to the worker. Calls go in batches, one round trip per move. The worker is inlined into the play script as a blob, so the offline broadside.html still works from disk.
  • The engine thinks during the aiming pause. The 220 ms "The engine is aiming…" pause now runs while the engine thinks, instead of before it. A shot lands after max(220 ms, thinking) instead of 220 ms + thinking.
  • The heat map is cached. It comes with the move that changes it (or when it's switched on), instead of being recomputed on every render(), which includes every hover.
  • Fallback: if no worker can start, the same batches run on the page, as before.
  • Late replies are dropped. A reply that arrives after its game was left (a resign while the engine is aiming) is ignored, and an engine failure mid-game is shown in place of the status line.

Measured (headless Chromium, 8 full games each, built site)

Before After
Long tasks (over 50 ms) 16, up to 267 ms 0
Longest gap between frames 250 ms 17 ms
Engine reply, median / 99th pct / max 234 / 441 / 500 ms 229 / 283 / 328 ms

Also played through in a browser:

  • Offline file from disk: worker runs, no long tasks.
  • Worker disabled: the fallback plays complete games.
  • Resign while the engine is aiming, then a new game: no stray shot lands.
  • Player win: banner and record are correct.
  • Heat map: shown, and the reveal after each game is drawn.

Checks

  • cargo clippy --all-targets: clean.
  • cargo test: 61 passed.
  • npm run check: 0 errors, 0 warnings.
  • npm run build: builds, including dist/broadside.html.

The engine ran on the page's thread: a tenth of its moves take over 60 ms,
the slowest several hundred, and the page froze for each. It now runs in a
worker, inlined into the play script (so the offline file keeps working),
with calls sent in batches, one round trip per move. The 220 ms aiming pause
now overlaps the engine's thinking instead of preceding it, and the heat map
comes with the move that changes it instead of being recomputed on every
render. Where no worker can start, the engine runs on the page as before.
@vercel

vercel Bot commented Nov 27, 2026 •

Copy link
Copy Markdown

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

Project Deployment Actions Updated
broadside Ready Ready Preview Nov 27, 2026 6:59am UTC

@AdminGodZ
AdminGodZ merged commit dee5564 into main Nov 27, 2026
4 checks 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.

1 participant