Skip to content

Commit 32d28bf

Browse files
committed
perf: throttle mouse events and cache window list queries
Performance optimizations from upstream PR jordanbaird#842 patterns: 1. Mouse Event Throttling: - Process 1 in 5 mouse moved events (80% reduction) - Significantly reduces CPU usage during mouse movement - Uses simple counter-based throttling for minimal overhead 2. Window List Caching: - Cache menu bar window list for 0.5 seconds - Reduces expensive WindowServer IPC calls - Hit-testing now uses cached data instead of fresh queries Combined Impact: - Reduced CPU usage during mouse interaction - Lower WindowServer communication overhead - Smoother app responsiveness - Better battery life on laptops Build: Verified with xcodebuild - BUILD SUCCEEDED
1 parent 721db1d commit 32d28bf

File tree

1 file changed

+20
-3
lines changed

1 file changed

+20
-3
lines changed

Ice/Events/HIDEventManager.swift

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,15 @@ final class HIDEventManager: ObservableObject {
2121
/// Storage for internal observers.
2222
private var cancellables = Set<AnyCancellable>()
2323

24+
/// Counter for throttling mouse moved events.
25+
private var mouseMovedCounter = 0
26+
27+
/// Cached window list for hit-testing.
28+
private var cachedWindowList: [CGWindowID] = []
29+
30+
/// Last time the window list was refreshed.
31+
private var lastWindowListRefresh: Date = .distantPast
32+
2433
/// History of the manager's enabled states.
2534
private var enabledStateStack = [Bool]()
2635

@@ -90,7 +99,11 @@ final class HIDEventManager: ObservableObject {
9099
option: .listenOnly
91100
) { [weak self] _, event in
92101
if let self, isEnabled, let appState, let screen = bestScreen(appState: appState) {
93-
handleShowOnHover(appState: appState, screen: screen)
102+
mouseMovedCounter += 1
103+
if mouseMovedCounter >= 5 {
104+
mouseMovedCounter = 0
105+
handleShowOnHover(appState: appState, screen: screen)
106+
}
94107
}
95108
return event
96109
}
@@ -494,8 +507,12 @@ extension HIDEventManager {
494507
guard let mouseLocation = MouseHelpers.locationCoreGraphics else {
495508
return false
496509
}
497-
let windowIDs = Bridging.getMenuBarWindowList(option: [.onScreen, .activeSpace, .itemsOnly])
498-
return windowIDs.contains { windowID in
510+
let now = Date()
511+
if now.timeIntervalSince(lastWindowListRefresh) > 0.5 {
512+
cachedWindowList = Bridging.getMenuBarWindowList(option: [.onScreen, .activeSpace, .itemsOnly])
513+
lastWindowListRefresh = now
514+
}
515+
return cachedWindowList.contains { windowID in
499516
guard let bounds = Bridging.getWindowBounds(for: windowID) else {
500517
return false
501518
}

0 commit comments

Comments
 (0)