Open source Loom alternative. Beautiful, shareable screen recordings.
Rust appcapcossloommacnextjsnextjs15open-sourceossreactrecordrustscreen-capturescreen-recorderscreenshotsolidjstauritauri-apptypescriptvite
perf(desktop-gpui): sync only the sidebar fields the frame draws
Pravin08aa56cd5a68
diff --git a/apps/desktop-gpui/src/editor_bench.rs b/apps/desktop-gpui/src/editor_bench.rsindex 824d5cd19..55cc468fd 100644--- a/apps/desktop-gpui/src/editor_bench.rs+++ b/apps/desktop-gpui/src/editor_bench.rs@@ -141,7 +141,7 @@ fn seed_caption_fields() {let step = view.total_duration() / SEEDED_CAPTION_SEGMENTS as f64;let Some(timeline) = view.project.timeline.as_mut() else {- return;+ panic!("timeline-bench: the fixture has no timeline to seed caption fields");};timeline.caption_segments = (0..SEEDED_CAPTION_SEGMENTS).map(|index| cap_project::CaptionTrackSegment {@@ -167,6 +167,11 @@ fn seed_caption_fields(view.ensure_field(key, window, cx);}}+ assert!(+ view.fields.len() >= SEEDED_CAPTION_SEGMENTS * 3,+ "timeline-bench: only {} caption fields were seeded",+ view.fields.len()+ );}pub(crate) async fn run(handle: WindowHandle<EditorWindow>, cx: &mut AsyncApp) {@@ -288,6 +293,21 @@ pub(crate) async fn run(handle: WindowHandle<EditorWindow>, cx: &mut AsyncApp) {);}+ // The visible case models the production sidebar on the Captions tab with+ // caption 0 selected. `TickFields` seeds the fields without changing the+ // tab, so set that state explicitly before either sync loop.+ let Ok(()) = handle.update(cx, |view, _, cx| {+ view.sidebar.tab = SidebarTab::Captions;+ view.selection = Some(crate::editor_edits::Selection::single(+ editor_timeline::TrackKind::Caption,+ 0,+ ));+ cx.notify();+ }) else {+ eprintln!("timeline-bench: the editor window closed");+ std::process::exit(1);+ };+// The per-render field sync on its own, with the seeded fields in place.let mut sync_us = Vec::with_capacity(MEASURED_FRAMES);for iteration in 0..WARMUP_FRAMES + MEASURED_FRAMES {@@ -311,5 +331,57 @@ pub(crate) async fn run(handle: WindowHandle<EditorWindow>, cx: &mut AsyncApp) {}draw_bench::report("timeline-bench", "sync-fields", &mut sync_us);+ // The production path over the same seeded state: the Captions tab with+ // caption 0 selected makes `prepare_sidebar_fields` list exactly these+ // three keys (`TrackKind::Caption`'s arm), so this is what a render now+ // syncs. The+ // all-fields case above is the faithful old path. The keys are written out+ // rather than derived by calling `prepare_sidebar_fields` because that+ // call would also time key derivation and field creation, not the sync.+ let visible = [+ FieldKey::CaptionText(0),+ FieldKey::CaptionStart(0),+ FieldKey::CaptionEnd(0),+ ];+ let Ok(missing) = handle.update(cx, |view, _, _| {+ visible+ .iter()+ .filter(|key| !view.fields.contains_key(key))+ .count()+ }) else {+ eprintln!("timeline-bench: the editor window closed");+ std::process::exit(1);+ };+ assert_eq!(+ missing, 0,+ "timeline-bench: the visible caption fields were not seeded"+ );+ let mut visible_sync_us = Vec::with_capacity(MEASURED_FRAMES);+ for iteration in 0..WARMUP_FRAMES + MEASURED_FRAMES {+ let Ok(elapsed) = handle.update(cx, |view, window, cx| {+ if view.playing {+ view.stop_for_measurement(cx);+ }+ let started = Instant::now();+ view.sync_visible_fields(&visible, window, cx);+ started.elapsed()+ }) else {+ eprintln!("timeline-bench: the editor window closed");+ std::process::exit(1);+ };+ if iteration >= WARMUP_FRAMES {+ visible_sync_us.push(elapsed.as_secs_f64() * 1e6);+ }+ }+ println!(+ "timeline-bench: sync-visible-fields over {} fields",+ visible.len()+ );+ draw_bench::report(+ "timeline-bench",+ "sync-visible-fields",+ &mut visible_sync_us,+ );+std::process::exit(0);}diff --git a/apps/desktop-gpui/src/editor_panels.rs b/apps/desktop-gpui/src/editor_panels.rsindex ec9bdbc5d..cdedff72c 100644--- a/apps/desktop-gpui/src/editor_panels.rs+++ b/apps/desktop-gpui/src/editor_panels.rs@@ -2727,27 +2727,60 @@ impl EditorWindow {self.fields.insert(key, input);}- /// The `createWritableMemo` half: re-derive a field's text whenever the- /// value moves under it (an undo, a preset, a timeline drag), but never- /// while it has focus. Runs from `render`, like `sync_hex_inputs`, because- /// the focus test needs a `&Window`.+ /// The all-fields path: every field ever created, whether the frame draws+ /// it or not. Kept as the faithful "before" for the benchmark and the+ /// equivalence tests; production renders go through+ /// [`Self::sync_visible_fields`].pub(crate) fn sync_fields(&mut self, window: &mut Window, cx: &mut Context<Self>) {- // Every field ever created is checked on every render, so the value is- // written into one reused buffer and only copied out when it moved.+ // The value is written into one reused buffer and only copied out+ // when it moved.let mut value = String::new();for (key, input) in &self.fields {- let state = input.read(cx);- if state.is_focused(window) {- continue;- }- value.clear();- if self.write_field_value(*key, &mut value).is_none() {+ self.sync_field(input, *key, &mut value, window, cx);+ }+ }++ /// The production path: only the fields [`Self::prepare_sidebar_fields`]+ /// says the next frame draws. A field that is not listed keeps its old+ /// text until a later frame lists it again -- that is how a hidden field+ /// catches up when it becomes visible. Focused fields are never written,+ /// exactly as on the all-fields path.+ pub(crate) fn sync_visible_fields(+ &mut self,+ keys: &[FieldKey],+ window: &mut Window,+ cx: &mut Context<Self>,+ ) {+ let mut value = String::new();+ for key in keys {+ let Some(input) = self.fields.get(key) else {continue;- }- if state.text() != value {- let text = value.clone();- input.update(cx, |input, cx| input.set_text(text, cx));- }+ };+ self.sync_field(input, *key, &mut value, window, cx);+ }+ }++ /// One field, shared by both sync paths so the visible-only path cannot+ /// drift from the all-fields one.+ fn sync_field(+ &self,+ input: &Entity<ui::TextInputState>,+ key: FieldKey,+ value: &mut String,+ window: &Window,+ cx: &mut Context<Self>,+ ) {+ let state = input.read(cx);+ if state.is_focused(window) {+ return;+ }+ value.clear();+ if self.write_field_value(key, value).is_none() {+ return;+ }+ if state.text() != value {+ let text = value.clone();+ input.update(cx, |input, cx| input.set_text(text, cx));}}@@ -7930,7 +7963,7 @@ impl EditorWindow {}impl EditorWindow {- /// Every text field the frame about to be built will draw.+ /// Every text field the frame about to be built may draw.////// Creating one needs `&mut Window`, and the sidebar's render chain is/// threaded with `&self`, so the set is computed here from the same state@@ -7942,9 +7975,8 @@ impl EditorWindow {let mut colors: Vec<ColorTarget> = Vec::new();// The crop dialog's four `BoundInput`s, while it is open. Created up- // front rather than pushed onto `fields`, because the match below- // bails early when the project has no timeline and the dialog is- // independent of it.+ // front because the match below bails early when the project has no+ // timeline and the dialog is independent of it.if self.crop.is_some() {use crate::editor_crop::CropField;for field in [@@ -7953,7 +7985,9 @@ impl EditorWindow {CropField::X,CropField::Y,] {- self.ensure_field(FieldKey::Crop(field), window, cx);+ let key = FieldKey::Crop(field);+ self.ensure_field(key, window, cx);+ fields.push(key);}}@@ -7966,6 +8000,9 @@ impl EditorWindow {warm_installed_fonts();}let Some(timeline) = self.timeline() else {+ // The crop dialog does not need the timeline, and its+ // fields were already ensured above.+ self.sync_visible_fields(&fields, window, cx);return;};let indices = |length: usize| -> Vec<usize> {@@ -8068,13 +8105,13 @@ impl EditorWindow {},}- for key in fields {+ for &key in &fields {self.ensure_field(key, window, cx);}for target in colors {self.ensure_hex_input(target, window, cx);}- self.sync_fields(window, cx);+ self.sync_visible_fields(&fields, window, cx);}}diff --git a/apps/desktop-gpui/src/editor_window.rs b/apps/desktop-gpui/src/editor_window.rsindex 164b8bdaf..a1b70191f 100644--- a/apps/desktop-gpui/src/editor_window.rs+++ b/apps/desktop-gpui/src/editor_window.rs@@ -12465,6 +12465,46 @@ mod tests {}}+ #[cfg(target_os = "linux")]+ fn field_text(+ editor: &Entity<EditorWindow>,+ key: crate::editor_panels::FieldKey,+ cx: &mut gpui::VisualTestContext,+ ) -> String {+ editor.read_with(cx, |editor, cx| {+ editor.field(key).unwrap().read(cx).text().to_string()+ })+ }++ #[cfg(target_os = "linux")]+ fn set_caption_timeline(editor: &mut EditorWindow, captions: &[(&str, f64, f64)]) {+ editor.project.timeline = Some(TimelineConfiguration {+ style_segments: Vec::new(),+ image_segments: Vec::new(),+ segments: Vec::new(),+ transitions: Vec::new(),+ zoom_segments: Vec::new(),+ scene_segments: Vec::new(),+ mask_segments: Vec::new(),+ text_segments: Vec::new(),+ caption_segments: captions+ .iter()+ .map(|(id, start, end)| {+ serde_json::from_value(serde_json::json!({+ "id": id,+ "start": start,+ "end": end,+ "text": format!("caption {id}"),+ }))+ .unwrap()+ })+ .collect(),+ keyboard_segments: Vec::new(),+ audio_segments: Vec::new(),+ camera3d_segments: Vec::new(),+ });+ }+#[cfg(target_os = "linux")]#[gpui::test]#[ignore = "manual preset menu repaint benchmark"]@@ -13490,6 +13530,238 @@ mod tests {assert_eq!(text(&editor, FieldKey::CaptionStart(0), cx), "1.25");}+ // gpui's test windows have no platform window handle on macOS or Windows, which the editor needs.+ #[cfg(target_os = "linux")]+ #[gpui::test]+ fn visible_field_sync_leaves_hidden_fields_until_they_are_visible(+ cx: &mut gpui::TestAppContext,+ ) {+ use crate::editor_panels::FieldKey;++ let project = tempfile::tempdir().unwrap();+ let project_path = project.path().to_path_buf();+ let (editor, cx) = cx+ .add_window_view(move |window, cx| EditorWindow::new(project_path.clone(), window, cx));+ let visible = [+ FieldKey::CaptionText(0),+ FieldKey::CaptionStart(0),+ FieldKey::CaptionEnd(0),+ ];+ let hidden = [+ FieldKey::CaptionText(1),+ FieldKey::CaptionStart(1),+ FieldKey::CaptionEnd(1),+ ];+ editor.update_in(cx, |editor, window, cx| {+ editor.set_error("fields".into(), window, cx);+ set_caption_timeline(editor, &[("first", 1.25, 3.5), ("second", 4., 6.)]);+ for key in visible.into_iter().chain(hidden) {+ editor.ensure_field(key, window, cx);+ }+ editor.sync_fields(window, cx);+ });+ assert_eq!(+ hidden.map(|key| field_text(&editor, key, cx)),+ ["caption second", "4.00", "6.00"]+ );++ // A hidden field is not touched by a sync that only lists the visible+ // caption, even after its value moved.+ editor.update_in(cx, |editor, window, cx| {+ let segment = &mut editor.project.timeline.as_mut().unwrap().caption_segments[1];+ segment.text = "caught up".into();+ segment.start = 5.;+ segment.end = 7.;+ editor.sync_visible_fields(&visible, window, cx);+ });+ assert_eq!(+ hidden.map(|key| field_text(&editor, key, cx)),+ ["caption second", "4.00", "6.00"]+ );++ // Becoming visible lists it again, and it catches up then.+ editor.update_in(cx, |editor, window, cx| {+ editor.sync_visible_fields(&hidden, window, cx);+ });+ assert_eq!(+ hidden.map(|key| field_text(&editor, key, cx)),+ ["caught up", "5.00", "7.00"]+ );++ // The all-fields path still catches every hidden field up, which is+ // the old behaviour the benchmark compares against.+ editor.update_in(cx, |editor, window, cx| {+ editor.project.timeline.as_mut().unwrap().caption_segments[1].text = "all".into();+ editor.sync_fields(window, cx);+ });+ assert_eq!(field_text(&editor, FieldKey::CaptionText(1), cx), "all");+ }++ // gpui's test windows have no platform window handle on macOS or Windows, which the editor needs.+ #[cfg(target_os = "linux")]+ #[gpui::test]+ fn visible_field_sync_never_overwrites_a_focused_field(cx: &mut gpui::TestAppContext) {+ use crate::editor_panels::FieldKey;++ let project = tempfile::tempdir().unwrap();+ let project_path = project.path().to_path_buf();+ let (editor, cx) = cx+ .add_window_view(move |window, cx| EditorWindow::new(project_path.clone(), window, cx));+ let visible = [+ FieldKey::CaptionText(0),+ FieldKey::CaptionStart(0),+ FieldKey::CaptionEnd(0),+ ];+ editor.update_in(cx, |editor, window, cx| {+ editor.set_error("fields".into(), window, cx);+ set_caption_timeline(editor, &[("first", 1.25, 3.5)]);+ for key in visible {+ editor.ensure_field(key, window, cx);+ }+ editor.sync_fields(window, cx);+ });+ let start_focus = editor.read_with(cx, |editor, cx| {+ editor+ .field(FieldKey::CaptionStart(0))+ .unwrap()+ .read(cx)+ .focus_handle()+ });++ // The visible caption moved, but its start box is being typed into.+ editor.update_in(cx, |editor, window, cx| {+ window.focus(&start_focus, cx);+ let segment = &mut editor.project.timeline.as_mut().unwrap().caption_segments[0];+ segment.start = 2.;+ segment.text = "Bye".into();+ editor.sync_visible_fields(&visible, window, cx);+ });+ assert_eq!(field_text(&editor, FieldKey::CaptionText(0), cx), "Bye");+ assert_eq!(field_text(&editor, FieldKey::CaptionStart(0), cx), "1.25");+ }++ // gpui's test windows have no platform window handle on macOS or Windows, which the editor needs.+ #[cfg(target_os = "linux")]+ #[gpui::test]+ fn prepare_sidebar_fields_syncs_only_the_selected_caption_and_the_crop(+ cx: &mut gpui::TestAppContext,+ ) {+ use crate::{+ editor_crop::{CropBounds, CropField, CropState},+ editor_panels::FieldKey,+ };++ let project = tempfile::tempdir().unwrap();+ let project_path = project.path().to_path_buf();+ let (editor, cx) = cx+ .add_window_view(move |window, cx| EditorWindow::new(project_path.clone(), window, cx));+ let caption0 = [+ FieldKey::CaptionText(0),+ FieldKey::CaptionStart(0),+ FieldKey::CaptionEnd(0),+ ];+ let caption1 = [+ FieldKey::CaptionText(1),+ FieldKey::CaptionStart(1),+ FieldKey::CaptionEnd(1),+ ];+ let crop_fields = [+ FieldKey::Crop(CropField::Width),+ FieldKey::Crop(CropField::Height),+ FieldKey::Crop(CropField::X),+ FieldKey::Crop(CropField::Y),+ ];+ editor.update_in(cx, |editor, window, cx| {+ editor.set_error("fields".into(), window, cx);+ set_caption_timeline(editor, &[("first", 1.25, 3.5), ("second", 4., 6.)]);+ // Both captions were selected at some point, so both sets of+ // fields exist and start in sync.+ for key in caption0.into_iter().chain(caption1) {+ editor.ensure_field(key, window, cx);+ }+ editor.sync_fields(window, cx);+ editor.crop = Some(CropState::new(+ (1920, 1080),+ (510., 331.),+ CropBounds::new(96., 54., 960., 540.),+ ));+ editor.selection = Some(Selection::single(TrackKind::Caption, 0));+ });+ assert_eq!(+ caption1.map(|key| field_text(&editor, key, cx)),+ ["caption second", "4.00", "6.00"]+ );++ // Move both captions, then run the production prepare path. It draws+ // caption 0 and the crop dialog, so caption 1 stays behind.+ editor.update_in(cx, |editor, window, cx| {+ let timeline = editor.project.timeline.as_mut().unwrap();+ timeline.caption_segments[0].text = "visible".into();+ timeline.caption_segments[0].start = 1.5;+ timeline.caption_segments[0].end = 3.0;+ timeline.caption_segments[1].text = "hidden".into();+ editor.prepare_sidebar_fields(window, cx);+ });+ assert_eq!(field_text(&editor, FieldKey::CaptionText(0), cx), "visible");+ assert_eq!(field_text(&editor, FieldKey::CaptionStart(0), cx), "1.50");+ assert_eq!(field_text(&editor, FieldKey::CaptionEnd(0), cx), "3.00");+ assert_eq!(+ field_text(&editor, FieldKey::CaptionText(1), cx),+ "caption second"+ );++ // The crop boxes are part of the visible set.+ let crop = editor.read_with(cx, |editor, _| editor.crop.as_ref().unwrap().real());+ assert_eq!(+ crop_fields.map(|key| {+ let FieldKey::Crop(_) = key else {+ unreachable!()+ };+ field_text(&editor, key, cx)+ }),+ crop_fields.map(|key| {+ let FieldKey::Crop(field) = key else {+ unreachable!()+ };+ crate::ui::format_number(field.read(crop))+ })+ );++ // Selecting caption 1 makes it visible, and it catches up.+ editor.update_in(cx, |editor, window, cx| {+ editor.selection = Some(Selection::single(TrackKind::Caption, 1));+ editor.prepare_sidebar_fields(window, cx);+ });+ assert_eq!(field_text(&editor, FieldKey::CaptionText(1), cx), "hidden");++ // The crop dialog is independent of the timeline: its four fields+ // still sync through the early-return path.+ editor.update_in(cx, |editor, window, cx| {+ editor.project.timeline = None;+ editor.crop = Some(CropState::new(+ (1920, 1080),+ (510., 331.),+ CropBounds::new(10., 20., 320., 480.),+ ));+ editor.prepare_sidebar_fields(window, cx);+ });+ let crop = editor.read_with(cx, |editor, _| editor.crop.as_ref().unwrap().real());+ assert_eq!(+ crop_fields.map(|key| {+ let FieldKey::Crop(_) = key else {+ unreachable!()+ };+ field_text(&editor, key, cx)+ }),+ crop_fields.map(|key| {+ let FieldKey::Crop(field) = key else {+ unreachable!()+ };+ crate::ui::format_number(field.read(crop))+ })+ );+ }+/// `default_editor_preview_resolution()` is asserted to be 1248x702 in the/// Tauri app itself (`lib.rs:192-194`); the render size of a display/// recording follows from it.