From c89c97a429de7e5e3a45aa42d1c23092c4be8857 Mon Sep 17 00:00:00 2001 From: LGUG2Z Date: Sat, 31 Oct 2026 14:48:18 -0700 Subject: [PATCH] perf(windows): write komorebi.hwnd.json from a writer thread, not under the window manager lock update_known_hwnds (at the end of every command) and the reaper opened, truncated and wrote komorebi.hwnd.json with the window manager locked. They now hand the list to reaper::save_known_hwnds, which keeps only the latest pending list and writes it from a thread of its own; errors are logged there as before (the reaper no longer returns the write error). BEHAVIOUR: the file is written a moment after the lock is released, not before the command returns, and when several updates arrive together only the last list is written. Nothing in the daemon reads the file. Gate needed: Windows e2e commands, lifecycle, and the lock stress. Verified: Windows clippy -D warnings via tools/fast-check PASS; e2e not run. --- komorebi/src/reaper.rs | 55 +++++++++++++++++++++++++++++----- komorebi/src/window_manager.rs | 22 ++------------ 2 files changed, 49 insertions(+), 28 deletions(-) diff --git a/komorebi/src/reaper.rs b/komorebi/src/reaper.rs index 37f8cd81..c2848733 100644 --- a/komorebi/src/reaper.rs +++ b/komorebi/src/reaper.rs @@ -134,14 +134,7 @@ fn handle_notifications(wm: Arc>) -> color_eyre::Result<()> } // Save to file - let hwnd_json = DATA_DIR.join("komorebi.hwnd.json"); - let file = OpenOptions::new() - .write(true) - .truncate(true) - .create(true) - .open(hwnd_json)?; - - serde_json::to_writer_pretty(&file, &wm.known_hwnds.keys().collect::>())?; + save_known_hwnds(wm.known_hwnds.keys().copied().collect()); } Ok(()) @@ -213,3 +206,49 @@ fn find_orphans() -> color_eyre::Result<()> { } } } + +static PENDING_HWNDS: Mutex>> = Mutex::new(None); +static PENDING_HWNDS_CHANGED: parking_lot::Condvar = parking_lot::Condvar::new(); +static HWNDS_WRITER: std::sync::Once = std::sync::Once::new(); + +/// Saves the list of known handles to `komorebi.hwnd.json`, from a thread of its own: writing the +/// file with the window manager locked made every other thread wait for the disk. Only the latest +/// list is written when several arrive before the writer is done, as the file only ever says what +/// is known now. +pub fn save_known_hwnds(hwnds: Vec) { + *PENDING_HWNDS.lock() = Some(hwnds); + HWNDS_WRITER.call_once(|| { + std::thread::spawn(|| { + loop { + let hwnds = { + let mut pending = PENDING_HWNDS.lock(); + while pending.is_none() { + PENDING_HWNDS_CHANGED.wait(&mut pending); + } + pending.take().unwrap_or_default() + }; + + let hwnd_json = DATA_DIR.join("komorebi.hwnd.json"); + match OpenOptions::new() + .write(true) + .truncate(true) + .create(true) + .open(hwnd_json) + { + Ok(file) => { + if let Err(error) = serde_json::to_writer_pretty(&file, &hwnds) { + tracing::error!( + "Failed to save list of known_hwnds on file: {}", + error + ); + } + } + Err(error) => { + tracing::error!("Failed to save list of known_hwnds on file: {}", error); + } + } + } + }); + }); + PENDING_HWNDS_CHANGED.notify_one(); +} diff --git a/komorebi/src/window_manager.rs b/komorebi/src/window_manager.rs index ab53d108..e2b734a2 100644 --- a/komorebi/src/window_manager.rs +++ b/komorebi/src/window_manager.rs @@ -5,7 +5,6 @@ use std::collections::HashMap; use std::collections::HashSet; use std::collections::VecDeque; use std::env::temp_dir; -use std::fs::OpenOptions; use std::io::ErrorKind; use std::net::Shutdown; use std::num::NonZeroUsize; @@ -4008,25 +4007,8 @@ impl WindowManager { *reaper_cache = known_hwnds.clone(); } - // Save to file - let hwnd_json = DATA_DIR.join("komorebi.hwnd.json"); - match OpenOptions::new() - .write(true) - .truncate(true) - .create(true) - .open(hwnd_json) - { - Ok(file) => { - if let Err(error) = - serde_json::to_writer_pretty(&file, &known_hwnds.keys().collect::>()) - { - tracing::error!("Failed to save list of known_hwnds on file: {}", error); - } - } - Err(error) => { - tracing::error!("Failed to save list of known_hwnds on file: {}", error); - } - } + // Save to file, from the writer thread of the reaper + crate::reaper::save_known_hwnds(known_hwnds.keys().copied().collect()); // Store new hwnds self.known_hwnds = known_hwnds;