Wake the statistics page on worker messages instead of polling (#28) #34

Merged
nessar merged 1 commits from perf/28-wake-on-worker-message into main 2026-11-17 19:14:08 +01:00
Owner

Closes #28 once its measurements are recorded (see below).

What changes

The statistics page no longer polls the workers' channel every 100 ms. It awaits gpui-kit's own async channel (gpui_kit::base::async_util, which is smol::channel on native), so the UI thread wakes only when a worker sends a message.

  • main.rs: the channel is created with unbounded() instead of std::sync::mpsc::channel().
  • worker.rs: workers send with try_send. On an unbounded channel this never blocks, and it fails only once the page is gone, which is ignored as before. The panic → Msg::Crashed path is unchanged.
  • stats_page.rs: the listener awaits rx.recv() and applies each message as it arrives. Its Task is now owned by the page instead of being detached, so closing the window drops the page, which cancels the task. The old detached loop kept its 100 ms timer running after the window closed, because the follower never drops its sender.
  • Msg, RefreshGate, apply, following the game client, the refresh button and the first-launch key prompt are unchanged.

The choice (asked by the ticket)

Polling was chosen to "keep the UI free of any extra dependency". The async channel that gpui-kit already re-exports gives both: no timer and no new dependency. Cargo.toml and Cargo.lock are unchanged. GPUI and gpui-component are untouched (ADR-0004). The choice is easy to reverse, so it is recorded in a code comment and here rather than in an ADR.

Checks

  • cargo fmt --all --check: OK
  • cargo.exe clippy --locked -p arena-tracker --all-targets -- -D warnings: OK
  • cargo.exe test --locked -p arena-tracker: 12 passed. The new test worker::tests::guarded_workers_deliver_messages_and_report_panics covers delivery from a worker thread, a panic arriving as Crashed, and the channel closing.
  • An independent review (Claude Opus) and /code-review (standards and spec) found nothing blocking.

Measurements: still to do on the reference laptop

Rule #1 asks for before/after numbers on a release build on the reference machine. This session ran on the desktop (7800X3D, 240 Hz), which is not the reference machine. There, the idle CPU of main alone varied from 0.5 % to 3.9 % between runs while the PC was in use, too noisy to show a 10 Hz wake-up. No conclusion is drawn from it. To measure on the idle laptop:

git switch --detach origin/main && scripts/prod build    # copy target\release\arena-tracker.exe aside as "before"
git switch perf/28-wake-on-worker-message && scripts/prod build   # "after"
# launch each build in turn, wait for the page to load (~45 s), then:
powershell.exe -ExecutionPolicy Bypass -File scripts/move-probe.ps1 -Title "Arena Tracker" -Seconds 30

Compare IDLE cpu (budget 2), alternating the two builds at least twice. Record the numbers on #28, and lower budget 2 in docs/performance.md if it is beaten durably (ratchet).

Limits

  • The new test covers the worker side of the channel, not the page's GPUI listener. That would need gpui-kit's test-support feature, which is a Cargo change.
  • With the game client closed, the follower still sends a Round every 5 s, and apply calls cx.notify() for each one, so the page redraws in full every 5 s. This predates the change and is a possible follow-up for budget 2: skip the notify when nothing changed.
  • Each Progress message now reaches apply as soon as it is sent, rather than in batches every 100 ms. Redraws stay bounded by vsync, and the Riot API rate limit keeps these messages infrequent.
Closes #28 once its measurements are recorded (see below). ## What changes The statistics page no longer polls the workers' channel every 100 ms. It awaits gpui-kit's own async channel (`gpui_kit::base::async_util`, which is `smol::channel` on native), so the UI thread wakes only when a worker sends a message. - `main.rs`: the channel is created with `unbounded()` instead of `std::sync::mpsc::channel()`. - `worker.rs`: workers send with `try_send`. On an unbounded channel this never blocks, and it fails only once the page is gone, which is ignored as before. The panic → `Msg::Crashed` path is unchanged. - `stats_page.rs`: the listener awaits `rx.recv()` and applies each message as it arrives. Its `Task` is now owned by the page instead of being detached, so closing the window drops the page, which cancels the task. The old detached loop kept its 100 ms timer running after the window closed, because the follower never drops its sender. - `Msg`, `RefreshGate`, `apply`, following the game client, the refresh button and the first-launch key prompt are unchanged. ## The choice (asked by the ticket) Polling was chosen to "keep the UI free of any extra dependency". The async channel that gpui-kit already re-exports gives both: no timer and no new dependency. `Cargo.toml` and `Cargo.lock` are unchanged. GPUI and gpui-component are untouched (ADR-0004). The choice is easy to reverse, so it is recorded in a code comment and here rather than in an ADR. ## Checks - `cargo fmt --all --check`: OK - `cargo.exe clippy --locked -p arena-tracker --all-targets -- -D warnings`: OK - `cargo.exe test --locked -p arena-tracker`: 12 passed. The new test `worker::tests::guarded_workers_deliver_messages_and_report_panics` covers delivery from a worker thread, a panic arriving as `Crashed`, and the channel closing. - An independent review (Claude Opus) and `/code-review` (standards and spec) found nothing blocking. ## Measurements: still to do on the reference laptop Rule #1 asks for before/after numbers on a release build on the reference machine. This session ran on the desktop (7800X3D, 240 Hz), which is not the reference machine. There, the idle CPU of `main` alone varied from 0.5 % to 3.9 % between runs while the PC was in use, too noisy to show a 10 Hz wake-up. No conclusion is drawn from it. To measure on the idle laptop: ```sh git switch --detach origin/main && scripts/prod build # copy target\release\arena-tracker.exe aside as "before" git switch perf/28-wake-on-worker-message && scripts/prod build # "after" # launch each build in turn, wait for the page to load (~45 s), then: powershell.exe -ExecutionPolicy Bypass -File scripts/move-probe.ps1 -Title "Arena Tracker" -Seconds 30 ``` Compare `IDLE cpu` (budget 2), alternating the two builds at least twice. Record the numbers on #28, and lower budget 2 in `docs/performance.md` if it is beaten durably (ratchet). ## Limits - The new test covers the worker side of the channel, not the page's GPUI listener. That would need gpui-kit's `test-support` feature, which is a Cargo change. - With the game client closed, the follower still sends a `Round` every 5 s, and `apply` calls `cx.notify()` for each one, so the page redraws in full every 5 s. This predates the change and is a possible follow-up for budget 2: skip the notify when nothing changed. - Each `Progress` message now reaches `apply` as soon as it is sent, rather than in batches every 100 ms. Redraws stay bounded by vsync, and the Riot API rate limit keeps these messages infrequent.
nessar added 1 commit 2026-11-17 18:46:33 +01:00
Wake the statistics page on worker messages instead of polling (#28)
CI / core (pull_request) Successful in 1m16s
CI / core (push) Successful in 1m3s
75dd0ea643
The page polled a std channel on a 100 ms timer, waking the UI thread ten
times per second with nothing to do. It now awaits gpui-kit's own async
channel (gpui_kit::base::async_util, smol's on native), so it wakes only
when a worker sends, with no timer and no new dependency. The listener
task is owned by the page, so it stops when the window closes.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Author
Owner

Measurements: desktop, for information only (not the reference machine)

Implementation: PR #34 (branch perf/28-wake-on-worker-message, commit 75dd0ea).

Machine: AMD Ryzen 7 7800X3D; AMD Radeon RX 7800 XT; display 3840x2160 @ 240 Hz. This is the desktop used for manual checks, not the reference laptop of docs/performance.md. These numbers are for information and are not a criterion. The PC was in use during the run.
Builds: release (scripts/prod build): main at e13db17 (bench\main-e13db17.exe) and this branch (bench\pr28.exe).
Scenario: launch with the real data, game client closed, window visible and untouched. Wait 45 s for the page to load, then measure over 30 s. The two builds alternate, two rounds each.
Method: process CPU from TotalProcessorTime. Wake-ups are context switches per second of each thread of the process, from two snapshots of Win32_PerfRawData_PerfProc_Thread 30 s apart. The UI thread is the thread that owns the window.

Build UI thread wake-ups/s All threads wake-ups/s Process CPU (% of one core) Working set
main, round 1 283.9 571 1.30 % 70 MB
#28, round 1 268.3 522 1.46 % 70 MB
main, round 2 291.0 578 0.99 % 70 MB
#28, round 2 268.6 523 1.77 % 70 MB
  • The UI thread wakes 15–23 fewer times per second.
  • Two threads at about 16 wake-ups/s (the timer) disappear, about 50 fewer wake-ups/s in all.
  • On this 240 Hz display, most of the remaining wake-ups come from GPUI's vsync: one thread at about 252/s, plus the UI thread it wakes.
  • Idle CPU does not measurably change here. The differences stay within the run-to-run noise of this machine in use: an earlier run of main alone ranged from 0.5 % to 3.9 % with the move probe's idle phase.
  • Budget 2 is still to be measured on the idle reference laptop (60 Hz), where the timer is a larger share of the wake-ups. The commands are in PR #34.
## Measurements: desktop, for information only (not the reference machine) Implementation: PR #34 (branch `perf/28-wake-on-worker-message`, commit 75dd0ea). **Machine**: AMD Ryzen 7 7800X3D; AMD Radeon RX 7800 XT; display 3840x2160 @ 240 Hz. This is the desktop used for manual checks, not the reference laptop of `docs/performance.md`. These numbers are for information and are not a criterion. The PC was in use during the run. **Builds**: release (`scripts/prod build`): `main` at e13db17 (`bench\main-e13db17.exe`) and this branch (`bench\pr28.exe`). **Scenario**: launch with the real data, game client closed, window visible and untouched. Wait 45 s for the page to load, then measure over 30 s. The two builds alternate, two rounds each. **Method**: process CPU from `TotalProcessorTime`. Wake-ups are context switches per second of each thread of the process, from two snapshots of `Win32_PerfRawData_PerfProc_Thread` 30 s apart. The UI thread is the thread that owns the window. | Build | UI thread wake-ups/s | All threads wake-ups/s | Process CPU (% of one core) | Working set | | --- | --- | --- | --- | --- | | main, round 1 | 283.9 | 571 | 1.30 % | 70 MB | | #28, round 1 | 268.3 | 522 | 1.46 % | 70 MB | | main, round 2 | 291.0 | 578 | 0.99 % | 70 MB | | #28, round 2 | 268.6 | 523 | 1.77 % | 70 MB | - The UI thread wakes 15–23 fewer times per second. - Two threads at about 16 wake-ups/s (the timer) disappear, about 50 fewer wake-ups/s in all. - On this 240 Hz display, most of the remaining wake-ups come from GPUI's vsync: one thread at about 252/s, plus the UI thread it wakes. - Idle CPU does not measurably change here. The differences stay within the run-to-run noise of this machine in use: an earlier run of `main` alone ranged from 0.5 % to 3.9 % with the move probe's idle phase. - Budget 2 is still to be measured on the idle reference laptop (60 Hz), where the timer is a larger share of the wake-ups. The commands are in PR #34.
nessar merged commit 75dd0ea643 into main 2026-11-17 19:14:08 +01:00
nessar deleted branch perf/28-wake-on-worker-message 2026-11-17 19:14:09 +01:00
Sign in to join this conversation.