Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

perf(desktop-gpui): borrow the scoped project while editing a style

Pravin9762cece8788
diff --git a/apps/desktop-gpui/src/editor_sidebar.rs b/apps/desktop-gpui/src/editor_sidebar.rs
index dcc1c0f2c..8411400b4 100644
--- a/apps/desktop-gpui/src/editor_sidebar.rs
+++ b/apps/desktop-gpui/src/editor_sidebar.rs
@@ -761,6 +761,11 @@ pub struct SidebarState {
pub audio_enhancement_default: bool,
pub audio_enhancement_error: Option<String>,
pub(crate) style_target: Option<(usize, StyleGroup)>,
+ /// Set while `EditorWindow::with_style_controls` has swapped the scoped
+ /// project into place. Controls rendered inside that scope read it through
+ /// `style_control_project`, which would otherwise clone the whole project
+ /// again for every slider and swatch on the page.
+ style_scoped: bool,
pub(crate) image_import_error: Option<String>,
pub(crate) image_asset_status: Option<(String, bool)>,
animated_gradient: animated_gradient::AnimatedGradientState,
@@ -881,6 +886,7 @@ impl SidebarState {
audio_enhancement_default: crate::store::studio_sound_by_default(),
audio_enhancement_error: None,
style_target: None,
+ style_scoped: false,
image_import_error: None,
image_asset_status: None,
animated_gradient: animated_gradient::AnimatedGradientState::new(),
@@ -4466,6 +4472,12 @@ impl EditorWindow {
}
pub(crate) fn style_control_project(&self) -> std::borrow::Cow<'_, ProjectConfiguration> {
+ // `with_style_controls` already replaced `self.project` with the scoped
+ // configuration, so inside its closure the override is in place and a
+ // second clone would only copy the copy.
+ if self.sidebar.style_scoped {
+ return std::borrow::Cow::Borrowed(&self.project);
+ }
let Some((index, group)) = self.sidebar.style_target else {
return std::borrow::Cow::Borrowed(&self.project);
};
@@ -4499,12 +4511,20 @@ impl EditorWindow {
}
pub(crate) fn with_style_controls<R>(&mut self, render: impl FnOnce(&mut Self) -> R) -> R {
+ // A nested scope would see the outer override through the flag and
+ // silently render with it, so callers must enter one scope at a time.
+ debug_assert!(
+ !self.sidebar.style_scoped,
+ "with_style_controls must not nest"
+ );
let std::borrow::Cow::Owned(scoped) = self.style_control_project() else {
return render(self);
};
// Existing controls read the project while building their elements. Restore the base before any event can publish or save it.
let base = std::mem::replace(&mut self.project, scoped);
+ self.sidebar.style_scoped = true;
let result = render(self);
+ self.sidebar.style_scoped = false;
self.project = base;
result
}
diff --git a/apps/desktop-gpui/src/editor_window.rs b/apps/desktop-gpui/src/editor_window.rs
index c9c031c45..65557724e 100644
--- a/apps/desktop-gpui/src/editor_window.rs
+++ b/apps/desktop-gpui/src/editor_window.rs
@@ -12287,6 +12287,130 @@ mod tests {
}
}
+ /// The style-edit scope swaps the overridden configuration into
+ /// `self.project`; controls built inside it must borrow that project
+ /// instead of cloning it once per slider. Pin both halves: the override is
+ /// visible while the scope is active, and the base project comes back
+ /// (with a clone) once it ends.
+ #[cfg(target_os = "linux")]
+ #[gpui::test]
+ fn style_controls_borrow_the_scoped_project(cx: &mut gpui::TestAppContext) {
+ 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));
+ editor.update_in(cx, |editor, _window, _cx| {
+ let mut config = editor.project.clone();
+ config.timeline = Some(cap_project::TimelineConfiguration {
+ segments: Vec::new(),
+ transitions: Vec::new(),
+ zoom_segments: Vec::new(),
+ scene_segments: Vec::new(),
+ mask_segments: Vec::new(),
+ text_segments: Vec::new(),
+ caption_segments: Vec::new(),
+ keyboard_segments: Vec::new(),
+ audio_segments: Vec::new(),
+ style_segments: vec![cap_project::StyleSegment {
+ overrides: cap_project::StyleOverrides {
+ background: Some(cap_project::BackgroundConfiguration {
+ padding: 77.5,
+ ..Default::default()
+ }),
+ ..Default::default()
+ },
+ ..Default::default()
+ }],
+ image_segments: Vec::new(),
+ camera3d_segments: Vec::new(),
+ });
+ editor.project = config;
+ editor.sidebar.style_target = Some((0, crate::editor_sidebar::StyleGroup::Background));
+ let base_padding = editor.project.background.padding;
+
+ editor.with_style_controls(|editor| {
+ assert_eq!(editor.project.background.padding, 77.5);
+ let scoped = editor.style_control_project();
+ assert!(matches!(&scoped, std::borrow::Cow::Borrowed(_)));
+ assert_eq!(scoped.background.padding, 77.5);
+ });
+
+ assert_eq!(editor.project.background.padding, base_padding);
+ // Outside the scope the method still owns the override for the
+ // control builders; only the base project itself is restored.
+ let restored = editor.style_control_project();
+ assert!(matches!(&restored, std::borrow::Cow::Owned(_)));
+ assert_eq!(restored.background.padding, 77.5);
+ });
+ }
+
+ /// Manual A/B benchmark for the style-edit scope. Run with:
+ /// `cargo test -p cap-desktop-gpui --bin cap-gpui -- --ignored benchmark_style_control_repaint --nocapture`
+ #[cfg(target_os = "linux")]
+ #[gpui::test]
+ #[ignore = "manual style-control repaint benchmark"]
+ fn benchmark_style_control_repaint(cx: &mut gpui::TestAppContext) {
+ 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));
+ editor.update_in(cx, |editor, _window, _cx| {
+ let mut config = editor.project.clone();
+ config.timeline = Some(cap_project::TimelineConfiguration {
+ segments: vec![cap_project::TimelineSegment::default(); 2_000],
+ transitions: Vec::new(),
+ zoom_segments: Vec::new(),
+ scene_segments: Vec::new(),
+ mask_segments: Vec::new(),
+ text_segments: Vec::new(),
+ caption_segments: Vec::new(),
+ keyboard_segments: Vec::new(),
+ audio_segments: Vec::new(),
+ style_segments: vec![cap_project::StyleSegment {
+ overrides: cap_project::StyleOverrides {
+ background: Some(cap_project::BackgroundConfiguration {
+ padding: 77.5,
+ ..Default::default()
+ }),
+ ..Default::default()
+ },
+ ..Default::default()
+ }],
+ image_segments: Vec::new(),
+ camera3d_segments: Vec::new(),
+ });
+ editor.project = config;
+ editor.sidebar.style_target = Some((0, crate::editor_sidebar::StyleGroup::Background));
+ editor.sidebar.tab = crate::editor_sidebar::SidebarTab::Background;
+ });
+
+ let mut samples = Vec::new();
+ for _ in 0..32 {
+ cx.draw(
+ point(px(0.), px(0.)),
+ gpui::size(px(800.), px(600.)),
+ |_, cx| {
+ editor.update(cx, |editor, cx| {
+ let started = std::time::Instant::now();
+ for _ in 0..10 {
+ drop(std::hint::black_box(editor.with_style_controls(|editor| {
+ editor.render_sidebar(cx).into_any_element()
+ })));
+ }
+ samples.push(started.elapsed().as_secs_f64() * 1_000_000. / 10.);
+ });
+ div()
+ },
+ );
+ }
+ samples.remove(0);
+ samples.sort_by(f64::total_cmp);
+ println!(
+ "style_controls timeline_segments=2000 samples=31 repaints_per_sample=10 median_us={:.3}",
+ samples[samples.len() / 2]
+ );
+ }
+
#[test]
fn auto_zoom_loads_clicks_without_losing_take_indices() {
let root =