From a68a09d27537f29c8e707bb1c296cee8045d1c93 Mon Sep 17 00:00:00 2001 From: Neel Date: Tue, 17 Nov 2026 22:36:25 +0000 Subject: [PATCH] gpui: Draw test windows from a queued task instead of after every update In test mode, flush_effects drew every dirty window whenever the effect queue drained, so a window was redrawn after every update. Production schedules a frame and draws once per frame instead. In UI-heavy test suites drawing was most of the CPU time: skipping it cut 65-73% of the CPU of Delta's app test groups. Test mode now queues one draw task per dirty window on the test scheduler, so updates made before the task runs share a draw. Simulated input, dispatched actions and debug_bounds draw a pending frame first, so they see the current frame the way platform input does. --- crates/gpui/src/app.rs | 55 +++++++++++++++++----- crates/gpui/src/app/test_app.rs | 2 + crates/gpui/src/app/test_context.rs | 7 ++- crates/gpui/src/app/visual_test_context.rs | 3 ++ crates/gpui/src/window.rs | 25 +++++++++- 5 files changed, 78 insertions(+), 14 deletions(-) diff --git a/crates/gpui/src/app.rs b/crates/gpui/src/app.rs index 64a79c79421..67609d7214e 100644 --- a/crates/gpui/src/app.rs +++ b/crates/gpui/src/app.rs @@ -2222,6 +2222,48 @@ impl App { self.svg_renderer.clone() } + /// Queues a task to draw each dirty window, standing in for the frame a + /// platform would deliver. + /// + /// Updates made before the task runs share one draw, as they share one + /// frame in production. Drawing after every update instead made drawing + /// most of the CPU time of UI-heavy test suites. Tests that read the + /// rendered frame after an update must let the task run first, for + /// example with `run_until_parked`; the simulated input helpers draw + /// pending frames themselves. + #[cfg(any(test, feature = "test-support"))] + fn schedule_test_draws(&mut self) { + let windows = self + .windows + .values() + .filter_map(|window| { + let window = window.as_deref()?; + (window.invalidator.is_dirty() && !window.test_draw_scheduled.replace(true)) + .then(|| (window.handle, window.test_draw_scheduled.clone())) + }) + .collect::>(); + for (window, scheduled) in windows { + self.spawn(async move |cx| { + scheduled.set(false); + // The scheduler can poll this task while an update blocks on + // the executor. That update's own flush schedules the draw + // again once it releases the app. + let Some(app) = cx.app.upgrade() else { + return; + }; + let Ok(mut app) = app.try_borrow_mut() else { + return; + }; + app.update(|cx| { + window + .update(cx, |_, window, cx| window.draw_pending_test_frame(cx)) + .ok(); + }); + }) + .detach(); + } + } + pub(crate) fn push_effect(&mut self, effect: Effect) { match &effect { Effect::Notify { emitter } => { @@ -2281,18 +2323,7 @@ impl App { } else { #[cfg(any(test, feature = "test-support"))] if matches!(self.mode, GpuiMode::Test { .. }) { - for window in self - .windows - .values() - .filter_map(|window| { - let window = window.as_deref()?; - window.invalidator.is_dirty().then_some(window.handle) - }) - .collect::>() - { - self.update_window(window, |_, window, cx| window.draw(cx).clear(cx)) - .unwrap(); - } + self.schedule_test_draws(); } if self.pending_effects.is_empty() { diff --git a/crates/gpui/src/app/test_app.rs b/crates/gpui/src/app/test_app.rs index dada94848d2..aea5b85a1cd 100644 --- a/crates/gpui/src/app/test_app.rs +++ b/crates/gpui/src/app/test_app.rs @@ -388,6 +388,7 @@ impl TestAppWindow { let mut app = self.app.borrow_mut(); let any_handle: AnyWindowHandle = self.handle.into(); app.update_window(any_handle, |_, window, cx| { + window.draw_pending_test_frame(cx); window.dispatch_keystroke(keystroke, cx); }) .unwrap(); @@ -463,6 +464,7 @@ impl TestAppWindow { let mut app = self.app.borrow_mut(); let any_handle: AnyWindowHandle = self.handle.into(); app.update_window(any_handle, |_, window, cx| { + window.draw_pending_test_frame(cx); window.dispatch_event(platform_input, cx); }) .unwrap(); diff --git a/crates/gpui/src/app/test_context.rs b/crates/gpui/src/app/test_context.rs index 4e6bf835798..fbbca1a8df7 100644 --- a/crates/gpui/src/app/test_context.rs +++ b/crates/gpui/src/app/test_context.rs @@ -619,6 +619,7 @@ impl TestAppContext { { window .update(self, |_, window, cx| { + window.draw_pending_test_frame(cx); window.dispatch_action(action.boxed_clone(), cx) }) .unwrap(); @@ -663,6 +664,7 @@ impl TestAppContext { /// dispatches a single Keystroke (see also `simulate_keystrokes` and `simulate_input`) pub fn dispatch_keystroke(&mut self, window: AnyWindowHandle, keystroke: Keystroke) { self.update_window(window, |_, window, cx| { + window.draw_pending_test_frame(cx); window.dispatch_keystroke(keystroke, cx) }) .unwrap(); @@ -1040,7 +1042,10 @@ impl VisualTestContext { /// Returns `None` in builds without `debug_assertions`, which don't record /// selectors; see [`crate::InteractiveElement::debug_selector`]. pub fn debug_bounds(&mut self, selector: &'static str) -> Option> { - self.update(|window, _| window.rendered_frame.debug_bounds.get(selector).copied()) + self.update(|window, cx| { + window.draw_pending_test_frame(cx); + window.rendered_frame.debug_bounds.get(selector).copied() + }) } /// Draw an element to the window. Useful for simulating events or actions diff --git a/crates/gpui/src/app/visual_test_context.rs b/crates/gpui/src/app/visual_test_context.rs index 23fedc4ec5b..f705400d136 100644 --- a/crates/gpui/src/app/visual_test_context.rs +++ b/crates/gpui/src/app/visual_test_context.rs @@ -235,6 +235,7 @@ impl VisualTestAppContext { /// Dispatches a single keystroke to a window. pub fn dispatch_keystroke(&mut self, window: AnyWindowHandle, keystroke: Keystroke) { self.update_window(window, |_, window, cx| { + window.draw_pending_test_frame(cx); window.dispatch_keystroke(keystroke, cx); }) .ok(); @@ -325,6 +326,7 @@ impl VisualTestAppContext { /// Simulates an input event on the given window. pub fn simulate_event(&mut self, window: AnyWindowHandle, event: E) { self.update_window(window, |_, window, cx| { + window.draw_pending_test_frame(cx); window.dispatch_event(event.to_platform_input(), cx); }) .ok(); @@ -334,6 +336,7 @@ impl VisualTestAppContext { /// Dispatches an action to the given window. pub fn dispatch_action(&mut self, window: AnyWindowHandle, action: impl Action) { self.update_window(window, |_, window, cx| { + window.draw_pending_test_frame(cx); window.dispatch_action(action.boxed_clone(), cx); }) .ok(); diff --git a/crates/gpui/src/window.rs b/crates/gpui/src/window.rs index 9432b5db092..78d81bea1ea 100644 --- a/crates/gpui/src/window.rs +++ b/crates/gpui/src/window.rs @@ -1287,6 +1287,8 @@ pub struct Window { SingleSubscriberSet bool>>, hovered: Rc>, pub(crate) needs_present: Rc>, + #[cfg(any(test, feature = "test-support"))] + pub(crate) test_draw_scheduled: Rc>, /// The rendered scene's [`Scene::animation_frame_interval`]. While it is /// `Some`, the scene must keep being presented even when nothing is redrawn. scene_animation_interval: Rc>>, @@ -2283,7 +2285,11 @@ impl Window { let mut cx = cx.to_async(); Box::new(move |event| { handle - .update(&mut cx, |_, window, cx| window.dispatch_event(event, cx)) + .update(&mut cx, |_, window, cx| { + #[cfg(any(test, feature = "test-support"))] + window.draw_pending_test_frame(cx); + window.dispatch_event(event, cx) + }) .with_context(|| { format!("dispatching input for window {:?}", handle.window_id()) }) @@ -2441,6 +2447,8 @@ impl Window { visibility_observers: SingleSubscriberSet::new(), hovered, needs_present, + #[cfg(any(test, feature = "test-support"))] + test_draw_scheduled: Rc::default(), scene_animation_interval, input_rate_tracker, #[cfg(feature = "profiler")] @@ -2875,6 +2883,8 @@ impl Window { cx.defer(move |cx| { window .update(cx, |_, window, cx| { + #[cfg(any(test, feature = "test-support"))] + window.draw_pending_test_frame(cx); let node_id = window.focus_node_id_in_rendered_frame(focus_id); window.dispatch_action_on_node(node_id, action.as_ref(), cx); }) @@ -3777,6 +3787,19 @@ impl Window { self.capslock } + /// Draws a test window whose content changed since its last frame. + /// + /// Test mode draws windows from a queued task rather than after every + /// update, so input dispatched before that task runs would otherwise hit + /// a stale frame. On a real platform, frames arrive far more often than + /// input, so the frame that input lands on is current. + #[cfg(any(test, feature = "test-support"))] + pub(crate) fn draw_pending_test_frame(&mut self, cx: &mut App) { + if matches!(cx.mode, crate::app::GpuiMode::Test { .. }) && self.invalidator.is_dirty() { + self.draw(cx).clear(cx); + } + } + /// Produces a new frame and assigns it to `rendered_frame`. To actually show /// the contents of the new [`Scene`], use [`Self::present`]. #[profiling::function]