From 1c699ed4c4cda25ba7e6d232c3be00461fcebc5f Mon Sep 17 00:00:00 2001 From: LGUG2Z Date: Mon, 16 Feb 2026 10:13:13 -0800 Subject: [PATCH] perf(bar): reduce cpu usage by sticking to logical pixels MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This was debugged by Lando Calrissian on Discord who submitted this text patch. The rest of this commit message is from Lando's investigation summary shared on Discord: Every frame, the update() method checks if egui's window rect matches the stored size_rect. The comparison code multiplied egui's outer_rect (which is in logical pixels) by scale_factor, producing physical pixel values (e.g., 3584x40 on a 2x Retina display). But size_rect stores logical pixel values (1792x20). These can never match on a HiDPI display, so every frame: 1. `size_rect != current_rect` evaluates to `true` 2. `position_bar()` is called, sending `ViewportCommand::OuterPosition` and `ViewportCommand::InnerSize` 3. Each `send_viewport_cmd` internally calls `request_repaint()` in egui 4. egui schedules another frame → go to step 1 Result: 120fps continuous rendering, consuming ~12% CPU. Debugging Method: Instrumented the code with frame counters, FPS tracking, and repaint cause logging. Systematically disabled components (repaint thread, komorebi notifications, widget rendering) to isolate the cause. Traced egui's repaint causes to context.rs:3853 (send_viewport_cmd_to), then found the size_rect mismatch firing 1112 times in 10 seconds. Fix: Removed the erroneous scale_factor multiplication from the rect comparison, since outer_rect already reports in logical pixels — the same coordinate space as size_rect. Result: ┌────────────┬───────────┬───────────────────┐ │ Metric │ Before │ After │ ├────────────┼───────────┼───────────────────┤ │ CPU (idle) │ 12-13% │ 0.0-0.1% │ ├────────────┼───────────┼───────────────────┤ │ Frames/sec │ 120 │ ~1 (timer-driven) │ ├────────────┼───────────┼───────────────────┤ │ CPU spikes │ Up to 20% │ None observed │ └────────────┴───────────┴───────────────────┘ --- komorebi-bar/src/bar.rs | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/komorebi-bar/src/bar.rs b/komorebi-bar/src/bar.rs index f7230441..896d29eb 100644 --- a/komorebi-bar/src/bar.rs +++ b/komorebi-bar/src/bar.rs @@ -1009,14 +1009,15 @@ impl eframe::App for Komobar { self.applied_theme_on_first_frame = true; } - // Check if egui's Window size is the expected one, if not, update it + // Check if egui's Window size is the expected one, if not, update it. + // Note: outer_rect is in logical pixels (same coordinate space as size_rect), + // so no scale_factor conversion is needed here. if let Some(current_rect) = ctx.input(|i| i.viewport().outer_rect) { - // Get the correct size according to scale factor let current_rect = komorebi_client::Rect { - left: (current_rect.min.x * self.scale_factor) as i32, - top: (current_rect.min.y * self.scale_factor) as i32, - right: ((current_rect.max.x - current_rect.min.x) * self.scale_factor) as i32, - bottom: ((current_rect.max.y - current_rect.min.y) * self.scale_factor) as i32, + left: current_rect.min.x as i32, + top: current_rect.min.y as i32, + right: (current_rect.max.x - current_rect.min.x) as i32, + bottom: (current_rect.max.y - current_rect.min.y) as i32, }; if self.size_rect != current_rect {