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.
This commit is contained in:
Neel
2026-11-17 22:36:25 +00:00
parent 9925feb84c
commit a68a09d275
5 changed files with 78 additions and 14 deletions
+43 -12
View File
@@ -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::<Vec<_>>();
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::<Vec<_>>()
{
self.update_window(window, |_, window, cx| window.draw(cx).clear(cx))
.unwrap();
}
self.schedule_test_draws();
}
if self.pending_effects.is_empty() {
+2
View File
@@ -388,6 +388,7 @@ impl<V: 'static + Render> TestAppWindow<V> {
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<V: 'static + Render> TestAppWindow<V> {
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();
+6 -1
View File
@@ -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<Bounds<Pixels>> {
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
@@ -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<E: InputEvent>(&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();
+24 -1
View File
@@ -1287,6 +1287,8 @@ pub struct Window {
SingleSubscriberSet<Box<dyn FnMut(WindowVisibility, &mut Window, &mut App) -> bool>>,
hovered: Rc<Cell<bool>>,
pub(crate) needs_present: Rc<Cell<bool>>,
#[cfg(any(test, feature = "test-support"))]
pub(crate) test_draw_scheduled: Rc<Cell<bool>>,
/// 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<Cell<Option<Duration>>>,
@@ -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]