From 849bf0888c242756bb70f044c871419d0b0daac4 Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Wed, 25 Nov 2026 08:06:56 -0800 Subject: [PATCH] Canvas: the anchor overlay groups selected anchors by path with one lookup per path canvas::selection::anchors added every selected path but the first to the overlay's PathSelection with PathSelection::also, which searches the list it is building for the same path. With n selected paths that is about n^2/2 key comparisons per rebuild of the marks. The anchors are now grouped by path with a hash map (by_key) and the result is assigned to `others` once. A path listed twice keeps its anchors together where it first appears, as `also` did. With 5,000 selected rectangles and Properties open (release test build, Linux x86_64), an idle frame went from 64.1 ms to 61.0 ms (mean of two runs). Test grouping_selected_anchors_by_path_compares_each_path_a_bounded_number_of_times counts the key comparisons for 2,001 entries; a per-entry search makes 1,999,197. --- crates/ui-egui/src/canvas/selection.rs | 28 +++++++++++++++--- crates/ui-egui/src/canvas/selection/tests.rs | 31 ++++++++++++++++++++ 2 files changed, 55 insertions(+), 4 deletions(-) diff --git a/crates/ui-egui/src/canvas/selection.rs b/crates/ui-egui/src/canvas/selection.rs index 2018e50323..96be3fcc89 100644 --- a/crates/ui-egui/src/canvas/selection.rs +++ b/crates/ui-egui/src/canvas/selection.rs @@ -2,7 +2,8 @@ //! centre point, point type's origin, the key anchor). Each rule is a function of the app that returns what to draw; //! `show` hands the results to the framework's controls, which keep what they stroke between passes. -use std::collections::hash_map::DefaultHasher; +use std::collections::HashMap; +use std::collections::hash_map::{DefaultHasher, Entry}; use std::hash::{Hash, Hasher}; use craft::behavior::Input; @@ -316,13 +317,32 @@ pub(super) fn anchors(app: &VectorcraftApp, st: &DocState, tool: &str, tool_poin primary.and_then(|key| chosen.iter().find(|(k, _)| *k == key)).map(|(_, knots)| knots.clone()).unwrap_or_default(), segment.map(|(_, at)| at), ); - for (key, knots) in chosen.into_iter().filter(|(key, _)| Some(*key) != primary) { - selection = selection.also(key, knots); - } + selection.others = by_key(chosen.into_iter().filter(|(key, _)| Some(*key) != primary)); out.selection = selection; out } +/// `listed` with the lists of a repeated key joined, each key where it first appears: what +/// [`PathSelection::also`] makes of the entries added one at a time, found with one lookup per entry. +fn by_key(listed: impl IntoIterator)>) -> Vec<(K, Vec)> { + let mut at: HashMap = HashMap::new(); + let mut out: Vec<(K, Vec)> = Vec::new(); + for (key, items) in listed { + match at.entry(key) { + Entry::Occupied(seen) => { + if let Some((_, held)) = out.get_mut(*seen.get()) { + held.extend(items); + } + } + Entry::Vacant(new) => { + new.insert(out.len()); + out.push((key, items)); + } + } + } + out +} + /// The outline of the object under the pointer at document point `at`, at view `zoom`, for Smart Guides › Object /// Highlighting: the object a click of the selection tools would pick, unless the click keeps the selection (the /// object, or with the Selection tool selected art inside it, is selected) or the object has too many points. diff --git a/crates/ui-egui/src/canvas/selection/tests.rs b/crates/ui-egui/src/canvas/selection/tests.rs index fdca4b2a9e..4f5e469a63 100644 --- a/crates/ui-egui/src/canvas/selection/tests.rs +++ b/crates/ui-egui/src/canvas/selection/tests.rs @@ -558,3 +558,34 @@ fn an_empty_selection_has_no_marks() { let st = app.session.active().unwrap(); assert_eq!(dots(&app, st).centers.len() + dots(&app, st).origins.len(), 0); } + +/// A path key that counts the comparisons made with it. +#[derive(Clone, Copy, Debug)] +struct Counted<'a>(u64, &'a std::cell::Cell); + +impl PartialEq for Counted<'_> { + fn eq(&self, other: &Self) -> bool { + self.1.set(self.1.get() + 1); + self.0 == other.0 + } +} + +impl Eq for Counted<'_> {} + +impl Hash for Counted<'_> { + fn hash(&self, state: &mut H) { + self.0.hash(state); + } +} + +// Grouping the selected anchors of 2,000 paths compares each key a bounded number of times, not once per path listed +// before it, and joins a repeated path's anchors where the path first appears, as `PathSelection::also` does. +#[test] +fn grouping_selected_anchors_by_path_compares_each_path_a_bounded_number_of_times() { + let compared = std::cell::Cell::new(0); + let listed = (0..2000u64).map(|key| (Counted(key, &compared), vec![key])).chain([(Counted(5, &compared), vec![9])]); + let grouped = by_key(listed); + assert_eq!(grouped.iter().map(|(key, _)| key.0).collect::>(), (0..2000).collect::>()); + assert_eq!(grouped[5].1, vec![5, 9]); + assert!(compared.get() < 2 * 2001, "{} comparisons for 2,001 entries", compared.get()); +}