From 0ab10f393597d516619fd83a6f0ca68627fea0ec Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Sat, 28 Nov 2026 15:00:04 -0800 Subject: [PATCH] Selection: the selected roots are found once for each selection selected_roots put the selected objects in paint order and dropped the inner ones, two walks of the document, every time a command or an enabled check asked. A rebuild of the menus asks many times for one selection. The roots are now kept on the thread with the document version (held weakly, so its address is not reused) and the selected objects, and found again only when either changes. With 10,000 rectangles selected, a menu rebuild after Select All went from about 500 ms to about 340 ms on the build host. --- crates/engine/src/cmd/edit.rs | 38 +++++++++++++++++++++++++++-- crates/engine/src/cmd/edit/tests.rs | 23 +++++++++++++++++ 2 files changed, 59 insertions(+), 2 deletions(-) diff --git a/crates/engine/src/cmd/edit.rs b/crates/engine/src/cmd/edit.rs index 43151bbebb..4fc1b83c29 100644 --- a/crates/engine/src/cmd/edit.rs +++ b/crates/engine/src/cmd/edit.rs @@ -954,9 +954,43 @@ pub(crate) fn selected_roots(s: &Session) -> Result> { Ok(selection_roots(s.doc()?)) } -/// [`selected_roots`] of one document's selection. +/// [`selected_roots`] of one document's selection, found once for each document version and set of selected +/// objects and kept on this thread: the menus' enabled checks read it many times for one selection. pub(crate) fn selection_roots(st: &crate::DocState) -> Vec { - roots_of(&st.doc, st.selection.in_paint_order(&st.doc)) + let at = Arc::as_ptr(&st.doc); + ROOTS.with_borrow_mut(|kept| { + if let Some((doc, objects, roots)) = kept.as_ref() + && doc.as_ptr() == at + && *objects == st.selection.objects + { + return roots.clone(); + } + #[cfg(test)] + ROOTS_FOUND.set(ROOTS_FOUND.get() + 1); + let roots = roots_of(&st.doc, st.selection.in_paint_order(&st.doc)); + *kept = Some((Arc::downgrade(&st.doc), st.selection.objects.clone(), roots.clone())); + roots + }) +} + +/// The selected objects and roots of the last [`selection_roots`], with the document version they belong to. It is +/// held weakly, so its address is not reused while it is kept, and an edit in place moves it. +type KeptRoots = Option<(std::sync::Weak, Vec, Vec)>; + +thread_local! { + static ROOTS: std::cell::RefCell = const { std::cell::RefCell::new(None) }; +} + +#[cfg(test)] +thread_local! { + /// How many times [`selection_roots`] found the roots on this thread. + static ROOTS_FOUND: std::cell::Cell = const { std::cell::Cell::new(0) }; +} + +/// How many times [`selection_roots`] has found the roots on this thread. +#[cfg(test)] +pub(crate) fn roots_found() -> u64 { + ROOTS_FOUND.get() } /// [`super::brushsym::part_only`] while an object of a dynamic symbol instance's art is selected: diff --git a/crates/engine/src/cmd/edit/tests.rs b/crates/engine/src/cmd/edit/tests.rs index 1e41b0c4ac..7ac2d9c162 100644 --- a/crates/engine/src/cmd/edit/tests.rs +++ b/crates/engine/src/cmd/edit/tests.rs @@ -347,3 +347,26 @@ fn direct_selection_cut_timing() { println!("median {}: cpu {:?}, wall {:?}", ["one at a time", "Cut"][k], median(cpu), median(wall)); } } + +/// The selected roots are found once for a document version and its selected objects, however many enabled checks +/// read them, and found again after the selection changes, after an edit, and for another selection. +#[test] +fn selected_roots_are_found_once_for_each_document_and_selection() { + let mut s = session(30, square); + let all = selected_roots(&s).unwrap(); + assert_eq!(all.len(), 30); + let before = roots_found(); + for _ in 0..20 { + assert_eq!(selected_roots(&s).unwrap(), all); + } + assert_eq!(roots_found() - before, 0, "reading the same selection again finds nothing"); + s.execute("select.set", &json!({"ids": [all[3].0, all[7].0]})).unwrap(); + assert_eq!(selected_roots(&s).unwrap(), vec![all[3], all[7]]); + assert_eq!(roots_found() - before, 1, "a new selection is found once"); + s.execute("object.group", &json!({})).unwrap(); + let grouped = selected_roots(&s).unwrap(); + assert_eq!(grouped.len(), 1, "the group is the one root: {grouped:?}"); + assert_ne!(grouped, vec![all[3], all[7]]); + selected_roots(&s).unwrap(); + assert_eq!(roots_found() - before, 2, "an edit finds the roots once again"); +}