From ed822330301a1895df50efa66ce3a6493c5059b6 Mon Sep 17 00:00:00 2001 From: Lukas Wirth Date: Mon, 20 Oct 2025 23:03:28 +0200 Subject: [PATCH] gpui: Box `Window` instances (#40733) We very frequently move this in and out of the windows slot map on `update_window_id` calls (and we call this a lot!). This alone showed up as `memmove`s at roughly 1% perf in Instruments when scrolling a buffer which makes sense, `Window` itself is 4kb in size. The fix is simple, just box the `Window` instances, moving a pointer is cheap in comparison. Release Notes: - N/A *or* Added/Fixed/Improved ... --- crates/gpui/src/app.rs | 10 +++++----- crates/gpui/src/app/test_context.rs | 2 +- crates/gpui/src/window.rs | 2 +- 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/crates/gpui/src/app.rs b/crates/gpui/src/app.rs index a5f53c2dad1019ad5ccd3427696a4ceb964c1e61..d4bd7798187a5b7a358106965d9e41fd85efeffe 100644 --- a/crates/gpui/src/app.rs +++ b/crates/gpui/src/app.rs @@ -553,7 +553,7 @@ pub struct App { pub(crate) entities: EntityMap, pub(crate) window_update_stack: Vec, pub(crate) new_entity_observers: SubscriberSet, - pub(crate) windows: SlotMap>, + pub(crate) windows: SlotMap>>, pub(crate) window_handles: FxHashMap, pub(crate) focus_handles: Arc, pub(crate) keymap: Rc>, @@ -964,7 +964,7 @@ impl App { clear.clear(); cx.window_handles.insert(id, window.handle); - cx.windows.get_mut(id).unwrap().replace(window); + cx.windows.get_mut(id).unwrap().replace(Box::new(window)); Ok(handle) } Err(e) => { @@ -1239,7 +1239,7 @@ impl App { .windows .values() .filter_map(|window| { - let window = window.as_ref()?; + let window = window.as_deref()?; window.invalidator.is_dirty().then_some(window.handle) }) .collect::>() @@ -1320,7 +1320,7 @@ impl App { fn apply_refresh_effect(&mut self) { for window in self.windows.values_mut() { - if let Some(window) = window.as_mut() { + if let Some(window) = window.as_deref_mut() { window.refreshing = true; window.invalidator.set_dirty(true); } @@ -2199,7 +2199,7 @@ impl AppContext for App { .windows .get(window.id) .context("window not found")? - .as_ref() + .as_deref() .expect("attempted to read a window that is already on the stack"); let root_view = window.root.clone().unwrap(); diff --git a/crates/gpui/src/app/test_context.rs b/crates/gpui/src/app/test_context.rs index 2e24c3ba195f9fc8f72c43cd3da09778e8b2b3e9..cba6de6e31901a43b7c80cff8460af6e0e6a09cc 100644 --- a/crates/gpui/src/app/test_context.rs +++ b/crates/gpui/src/app/test_context.rs @@ -455,7 +455,7 @@ impl TestAppContext { .windows .get_mut(window.id) .unwrap() - .as_mut() + .as_deref_mut() .unwrap() .platform_window .as_test() diff --git a/crates/gpui/src/window.rs b/crates/gpui/src/window.rs index 5534a4a8ddf98c2bf5d26637b7af3a499dd63ae1..6855874bae25db1b0990541902333e4e72a283b3 100644 --- a/crates/gpui/src/window.rs +++ b/crates/gpui/src/window.rs @@ -4718,7 +4718,7 @@ impl WindowHandle { .get(self.id) .and_then(|window| { window - .as_ref() + .as_deref() .and_then(|window| window.root.clone()) .map(|root_view| root_view.downcast::()) })