diff --git a/crates/app/Cargo.toml b/crates/app/Cargo.toml index 5b563c4..03b2c50 100644 --- a/crates/app/Cargo.toml +++ b/crates/app/Cargo.toml @@ -17,6 +17,7 @@ image = { version = "0.25.10", default-features = false, features = ["jpeg"] } [target.'cfg(windows)'.dependencies] raw-window-handle = "0.6" windows = { version = "0.62", features = [ + "Win32_Graphics_Gdi", "Win32_Security", "Win32_System_LibraryLoader", "Win32_System_Registry", diff --git a/crates/app/src/main.rs b/crates/app/src/main.rs index 392b658..594fa62 100644 --- a/crates/app/src/main.rs +++ b/crates/app/src/main.rs @@ -8,6 +8,7 @@ mod card_lift; mod card_order; mod champion_table; mod logon; +mod minimized; mod motion; mod page; #[cfg(feature = "profile")] @@ -342,6 +343,7 @@ fn main() { gpui_kit::open_window(options, cx, |window, cx| { single_instance::accept_relaunches(window); motion::follow_windows_setting(window, cx); + minimized::skip_paint_while_minimized(window); // The settings form installs the tray that hides the window. let root = cx.new(|cx| RootView::new(setup, at_logon, window, cx)); if at_logon { diff --git a/crates/app/src/minimized.rs b/crates/app/src/minimized.rs new file mode 100644 index 0000000..2a9fa58 --- /dev/null +++ b/crates/app/src/minimized.rs @@ -0,0 +1,44 @@ +//! A minimised window draws nothing, yet GPUI's vsync thread invalidates it at each vsync, and +//! each WM_PAINT went through GPUI, which has no frame to draw while minimised, then +//! DefWindowProc: the UI thread spent more minimised than visible (budget 3, +//! `docs/performance.md`). The paint is validated here instead, before GPUI sees it. + +use gpui_kit::Window; +use windows::Win32::Foundation::{HWND, LPARAM, LRESULT, WPARAM}; +use windows::Win32::Graphics::Gdi::ValidateRect; +use windows::Win32::UI::Shell::{DefSubclassProc, RemoveWindowSubclass, SetWindowSubclass}; +use windows::Win32::UI::WindowsAndMessaging::{IsIconic, WM_NCDESTROY, WM_PAINT}; + +use crate::tray::window_hwnd; + +/// Subclass ids are per procedure. +const MINIMIZED_SUBCLASS: usize = 0; + +pub(crate) fn skip_paint_while_minimized(window: &Window) { + if let Some(hwnd) = window_hwnd(window) { + // Without the subclass, GPUI handles these paints as before. + let _ = unsafe { SetWindowSubclass(hwnd, Some(paint_proc), MINIMIZED_SUBCLASS, 0) }; + } +} + +unsafe extern "system" fn paint_proc( + hwnd: HWND, + message: u32, + wparam: WPARAM, + lparam: LPARAM, + _id: usize, + _data: usize, +) -> LRESULT { + match message { + // GPUI gives its frame callback back at the restore's WM_SIZE, and draws then. + WM_PAINT if unsafe { IsIconic(hwnd) }.as_bool() => { + let _ = unsafe { ValidateRect(Some(hwnd), None) }; + return LRESULT(0); + } + WM_NCDESTROY => { + let _ = unsafe { RemoveWindowSubclass(hwnd, Some(paint_proc), MINIMIZED_SUBCLASS) }; + } + _ => {} + } + unsafe { DefSubclassProc(hwnd, message, wparam, lparam) } +} diff --git a/docs/performance.md b/docs/performance.md index 31b8f0b..54f31ff 100644 --- a/docs/performance.md +++ b/docs/performance.md @@ -22,7 +22,7 @@ Values of the 1.0 release benchmark: 2026-11-23, reference machine, release buil - **Budget 1.** Nothing a frame is asked for is cheap: each window move, hovered row or wheel step draws the whole window, at 60 frames per second (`request_layout` 0.7–1.0 ms, `prepaint` 1.6–2.2 ms, `paint` 0.7–1.3 ms at the median). On « Défi » the card grid's own prepaint takes 1.6–1.8 ms per frame, whether the window moves or the grid scrolls; on « Statistiques » the table's prepaint takes 0.8–1.0 ms and the charts' cached paint 0.3–0.4 ms; the page's layout 0.6 ms when it renders again; the rest is GPUI's own work, in pieces under the 0.5 ms the profile records. Hovering the table costs as much as moving the window: 31 % of one core at 240 moves per second, input waiting up to 5.2 ms at the 99th percentile. Scrolling the home grid draws a frame per wheel step at 4.0 ms median and 5.8–6.0 ms at the 99th percentile. - **Budget 2.** The cause of each wake-up, window visible and nothing drawn: GPUI's `VSyncProvider` thread (`gpui-pre-windows`, `platform.rs`) waits on `DwmFlush` at each vsync (61 wakes per second, about 0.3 % of one core) and invalidates every window with `RedrawWindow`; the UI thread then takes a `WM_PAINT` at each vsync (67 wakes per second, 0.4–0.7 %), runs GPUI's frame callback, finds nothing to draw and draws nothing. The app's own threads wake about once per second (under 0.05 %). This floor, 0.7–1.1 %, cannot be lowered without modifying GPUI (ADR-0004): as #32 provides, it became budget 2 on 2026-11-23, by the user's decision. It is lowered as soon as GPUI's floor drops (ratchet). -- **Budget 3.** Hidden in the notification area, the UI thread never wakes; GPUI's vsync thread alone still does, 61 times per second (0.24–0.29 %). Minimised, the vsync thread keeps invalidating the window (it only skips the frame signal for an iconic window) and the UI thread wakes about 89 times per second without drawing: 1.2–1.7 %, more than visible. +- **Budget 3.** Hidden in the notification area, the UI thread never wakes; GPUI's vsync thread alone still does, 61 times per second (0.24–0.29 %). Minimised, the vsync thread keeps invalidating the window (it only skips the frame signal for an iconic window) and the UI thread wakes about 89 times per second without drawing: 1.2–1.7 %, more than visible. Fixed after the benchmark: while the window is minimised, a subclass validates each `WM_PAINT` before GPUI sees it (`minimized.rs`), as GPUI has no frame to draw then. On the manual checks machine (240 Hz, for information), minimised: 3.03 % of one core before, 0.22 % after, the UI thread 469 ms then 31 ms over 21 s; to measure again on the reference machine. - **Budget 4.** With the full page loaded, the process commits about 108 MB of GPU memory, held in system memory on this integrated GPU (86–94 MB shared). Hiding parts with the profile build (`ARENA_TRACKER_HIDE`) splits the private memory: about 40 MB for the home grid's portraits, about 25 MB for the champion table, and 106 MB with both hidden, mostly GPUI's renderer (swap chain, atlases). The working set keeps 177 MB while the window is hidden. - **Budget 5.** Following the client (open throughout) and importing a whole history into an empty data folder, with its 175 card portraits and splashes: the work ran on the two follower threads and the art download thread, all below normal. Threads at normal priority showed at most 1.1 % in a sample; threads born and gone between two samples (up to 5 % in a sample, priority unknown) match the short DNS thread `ureq` starts per request, which `worker.rs` already notes runs at normal priority. - **Not measured: scrolling a champ select.** It needs a live Arena champ select in the game client; the captured one replays only in GPUI's test context, which draws nothing. Scrolling the home grid, which shares its cost structure, is the nearest value: 4.0 ms per frame at the median, 6.0 ms at the 99th percentile. To measure at the next champ select (see Measuring).