Draw and reload nothing for a window nobody sees (#29) #36

Merged
nessar merged 2 commits from perf/29-nothing-while-hidden into main 2026-11-17 20:11:24 +01:00
Owner

Part of #29. The ticket stays open until budget 3 is measured on the reference laptop and recorded in docs/performance.md.

What we found in GPUI (unmodified, ADR-0004)

GPUI 0.3.12 (through gpui-kit 0.9.1) already does part of the job on Windows:

  • Minimised window: GPUI sets aside its frame callback, so no frame can be drawn (gpui-pre-windows events.rs, handle_size_msg).
  • Fully covered window: Windows still counts it as visible (IsWindowVisible && !IsIconic), so GPUI draws it whenever a view calls cx.notify().

The app was the one asking for frames. The follower sends a Round every 5 s while the game client is closed, and apply rebuilt the whole page for every one of them, even when nothing had changed and even when the window was covered.

What changes

  1. No redraw for a round that changes nothing (stats_page.rs).

    • A Round returns before cx.notify() when all of these hold: the same Status, no reload asked, no pending first round, no progress shown and no crash banner.
    • Every other message still redraws.
    • This applies whatever the visibility, because a covered window counts as visible on Windows.
  2. Statistics computed on a background worker (stats_page.rs, worker.rs). The reloads triggered by the workers (import progress, champions loaded, a round with new Matches, end of a refresh) work like this:

    • The Accounts are still read on the UI thread: two small queries, and the account drop-down is updated.
    • The statistics and the Match count are computed by spawn_statistics, on a spawn_guarded thread, so at below-normal priority (#30). The result comes back as Msg::Statistics and is applied at once, whether the window is visible or not. A restored window is therefore up to date at once, and nobody waits for the computation.
    • The old statistics stay shown until the new ones arrive.
    • At most one job runs at a time (RefreshGate). A request made meanwhile is served once, with the current scope.
    • A generation number is bumped when statistics are computed (a job started, or a user's selection) or when the viewed Account changes. A result overtaken by a newer request is ignored. A request merely queued for the same scope does not discard the running job's result.
    • The job reports its own panic as an error result (Statistiques indisponibles : erreur interne). The crash of another worker never discards it.
    • User selections (Account, Season, Format) and the first display still compute synchronously, as before.

    This version replaces the first one, which deferred these reloads while the window was hidden. You asked that the computation keep running so that nobody waits for it.

  3. This branch also contains:

    • the rebase onto main (#28, #30) done from Gitea;
    • the one-line fix of the priority test (same change as PR #38). Since #35 and #34 were merged, main no longer compiles its tests.

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: 14 passed, including:
    • worker::tests::statistics_workers_deliver_the_generation_count_and_errors: a real in-memory Tracker; it checks the generation, the count and the error.
  • Independent review (Claude Opus) and /code-review (standards and spec), on both versions. On the background version they found:
    • a crash of another worker that discarded a valid result: fixed;
    • results discarded by a request merely queued: fixed;
    • a test tied to the core's wording: fixed.

Measurements: desktop, for information only

These were taken on the desktop (Ryzen 7 7800X3D, RX 7800 XT, 3840x2160 @ 240 Hz), which is not the reference laptop, while the PC was in use. Release builds, real data, game client closed. CPU is the process's CPU time; GPU is the process's GPU Engine running time (0 ms means no frame was drawn).

Hidden window (8 s per state after 30 s of loading; first version of this branch, the same for parts 1 and 3 since nothing is reloaded at idle):

State main e13db17: CPU, GPU this branch: CPU, GPU
Visible 0.39 %, 1 ms 0.20 %, 0 ms
Minimised 3.13 %, 0 ms 1.17 %, 0 ms
Covered 0.39 %, 1 ms 0.20 %, 0 ms

Import from an empty folder, window visible (CPU sampled every second for 50 s, threads seen busy from 20 ms/s; main dc2bcd0 against the background version; 92 Matches imported in both):

main dc2bcd0 background version
UI thread busy during the import 10 s out of 50, up to 63 ms/s 5 s out of 50, up to 47 ms/s
Statistics computation on the UI thread below-normal thread, at most 31 ms/s
App's mean CPU over 50 s 12.4 % 9.2 %
  • Covered window: no frame is drawn any more.
  • Minimised window: no frame in either build; GPUI already guarantees that.
  • Minimised residual CPU, unexplained: it varies from run to run on this machine (1.2–4.5 %). A per-thread sample found 0.00 %. The app does not cause it. It is to be measured on the idle laptop before budget 3's floor is recorded.
  • The measured background build predates the two review fixes. Those only change edge cases (a crash, a queued request), not the measured paths.

Limits

  • During an import, a covered window still redraws on each progress message and each result. The app cannot tell it is covered: GPUI only reports occlusion on other platforms.
  • The UI thread can wait for the database lock while the job reads the Matches. That is a few ms, and std's mutex has no priority inheritance with a below-normal holder. The follower already shared that lock.
  • The format list lags briefly when the followed Account changes. Until the result arrives, it still holds the previous Account's formats.
  • No automated test of the page logic (gate plus generation in StatsPage). It would need GPUI's test-support, or pulling the decision out into a pure struct.
  • The caret blinks with no idle timeout in the key field of the settings and first-launch screens (gpui-component). A possible follow-up, through Theme.cursor_blink.

🤖 Generated with Claude Code

Part of #29. The ticket stays open until budget 3 is measured on the reference laptop and recorded in `docs/performance.md`. ## What we found in GPUI (unmodified, ADR-0004) GPUI 0.3.12 (through gpui-kit 0.9.1) already does part of the job on Windows: - **Minimised window:** GPUI sets aside its frame callback, so no frame can be drawn (`gpui-pre-windows` `events.rs`, `handle_size_msg`). - **Fully covered window:** Windows still counts it as visible (`IsWindowVisible && !IsIconic`), so GPUI draws it whenever a view calls `cx.notify()`. The app was the one asking for frames. The follower sends a `Round` every 5 s while the game client is closed, and `apply` rebuilt the whole page for every one of them, even when nothing had changed and even when the window was covered. ## What changes 1. **No redraw for a round that changes nothing** (`stats_page.rs`). - A `Round` returns before `cx.notify()` when all of these hold: the same `Status`, no reload asked, no pending first round, no progress shown and no crash banner. - Every other message still redraws. - This applies whatever the visibility, because a covered window counts as visible on Windows. 2. **Statistics computed on a background worker** (`stats_page.rs`, `worker.rs`). The reloads triggered by the workers (import progress, champions loaded, a round with new Matches, end of a refresh) work like this: - The Accounts are still read on the UI thread: two small queries, and the account drop-down is updated. - The statistics and the Match count are computed by `spawn_statistics`, on a `spawn_guarded` thread, so at below-normal priority (#30). The result comes back as `Msg::Statistics` and is applied at once, **whether the window is visible or not**. A restored window is therefore up to date at once, and nobody waits for the computation. - The old statistics stay shown until the new ones arrive. - At most one job runs at a time (`RefreshGate`). A request made meanwhile is served once, with the current scope. - A **generation** number is bumped when statistics are computed (a job started, or a user's selection) or when the viewed Account changes. A result overtaken by a newer request is ignored. A request merely queued for the same scope does not discard the running job's result. - The job reports its own panic as an error result (`Statistiques indisponibles : erreur interne`). The crash of another worker never discards it. - User selections (Account, Season, Format) and the first display still compute synchronously, as before. This version replaces the first one, which deferred these reloads while the window was hidden. You asked that the computation keep running so that nobody waits for it. 3. **This branch also contains:** - the rebase onto `main` (#28, #30) done from Gitea; - the one-line fix of the priority test (same change as PR #38). Since #35 and #34 were merged, `main` no longer compiles its tests. ## 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`: 14 passed, including: - `worker::tests::statistics_workers_deliver_the_generation_count_and_errors`: a real in-memory `Tracker`; it checks the generation, the count and the error. - Independent review (Claude Opus) and `/code-review` (standards and spec), on both versions. On the background version they found: - a crash of another worker that discarded a valid result: **fixed**; - results discarded by a request merely queued: **fixed**; - a test tied to the core's wording: **fixed**. ## Measurements: desktop, for information only These were taken on the desktop (Ryzen 7 7800X3D, RX 7800 XT, 3840x2160 @ 240 Hz), which is not the reference laptop, while the PC was in use. Release builds, real data, game client closed. CPU is the process's CPU time; GPU is the process's `GPU Engine` running time (0 ms means no frame was drawn). **Hidden window** (8 s per state after 30 s of loading; first version of this branch, the same for parts 1 and 3 since nothing is reloaded at idle): | State | main e13db17: CPU, GPU | this branch: CPU, GPU | | --- | --- | --- | | Visible | 0.39 %, 1 ms | 0.20 %, 0 ms | | Minimised | 3.13 %, 0 ms | 1.17 %, 0 ms | | Covered | 0.39 %, 1 ms | 0.20 %, **0 ms** | **Import from an empty folder, window visible** (CPU sampled every second for 50 s, threads seen busy from 20 ms/s; `main` dc2bcd0 against the background version; 92 Matches imported in both): | | main dc2bcd0 | background version | | --- | --- | --- | | UI thread busy during the import | 10 s out of 50, up to 63 ms/s | 5 s out of 50, up to 47 ms/s | | Statistics computation | on the UI thread | below-normal thread, at most 31 ms/s | | App's mean CPU over 50 s | 12.4 % | 9.2 % | - **Covered window:** no frame is drawn any more. - **Minimised window:** no frame in either build; GPUI already guarantees that. - **Minimised residual CPU, unexplained:** it varies from run to run on this machine (1.2–4.5 %). A per-thread sample found 0.00 %. The app does not cause it. It is to be measured on the idle laptop before budget 3's floor is recorded. - The measured background build predates the two review fixes. Those only change edge cases (a crash, a queued request), not the measured paths. ## Limits - **During an import, a covered window still redraws** on each progress message and each result. The app cannot tell it is covered: GPUI only reports occlusion on other platforms. - **The UI thread can wait for the database lock** while the job reads the Matches. That is a few ms, and std's mutex has no priority inheritance with a below-normal holder. The follower already shared that lock. - **The format list lags briefly when the followed Account changes.** Until the result arrives, it still holds the previous Account's formats. - **No automated test of the page logic** (gate plus generation in `StatsPage`). It would need GPUI's `test-support`, or pulling the decision out into a pure struct. - **The caret blinks with no idle timeout** in the key field of the settings and first-launch screens (gpui-component). A possible follow-up, through `Theme.cursor_blink`. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
nessar force-pushed perf/29-nothing-while-hidden from 7b656b6c36 to db245b64ba 2026-11-17 19:47:22 +01:00 Compare
Author
Owner

Background version pushed (commits 1e2a01e, 511554b), as you asked: the worker-triggered reloads are no longer deferred while the window is hidden; the statistics are computed on a below-normal thread and applied at once, visible or not. During an import, the UI thread is busy 5 s out of 50 instead of 10 (desktop, for information). The description has been updated, with the measurements and limits.

The branch also carries the one-line test fix from PR #38: since #34 and #35 were merged, main no longer compiles its tests (worker.rs:187, E0308).

Background version pushed (commits 1e2a01e, 511554b), as you asked: the worker-triggered reloads are no longer deferred while the window is hidden; the statistics are computed on a below-normal thread and applied at once, visible or not. During an import, the UI thread is busy 5 s out of 50 instead of 10 (desktop, for information). The description has been updated, with the measurements and limits. The branch also carries the one-line test fix from PR #38: since #34 and #35 were merged, `main` no longer compiles its tests (`worker.rs:187`, E0308).
nessar added 2 commits 2026-11-17 19:59:44 +01:00
A follower round that changes nothing shown no longer rebuilds the page.
With the game client closed one arrives every 5 s, and each redrew the
whole page, also while the window was covered by another window.

Worker-driven reloads wait while GPUI reports the window hidden
(minimised or not shown) and run as soon as it is shown again, so a
restored window shows the Matches imported meanwhile at once.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Compute statistics on a background worker instead of deferring them (#29)
CI / core (pull_request) Successful in 37s
CI / core (push) Successful in 45s
c9f5b264e6
Reloads triggered by the workers no longer wait for the window to be
shown: the statistics and Match count are computed on a spawn_guarded
thread (below normal priority) and applied as soon as they arrive,
visible or not, so a restored window is up to date at once and the UI
thread computes nothing during an import. The Accounts are still read
on the UI thread; the old statistics stay shown until the new ones land.

One job runs at a time (RefreshGate); a generation number, bumped when
statistics are computed or the viewed Account changes, drops results
overtaken by a newer request or a user's selection. The job reports its
own panic as an error result, so another worker's crash never discards
it. User selections still recompute synchronously.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
nessar force-pushed perf/29-nothing-while-hidden from 511554b805 to c9f5b264e6 2026-11-17 19:59:44 +01:00 Compare
nessar merged commit c9f5b264e6 into main 2026-11-17 20:11:24 +01:00
nessar deleted branch perf/29-nothing-while-hidden 2026-11-17 20:11:24 +01:00
Sign in to join this conversation.