Run background threads below normal priority (#30) #35

Merged
nessar merged 1 commits from perf/30-background-low-priority into main 2026-11-17 19:42:07 +01:00
Owner

Closes #30 once its measurements on the reference machine are recorded (see below).

What changes

Every thread the app starts itself goes through spawn_guarded (crates/app/src/worker.rs): the champion catalog and portraits, the game client follower, and the refresh button's sync. Each of these threads now lowers itself to THREAD_PRIORITY_BELOW_NORMAL before running its body. The game and the UI thread therefore keep the CPU they need (rule #1, budget 5).

  • The two kernel32 calls (GetCurrentThread, SetThreadPriority) are declared in place behind #[cfg(windows)]. No dependency is added, and Cargo.toml and Cargo.lock are unchanged. On other targets the function does nothing, and the core crate is not touched (ADR-0003).
  • The call is best effort: if it fails, the work still runs.
  • The panic → Msg::Crashed path and the bodies of the follower, refresh and champion threads are unchanged.
  • GPUI and gpui-component are untouched (ADR-0004).

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 Windows test worker::tests::guarded_threads_run_below_normal_priority reads the priority from inside a spawn_guarded body and expects -1.
  • An independent review (Claude Opus) and /code-review (standards and spec) found nothing blocking.

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. They are not a criterion. Each run is an import from an empty data folder holding the real config.toml and seasons.toml, so the real database is untouched. The app's CPU is sampled every second, the thread priorities are read with Get-Process, and the move probe runs for 5 s during the import. All builds are release builds (scripts/prod build).

main (e13db17) this branch
Priority of the worker threads Normal BelowNormal (Get-Process at 10 s: 1 BelowNormal; 2 distinct BelowNormal threads seen busy)
Busiest worker thread under 20 ms/s at most 63 ms/s
App CPU during the import, max 59 % of one core (UI thread during the move probe) 75 % of one core (UI thread during the move probe)
Window moves during the import, MOVE block p50 1.25, p95 2.90, p99 3.15 ms p50 1.23, p95 1.57, p99 1.88 ms
Import (91 Matches from an empty folder) database last grew 42 s after launch not comparable, see below
  • The background work itself stays far under one core: the import is network-bound. The CPU during an import is the UI thread reloading and redrawing the page.
  • The import duration of the branch is not comparable. The Riot API rate limit had been used up by an interrupted run just before, so 89 of the 91 Matches were stored when the app closed at 52 s.

Still to do on the idle reference laptop:

  • an import from an empty folder with each build, for budget 5 and the import duration;
  • recording budget 5 in docs/performance.md (left unchanged here, since desktop numbers are not a criterion).

Limits

  • ureq's DNS threads. ureq, the core's HTTP library, starts a short DNS-lookup thread per request, because the core's agents set a timeout. Windows does not inherit priority across threads, so these run at normal priority. Their CPU use is negligible. As decided, this is documented with a shortcut: comment rather than replacing ureq's resolver, which would lose its timeout.
  • One core at most is not enforced in code (no affinity, no CPU sets). It holds by measurement: the import is sequential, and the workers are I/O-bound.
  • Only the CPU priority changes. I/O and memory priority are unchanged. THREAD_MODE_BACKGROUND_BEGIN would also lower those, if a profile ever shows disk contention with the game.
  • Hovering the table during an import is not measured: the hover probe comes with #27.
Closes #30 once its measurements on the reference machine are recorded (see below). ## What changes Every thread the app starts itself goes through `spawn_guarded` (`crates/app/src/worker.rs`): the champion catalog and portraits, the game client follower, and the refresh button's sync. Each of these threads now lowers itself to `THREAD_PRIORITY_BELOW_NORMAL` before running its body. The game and the UI thread therefore keep the CPU they need (rule #1, budget 5). - The two kernel32 calls (`GetCurrentThread`, `SetThreadPriority`) are declared in place behind `#[cfg(windows)]`. No dependency is added, and `Cargo.toml` and `Cargo.lock` are unchanged. On other targets the function does nothing, and the core crate is not touched (ADR-0003). - The call is best effort: if it fails, the work still runs. - The panic → `Msg::Crashed` path and the bodies of the follower, refresh and champion threads are unchanged. - GPUI and gpui-component are untouched (ADR-0004). ## 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 Windows test `worker::tests::guarded_threads_run_below_normal_priority` reads the priority from inside a `spawn_guarded` body and expects -1. - An independent review (Claude Opus) and `/code-review` (standards and spec) found nothing blocking. ## 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. They are not a criterion. Each run is an import from an empty data folder holding the real `config.toml` and `seasons.toml`, so the real database is untouched. The app's CPU is sampled every second, the thread priorities are read with `Get-Process`, and the move probe runs for 5 s during the import. All builds are release builds (`scripts/prod build`). | | main (e13db17) | this branch | | --- | --- | --- | | Priority of the worker threads | Normal | **BelowNormal** (`Get-Process` at 10 s: 1 BelowNormal; 2 distinct BelowNormal threads seen busy) | | Busiest worker thread | under 20 ms/s | at most 63 ms/s | | App CPU during the import, max | 59 % of one core (UI thread during the move probe) | 75 % of one core (UI thread during the move probe) | | Window moves during the import, `MOVE block` | p50 1.25, p95 2.90, p99 3.15 ms | p50 1.23, p95 1.57, p99 1.88 ms | | Import (91 Matches from an empty folder) | database last grew 42 s after launch | not comparable, see below | - The background work itself stays far under one core: the import is network-bound. The CPU during an import is the UI thread reloading and redrawing the page. - The import duration of the branch is not comparable. The Riot API rate limit had been used up by an interrupted run just before, so 89 of the 91 Matches were stored when the app closed at 52 s. Still to do on the idle reference laptop: - an import from an empty folder with each build, for budget 5 and the import duration; - recording budget 5 in `docs/performance.md` (left unchanged here, since desktop numbers are not a criterion). ## Limits - **ureq's DNS threads.** ureq, the core's HTTP library, starts a short DNS-lookup thread per request, because the core's agents set a timeout. Windows does not inherit priority across threads, so these run at normal priority. Their CPU use is negligible. As decided, this is documented with a `shortcut:` comment rather than replacing ureq's resolver, which would lose its timeout. - **One core at most is not enforced in code** (no affinity, no CPU sets). It holds by measurement: the import is sequential, and the workers are I/O-bound. - **Only the CPU priority changes.** I/O and memory priority are unchanged. `THREAD_MODE_BACKGROUND_BEGIN` would also lower those, if a profile ever shows disk contention with the game. - **Hovering the table during an import** is not measured: the hover probe comes with #27.
nessar added 1 commit 2026-11-17 19:41:54 +01:00
Run background threads below normal priority (#30)
CI / core (pull_request) Successful in 1m8s
CI / core (push) Successful in 33s
dc2bcd0acd
Every thread the app starts goes through spawn_guarded: the champion
catalog and portraits, the game client follower and the refresh button.
Each now lowers itself to THREAD_PRIORITY_BELOW_NORMAL before running,
so the game and the UI keep the CPU they need (rule #1, budget 5).
Two kernel32 calls declared in place, no new dependency.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
nessar force-pushed perf/30-background-low-priority from 181090374d to dc2bcd0acd 2026-11-17 19:41:54 +01:00 Compare
nessar merged commit dc2bcd0acd into main 2026-11-17 19:42:07 +01:00
nessar deleted branch perf/30-background-low-priority 2026-11-17 19:42:09 +01:00
Sign in to join this conversation.