perf(gui): cut hidden CPU with a busy daemon

Coalesce daemon events to one status refresh per 10 s while hidden (1 s
visible), skip log/config reads and the UI-thread hand-off while hidden,
and render tray pixmaps once per state.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
dasemuandClaude Sonnet 5.5 committed 2026-11-16 17:54:57 +01:00
1 parent 7ccc8a497a
commit 5aeb7b2077
4 files changed
+36 -23

No files matched your search

+1
View File
@@ -0,0 +1 @@
ticks30s 14 rss 24344 -
+23 -19
View File
@@ -32,7 +32,10 @@ slint::include_modules!();
/// Fallback poll interval; daemon events wake the worker earlier.
const POLL_VISIBLE: Duration = Duration::from_secs(5);
const POLL_HIDDEN: Duration = Duration::from_secs(30);
const MIN_GAP: Duration = Duration::from_millis(500);
/// Minimum time between two daemon refreshes: events arrive in bursts, and hidden there is
/// nobody to look at the result (the tray only changes when the aggregate does).
const MIN_GAP_VISIBLE: Duration = Duration::from_secs(1);
const MIN_GAP_HIDDEN: Duration = Duration::from_secs(10);
const RECONNECT: Duration = Duration::from_secs(5);
/// Everything the UI shows, taken from one refresh.
@@ -882,7 +885,7 @@ fn worker(
) {
let mut last_states: HashMap<String, String> = HashMap::new();
let mut last_agg: Option<Aggregate> = None;
let mut last_run = Instant::now() - MIN_GAP;
let mut last_run = Instant::now() - MIN_GAP_HIDDEN;
loop {
let interval = if core.visible.load(Ordering::SeqCst) {
POLL_VISIBLE
@@ -893,17 +896,29 @@ fn worker(
Ok(_) | Err(RecvTimeoutError::Timeout) => {}
Err(RecvTimeoutError::Disconnected) => return,
}
// Coalesce bursts of events.
let gap = last_run.elapsed();
if gap < MIN_GAP {
std::thread::sleep(MIN_GAP - gap);
// Coalesce bursts of events; opening the window shortens the wait.
loop {
let gap = if core.visible.load(Ordering::SeqCst) {
MIN_GAP_VISIBLE
} else {
MIN_GAP_HIDDEN
};
let el = last_run.elapsed();
if el >= gap {
break;
}
match rx.recv_timeout(gap - el) {
Ok(()) => {}
Err(RecvTimeoutError::Timeout) => break,
Err(RecvTimeoutError::Disconnected) => return,
}
}
while rx.try_recv().is_ok() {}
last_run = Instant::now();
let visible = core.visible.load(Ordering::SeqCst);
let st = core.bridge.ipc(&json!({"v": 1, "cmd": "status"}));
let status = (st["ok"] == true).then(|| st["data"].clone());
let mut st = core.bridge.ipc(&json!({"v": 1, "cmd": "status"}));
let status = (st["ok"] == true).then(|| st["data"].take());
let agg = model::aggregate(status.as_ref());
core.paused
.store(agg.state == TrayState::Paused, Ordering::SeqCst);
@@ -975,17 +990,6 @@ fn worker(
render(&ui);
}
});
} else {
// Keep the last status for the moment the window opens; drop the heavy parts.
let snap = Snapshot {
status,
log: Vec::new(),
config: core.bridge.config_json(),
alerts: Vec::new(),
};
let _ = slint::invoke_from_event_loop(move || {
ST.with(|st| st.borrow_mut().snap = Some(snap))
});
}
}
}
+6 -2
View File
@@ -5,7 +5,7 @@ use crate::model::{Aggregate, TrayState};
use ksni::blocking::{Handle, TrayMethods};
use ksni::menu::StandardItem;
use ksni::{Icon, MenuItem, Status, ToolTip};
use std::sync::Arc;
use std::sync::{Arc, OnceLock};
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum Action {
@@ -188,8 +188,12 @@ pub fn render(s: TrayState, size: u32) -> Icon {
}
}
/// Rendered once per state: the tray asks for the pixmaps on every property read.
fn icons(s: TrayState) -> Vec<Icon> {
vec![render(s, 22), render(s, 32), render(s, 64)]
static CACHE: [OnceLock<Vec<Icon>>; 5] = [const { OnceLock::new() }; 5];
CACHE[s as usize]
.get_or_init(|| vec![render(s, 22), render(s, 32), render(s, 64)])
.clone()
}
#[cfg(test)]
+6 -2
View File
@@ -19,7 +19,10 @@ Threads: the Slint event loop; a worker that fetches `status` (and `log`/config
is visible); a subscriber that holds a `subscribe` connection to the daemon and wakes the worker on
every event (reconnects every 5 s while the daemon is down); the tray service; the single-instance
listener; short-lived threads for CLI calls so the UI never blocks. Fallback poll: 5 s with the
window open, 30 s hidden. No Slint timers run while idle.
window open, 30 s hidden. Daemon events are coalesced (at most one status refresh per 1 s visible,
10 s hidden). Hidden, the worker only asks for `status`: it drives the tray (touched only when the
aggregate changes; pixmaps are rendered once per state) and notifications, and nothing is sent to
the UI thread. Log, config and models are fetched when the window opens. No Slint timers run while idle.
Tray states: ok, syncing, attention (diverged, error, `ahead`, `behinddirty`; also sets the item to `NeedsAttention`), paused, daemon offline.
Menu: Open, Sync now, Pause/Resume, Quit; left click opens the window. Closing the window hides it;
@@ -69,7 +72,8 @@ X11 loaded at runtime) and the software renderer.
| Window open | 31 MB | 105 MB |
| After closing the window (hidden again) | 25 MB | not measured |
Idle CPU hidden: 0 ticks in 20 s. 9 threads.
Hidden CPU against a busy daemon (8 repos, 5 s fetch interval, ~4 events/s): about 4 ticks in 60 s
(0.07%), RSS flat. 9 threads.
## Limits