From 4fc4cbca0b23187dd3f9bb2977626463eccf78e9 Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Sat, 28 Nov 2026 17:00:31 -0800 Subject: [PATCH] Performance: Document::node reads the node from the lookup cache without copying its path Document::node went through index_path, which copied the node's index path out of the lookup cache into a new Vec, walked the tree to check it, and walked it again to return the node. The menus' states call it for every selected object in many of their checks, so a rebuild of the menu states with 10,000 objects selected made hundreds of thousands of copies. node now follows the place the cache has for the id once, without a copy. When the cache has no single place for it, the path is deeper than 32 levels, or the node there no longer has the id, it looks the node up as index_path does. vectorcraft_doc::path_copies counts the paths the cache copies out, for the new test. --- crates/doc/src/document.rs | 8 +++++- crates/doc/src/id_index.rs | 43 +++++++++++++++++++++++++++++++++ crates/doc/src/lib.rs | 2 +- crates/doc/src/tests/lookups.rs | 29 ++++++++++++++++++++++ 4 files changed, 80 insertions(+), 2 deletions(-) diff --git a/crates/doc/src/document.rs b/crates/doc/src/document.rs index 3e5743aec3..dc1e082ac0 100644 --- a/crates/doc/src/document.rs +++ b/crates/doc/src/document.rs @@ -314,8 +314,14 @@ impl Document { self.renumber_stored_duplicates(); } - /// Find a node anywhere in the tree. + /// Find a node anywhere in the tree. With the lookup cache built, the node comes from the place the cache has for + /// it, read once and without copying its index path; otherwise, and when that place no longer holds it, as + /// [`Document::index_path`] finds it. pub fn node(&self, id: NodeId) -> Option<&Node> { + if let Some(n) = self.index.get().and_then(|cached| cached.node_in(&self.layers, id)) { + crate::id_index::note_path_lookup(); + return Some(n); + } self.index_path(id).and_then(|p| self.node_at(&p)) } diff --git a/crates/doc/src/id_index.rs b/crates/doc/src/id_index.rs index 34453234ec..0e1673580a 100644 --- a/crates/doc/src/id_index.rs +++ b/crates/doc/src/id_index.rs @@ -82,6 +82,7 @@ impl Paths { /// isn't in the table or it, or a node it is in, was removed. pub(crate) fn get(&self, id: &NodeId) -> Option>> { let e = (*self.table.ids.get(id)?)?; + note_path_copy(); let mut path = Vec::new(); let mut cur = e; // Each step goes to a parent entry, made before its children: at most `at.len()` steps. @@ -96,6 +97,29 @@ impl Paths { } Some(None) } + /// The node with `id` in `layers` at the place the table has for it, found without copying its index path: `None` + /// when the table does not have one place for `id`, its path is deeper than [`NODE_IN_DEPTH`], or the node there + /// does not have the id (the tree changed). The caller then looks it up as [`crate::Document::index_path`] does. + pub(crate) fn node_in<'a>(&self, layers: &'a [Arc], id: NodeId) -> Option<&'a Node> { + let mut cur = (*self.table.ids.get(&id)?)?; + let mut steps = [0u32; NODE_IN_DEPTH]; + let mut depth = 0; + loop { + let &(parent, i) = self.table.at.get(cur as usize)?; + *steps.get_mut(depth)? = self.shifted(parent, i)?; + depth += 1; + if parent == u32::MAX { + break; + } + cur = parent; + } + let mut path = steps[..depth].iter().rev(); + let mut node: &Node = layers.get(*path.next()? as usize)?; + for &i in path { + node = node.children()?.get(i as usize)?; + } + (node.id == id).then_some(node) + } /// Where the table has `id` as it was built: its parent's entry and its index among its /// siblings then; `None` when the id isn't in the table or appears more than once. pub(crate) fn table_place(&self, id: &NodeId) -> Option<(u32, u32)> { @@ -170,6 +194,25 @@ pub(crate) fn note_path_lookup() { PATH_LOOKUPS.set(PATH_LOOKUPS.get().saturating_add(1)); } +/// The deepest index path [`Paths::node_in`] follows without copying it. +const NODE_IN_DEPTH: usize = 32; + +thread_local! { + /// [`path_copies`] of this thread. + static PATH_COPIES: std::cell::Cell = const { std::cell::Cell::new(0) }; +} + +/// How many index paths the lookup cache has copied out on this thread, one for each [`crate::Document::index_path`] +/// that it answered. [`crate::Document::node`] reads the node from the cache without one. +pub fn path_copies() -> u64 { + PATH_COPIES.get() +} + +/// Adds one to [`path_copies`]. +fn note_path_copy() { + PATH_COPIES.set(PATH_COPIES.get().saturating_add(1)); +} + /// The cache, shared by clones of a document (each checks every path it uses against its own tree), /// the lookups that missed it, and the nodes removed from the tree since it was built. #[derive(Default)] diff --git a/crates/doc/src/lib.rs b/crates/doc/src/lib.rs index de8bfc2875..470b0a94c4 100644 --- a/crates/doc/src/lib.rs +++ b/crates/doc/src/lib.rs @@ -24,7 +24,7 @@ mod halftone_radii; pub mod hit; mod id_index; pub mod image_quad; -pub use id_index::{path_lookups, tree_walks}; +pub use id_index::{path_copies, path_lookups, tree_walks}; pub mod inks; mod inline; pub mod intertwine; diff --git a/crates/doc/src/tests/lookups.rs b/crates/doc/src/tests/lookups.rs index 295c347765..4a8f6a9d71 100644 --- a/crates/doc/src/tests/lookups.rs +++ b/crates/doc/src/tests/lookups.rs @@ -579,3 +579,32 @@ fn nearest_finder_reads_one_path_per_object() { assert_eq!(path_lookups() - lookups, 0, "lookups by id"); assert_eq!(tree_walks() - walks, 0, "walks of the tree"); } + +/// `Document::node` reads a node from the lookup cache without copying its index path, in nested groups and after +/// objects were removed, and finds what a walk finds: 10,000 objects looked up one at a time copy no path. +#[test] +fn node_lookups_copy_no_index_path() { + let (mut d, ids) = squares(10_000); + let layer = d.layers[0].id; + let mut nested = vec![]; + for g in 0..50 { + let kids: Vec> = (0..4) + .map(|_| { + let id = d.alloc_id(); + nested.push(id); + Arc::new(Node::path(id, shapes::rectangle(Rect::new(0.0, 0.0, 5.0, 5.0)), Appearance::default_art())) + }) + .collect(); + let (gid, inner) = (d.alloc_id(), d.alloc_id()); + d.insert(Some(layer), g, Node::group(gid, vec![Arc::new(Node::group(inner, kids))])).unwrap(); + } + d.remove_many(&ids[..100]).unwrap(); + d.node(layer); + let copies = path_copies(); + let found = ids[100..].iter().chain(&nested).filter(|id| d.node(**id).is_some_and(|n| n.id == **id)).count(); + assert_eq!(found, ids.len() - 100 + nested.len()); + assert_eq!(path_copies() - copies, 0, "index paths copied"); + assert!(ids[..100].iter().all(|id| d.node(*id).is_none()), "removed objects are not found"); + agree(&d, &ids[..200], "after remove_many"); + agree(&d, &nested, "nested"); +}