From 41732e2f5f27e897df671a11c6d0f98489414ae5 Mon Sep 17 00:00:00 2001 From: LGUG2Z Date: Sun, 9 Jun 2024 06:53:04 -0700 Subject: [PATCH] perf(borders): reduce mutex usage in hot path --- komorebi/src/border_manager/border.rs | 17 ++++------------- komorebi/src/border_manager/mod.rs | 16 +++++++--------- komorebi/src/process_command.rs | 3 +-- komorebi/src/stackbar_manager/stackbar.rs | 2 +- komorebi/src/static_config.rs | 7 +++---- komorebi/src/window_manager.rs | 2 +- 6 files changed, 17 insertions(+), 30 deletions(-) diff --git a/komorebi/src/border_manager/border.rs b/komorebi/src/border_manager/border.rs index 69c9d361..ee6d2583 100644 --- a/komorebi/src/border_manager/border.rs +++ b/komorebi/src/border_manager/border.rs @@ -4,7 +4,6 @@ use crate::border_manager::BORDER_WIDTH; use crate::border_manager::FOCUSED; use crate::border_manager::FOCUS_STATE; use crate::border_manager::MONOCLE; -use crate::border_manager::RECT_STATE; use crate::border_manager::STACK; use crate::border_manager::STYLE; use crate::border_manager::UNFOCUSED; @@ -127,15 +126,9 @@ impl Border { rect.add_margin(BORDER_WIDTH.load(Ordering::SeqCst)); rect.add_padding(-BORDER_OFFSET.load(Ordering::SeqCst)); - // Store the border rect so that it can be used by the callback - { - let mut rects = RECT_STATE.lock(); - rects.insert(self.hwnd, rect); - } - // Update the position of the border if required if !WindowsApi::window_rect(self.hwnd())?.eq(&rect) { - WindowsApi::set_border_pos(self.hwnd(), &rect, HWND((*Z_ORDER.lock()).into()))?; + WindowsApi::set_border_pos(self.hwnd(), &rect, HWND((Z_ORDER.load()).into()))?; } // Invalidate the rect to trigger the callback to update colours etc. @@ -157,10 +150,8 @@ impl Border { unsafe { match message { WM_PAINT => { - let rects = RECT_STATE.lock(); - - // With the rect that we stored in Self::update - if let Some(rect) = rects.get(&window.0).copied() { + // With the rect that we set in Self::update + if let Ok(rect) = WindowsApi::window_rect(window) { // Grab the focus kind for this border let focus_kind = { FOCUS_STATE @@ -195,7 +186,7 @@ impl Border { // the window was made with DWMWCP_ROUNDSMALL then this is the // wrong size. In the future we should read the DWM properties // of windows and attempt to match appropriately. - match *STYLE.lock() { + match STYLE.load() { BorderStyle::System => { if *WINDOWS_11 { RoundRect(hdc, 0, 0, rect.right, rect.bottom, 20, 20); diff --git a/komorebi/src/border_manager/mod.rs b/komorebi/src/border_manager/mod.rs index feb7a7b8..6803f6f6 100644 --- a/komorebi/src/border_manager/mod.rs +++ b/komorebi/src/border_manager/mod.rs @@ -4,6 +4,7 @@ mod border; use crossbeam_channel::Receiver; use crossbeam_channel::Sender; +use crossbeam_utils::atomic::AtomicCell; use crossbeam_utils::atomic::AtomicConsume; use komorebi_core::BorderStyle; use lazy_static::lazy_static; @@ -22,7 +23,6 @@ use windows::Win32::Foundation::HWND; use crate::workspace_reconciliator::ALT_TAB_HWND; use crate::Colour; -use crate::Rect; use crate::Rgb; use crate::WindowManager; use crate::WindowsApi; @@ -36,8 +36,8 @@ pub static BORDER_OFFSET: AtomicI32 = AtomicI32::new(-1); pub static BORDER_ENABLED: AtomicBool = AtomicBool::new(true); lazy_static! { - pub static ref Z_ORDER: Arc> = Arc::new(Mutex::new(ZOrder::Bottom)); - pub static ref STYLE: Arc> = Arc::new(Mutex::new(BorderStyle::System)); + pub static ref Z_ORDER: AtomicCell = AtomicCell::new(ZOrder::Bottom); + pub static ref STYLE: AtomicCell = AtomicCell::new(BorderStyle::System); pub static ref FOCUSED: AtomicU32 = AtomicU32::new(u32::from(Colour::Rgb(Rgb::new(66, 165, 245)))); pub static ref UNFOCUSED: AtomicU32 = @@ -50,7 +50,6 @@ lazy_static! { lazy_static! { static ref BORDERS_MONITORS: Mutex> = Mutex::new(HashMap::new()); static ref BORDER_STATE: Mutex> = Mutex::new(HashMap::new()); - static ref RECT_STATE: Mutex> = Mutex::new(HashMap::new()); static ref FOCUS_STATE: Mutex> = Mutex::new(HashMap::new()); } @@ -82,7 +81,6 @@ pub fn destroy_all_borders() -> color_eyre::Result<()> { } borders.clear(); - RECT_STATE.lock().clear(); BORDERS_MONITORS.lock().clear(); FOCUS_STATE.lock().clear(); @@ -132,7 +130,7 @@ pub fn handle_notifications(wm: Arc>) -> color_eyre::Result let is_paused = state.is_paused; let focused_monitor_idx = state.focused_monitor_idx(); let monitors = state.monitors.elements().clone(); - let pending_move_op = state.pending_move_op.clone(); + let pending_move_op = state.pending_move_op; drop(state); // If borders are disabled @@ -266,8 +264,8 @@ pub fn handle_notifications(wm: Arc>) -> color_eyre::Result for (idx, c) in ws.containers().iter().enumerate() { // Update border when moving or resizing with mouse if pending_move_op.is_some() && idx == ws.focused_container_idx() { - let restore_z_order = *Z_ORDER.lock(); - *Z_ORDER.lock() = ZOrder::TopMost; + let restore_z_order = Z_ORDER.load(); + Z_ORDER.store(ZOrder::TopMost); let mut rect = WindowsApi::window_rect( c.focused_window().copied().unwrap_or_default().hwnd(), @@ -295,7 +293,7 @@ pub fn handle_notifications(wm: Arc>) -> color_eyre::Result } } - *Z_ORDER.lock() = restore_z_order; + Z_ORDER.store(restore_z_order); continue 'monitors; } diff --git a/komorebi/src/process_command.rs b/komorebi/src/process_command.rs index 4384a65e..02929c60 100644 --- a/komorebi/src/process_command.rs +++ b/komorebi/src/process_command.rs @@ -1256,8 +1256,7 @@ impl WindowManager { } }, SocketMessage::BorderStyle(style) => { - let mut border_style = STYLE.lock(); - *border_style = style; + STYLE.store(style); } SocketMessage::BorderWidth(width) => { border_manager::BORDER_WIDTH.store(width, Ordering::SeqCst); diff --git a/komorebi/src/stackbar_manager/stackbar.rs b/komorebi/src/stackbar_manager/stackbar.rs index f7f0084c..03ef7702 100644 --- a/komorebi/src/stackbar_manager/stackbar.rs +++ b/komorebi/src/stackbar_manager/stackbar.rs @@ -198,7 +198,7 @@ impl Stackbar { bottom: height, }; - match *STYLE.lock() { + match STYLE.load() { BorderStyle::System => { if *WINDOWS_11 { RoundRect(hdc, rect.left, rect.top, rect.right, rect.bottom, 20, 20); diff --git a/komorebi/src/static_config.rs b/komorebi/src/static_config.rs index ac4816c5..8be32cf1 100644 --- a/komorebi/src/static_config.rs +++ b/komorebi/src/static_config.rs @@ -481,8 +481,8 @@ impl From<&WindowManager> for StaticConfig { transparency_alpha: Option::from( transparency_manager::TRANSPARENCY_ALPHA.load(Ordering::SeqCst), ), - border_style: Option::from(*STYLE.lock()), - border_z_order: Option::from(*Z_ORDER.lock()), + border_style: Option::from(STYLE.load()), + border_z_order: Option::from(Z_ORDER.load()), default_workspace_padding: Option::from( DEFAULT_WORKSPACE_PADDING.load(Ordering::SeqCst), ), @@ -556,8 +556,7 @@ impl StaticConfig { } } - let border_style = self.border_style.unwrap_or_default(); - *STYLE.lock() = border_style; + STYLE.store(self.border_style.unwrap_or_default()); transparency_manager::TRANSPARENCY_ENABLED .store(self.transparency.unwrap_or(false), Ordering::SeqCst); diff --git a/komorebi/src/window_manager.rs b/komorebi/src/window_manager.rs index 601a7a54..74eaf44e 100644 --- a/komorebi/src/window_manager.rs +++ b/komorebi/src/window_manager.rs @@ -164,7 +164,7 @@ impl Default for GlobalState { border_manager::UNFOCUSED.load(Ordering::SeqCst), ))), }, - border_style: *STYLE.lock(), + border_style: STYLE.load(), border_offset: border_manager::BORDER_OFFSET.load(Ordering::SeqCst), border_width: border_manager::BORDER_WIDTH.load(Ordering::SeqCst), stackbar_mode: STACKBAR_MODE.load(),