Skip to content

Commit

Permalink
perf(borders): introduce state snapshot checks
Browse files Browse the repository at this point in the history
This commit introduce state snapshot checks in the border manager, which
will ensure that we don't even attempt to acquire any mutex locks if the
state hasn't changed.
  • Loading branch information
LGUG2Z committed Jun 9, 2024
1 parent 41732e2 commit 6b9a084
Show file tree
Hide file tree
Showing 8 changed files with 53 additions and 16 deletions.
11 changes: 10 additions & 1 deletion komorebi-core/src/arrangement.rs
Original file line number Diff line number Diff line change
Expand Up @@ -604,7 +604,16 @@ impl Arrangement for CustomLayout {
}

#[derive(
Clone, Copy, Debug, Serialize, Deserialize, Display, EnumString, ValueEnum, JsonSchema,
Clone,
Copy,
Debug,
Serialize,
Deserialize,
Display,
EnumString,
ValueEnum,
JsonSchema,
PartialEq,
)]
pub enum Axis {
Horizontal,
Expand Down
10 changes: 5 additions & 5 deletions komorebi-core/src/custom_layout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use serde::Serialize;

use crate::Rect;

#[derive(Clone, Debug, Serialize, Deserialize, JsonSchema)]
#[derive(Clone, Debug, Serialize, Deserialize, JsonSchema, PartialEq)]
pub struct CustomLayout(Vec<Column>);

impl Deref for CustomLayout {
Expand Down Expand Up @@ -250,26 +250,26 @@ impl CustomLayout {
}
}

#[derive(Clone, Copy, Debug, Serialize, Deserialize, JsonSchema)]
#[derive(Clone, Copy, Debug, Serialize, Deserialize, JsonSchema, PartialEq)]
#[serde(tag = "column", content = "configuration")]
pub enum Column {
Primary(Option<ColumnWidth>),
Secondary(Option<ColumnSplitWithCapacity>),
Tertiary(ColumnSplit),
}

#[derive(Clone, Copy, Debug, Serialize, Deserialize, JsonSchema)]
#[derive(Clone, Copy, Debug, Serialize, Deserialize, JsonSchema, PartialEq)]
pub enum ColumnWidth {
WidthPercentage(f32),
}

#[derive(Clone, Copy, Debug, Serialize, Deserialize, JsonSchema)]
#[derive(Clone, Copy, Debug, Serialize, Deserialize, JsonSchema, PartialEq)]
pub enum ColumnSplit {
Horizontal,
Vertical,
}

#[derive(Clone, Copy, Debug, Serialize, Deserialize, JsonSchema)]
#[derive(Clone, Copy, Debug, Serialize, Deserialize, JsonSchema, PartialEq)]
pub enum ColumnSplitWithCapacity {
Horizontal(usize),
Vertical(usize),
Expand Down
2 changes: 1 addition & 1 deletion komorebi-core/src/layout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use crate::CustomLayout;
use crate::DefaultLayout;
use crate::Direction;

#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema)]
#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema, PartialEq)]
pub enum Layout {
Default(DefaultLayout),
Custom(CustomLayout),
Expand Down
20 changes: 15 additions & 5 deletions komorebi/src/border_manager/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ use std::sync::Arc;
use std::sync::OnceLock;
use windows::Win32::Foundation::HWND;

use crate::ring::Ring;
use crate::workspace_reconciliator::ALT_TAB_HWND;
use crate::Colour;
use crate::Rgb;
Expand Down Expand Up @@ -121,18 +122,25 @@ pub fn handle_notifications(wm: Arc<Mutex<WindowManager>>) -> color_eyre::Result
let receiver = event_rx();
event_tx().send(Notification)?;

'receiver: for _ in receiver {
let mut borders = BORDER_STATE.lock();
let mut borders_monitors = BORDERS_MONITORS.lock();
let mut latest_snapshot = Ring::default();

'receiver: for _ in receiver {
// Check the wm state every time we receive a notification
let state = wm.lock();
let is_paused = state.is_paused;
let focused_monitor_idx = state.focused_monitor_idx();
let monitors = state.monitors.elements().clone();
let monitors = state.monitors.clone();
let pending_move_op = state.pending_move_op;
drop(state);

if monitors == latest_snapshot {
tracing::trace!("monitor state matches latest snapshot, skipping notification");
continue 'receiver;
}

let mut borders = BORDER_STATE.lock();
let mut borders_monitors = BORDERS_MONITORS.lock();

// If borders are disabled
if !BORDER_ENABLED.load_consume()
// Or if the wm is paused
Expand All @@ -149,7 +157,7 @@ pub fn handle_notifications(wm: Arc<Mutex<WindowManager>>) -> color_eyre::Result
continue 'receiver;
}

'monitors: for (monitor_idx, m) in monitors.iter().enumerate() {
'monitors: for (monitor_idx, m) in monitors.elements().iter().enumerate() {
// Only operate on the focused workspace of each monitor
if let Some(ws) = m.focused_workspace() {
// Workspaces with tiling disabled don't have borders
Expand Down Expand Up @@ -337,6 +345,8 @@ pub fn handle_notifications(wm: Arc<Mutex<WindowManager>>) -> color_eyre::Result
}
}
}

latest_snapshot = monitors;
}

Ok(())
Expand Down
11 changes: 10 additions & 1 deletion komorebi/src/monitor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,16 @@ use crate::ring::Ring;
use crate::workspace::Workspace;

#[derive(
Debug, Clone, Serialize, Deserialize, Getters, CopyGetters, MutGetters, Setters, JsonSchema,
Debug,
Clone,
Serialize,
Deserialize,
Getters,
CopyGetters,
MutGetters,
Setters,
JsonSchema,
PartialEq,
)]
pub struct Monitor {
#[getset(get_copy = "pub", set = "pub")]
Expand Down
2 changes: 1 addition & 1 deletion komorebi/src/ring.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use schemars::JsonSchema;
use serde::Deserialize;
use serde::Serialize;

#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema)]
#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema, PartialEq)]
pub struct Ring<T> {
elements: VecDeque<T>,
focused: usize,
Expand Down
2 changes: 1 addition & 1 deletion komorebi/src/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ use crate::PERMAIGNORE_CLASSES;
use crate::REGEX_IDENTIFIERS;
use crate::WSL2_UI_PROCESSES;

#[derive(Debug, Default, Clone, Copy, Deserialize, JsonSchema)]
#[derive(Debug, Default, Clone, Copy, Deserialize, JsonSchema, PartialEq)]
pub struct Window {
pub hwnd: isize,
}
Expand Down
11 changes: 10 additions & 1 deletion komorebi/src/workspace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,16 @@ use crate::REMOVE_TITLEBARS;

#[allow(clippy::struct_field_names)]
#[derive(
Debug, Clone, Serialize, Deserialize, Getters, CopyGetters, MutGetters, Setters, JsonSchema,
Debug,
Clone,
Serialize,
Deserialize,
Getters,
CopyGetters,
MutGetters,
Setters,
JsonSchema,
PartialEq,
)]
pub struct Workspace {
#[getset(get = "pub", set = "pub")]
Expand Down

0 comments on commit 6b9a084

Please sign in to comment.