SolidCraft: an open-source parametric 3D CAD application built in pure Rust. Desktop, web, and automation through MCP.
perf(#3892): index the hidden joints and folders of the view (#9823)
Homero Thompson810a9558bded
diff --git a/crates/engine/src/cmd/joints_tests.rs b/crates/engine/src/cmd/joints_tests.rsindex 7a772adaef..68c1b7feed 100644--- a/crates/engine/src/cmd/joints_tests.rs+++ b/crates/engine/src/cmd/joints_tests.rs@@ -1287,6 +1287,87 @@ fn hidden_joints_still_drive_and_are_saved_with_the_design() {let _ = std::fs::remove_dir_all(&dir);}+/// #3892: a large nested STEP assembly hides thousands of joints, and the browser asks about+/// every joint of the design on every frame: the answer must not walk the hidden-joint list,+/// so the work must not grow with the number of hidden joints. The hidden joint stays hidden,+/// the shown one stays shown, and undo and redo keep the hiding right.+#[test]+fn hidden_joint_lookups_do_not_walk_the_hidden_joint_list() {+ use crate::occurrences::visibility_work;++ let mut s = hinged();+ let (hinge, glue) = (joint(&s, "Hinge"), joint(&s, "Glue"));++ // The hidden joints of an imported assembly: thousands of ids (#3892).+ let hide_many = |s: &mut Session, n: u64| {+ s.visibility.hidden_joints.clear();+ for id in 0..n {+ s.visibility.hidden_joints.push(id);+ }+ };+ // The entries one lookup walked, for a joint that is not hidden.+ let walked_by = |s: &Session, id: u64| {+ visibility_work::take();+ let visible = s.joint_visible(id);+ let walked = visibility_work::take();+ assert!(visible, "joint {id} is not hidden here");+ walked+ };++ hide_many(&mut s, 10);+ let few = walked_by(&s, 9_000_001);+ hide_many(&mut s, 10_000);+ let many = walked_by(&s, 9_000_001);+ assert!(+ many <= few + 4,+ "a joint lookup walked {many} entries of a 10,000-entry hidden-joint list and {few} of a \+ 10-entry one: the work must not grow with the number of hidden joints"+ );++ // The behaviour is unchanged: the hidden joint is hidden, the shown one is shown, and undo+ // and redo keep the hiding right.+ s.visibility.hidden_joints.clear();+ run(&mut s, "browser.visibility", json!({"joints": ["Hinge"], "visible": false}));+ assert!(!s.joint_visible(hinge) && s.joint_visible(glue));+ run(&mut s, "edit.undo", json!({}));+ assert!(s.joint_visible(hinge), "undo shows the hinge again");+ run(&mut s, "edit.redo", json!({}));+ assert!(!s.joint_visible(hinge), "redo hides it again");+}++/// The cost of a frame's worth of joint questions, with few and with many hidden joints: the+/// manual measurement behind #3892. Run it with `--ignored --nocapture`.+#[test]+#[ignore = "manual scaling measurement"]+fn joint_visibility_lookup_cost_by_hidden_joint_count() {+ use crate::occurrences::visibility_work;++ let joints = 1_000u64;+ for hidden in [0u64, 10, 100, 1_000, 10_000] {+ let mut s = Session::default();+ for id in 0..hidden {+ s.visibility.hidden_joints.push(id);+ }+ let mut times = Vec::new();+ for _ in 0..11 {+ let start = std::time::Instant::now();+ for id in 0..joints {+ let _ = s.joint_visible(id + hidden + 1);+ }+ times.push(start.elapsed().as_secs_f64() * 1e3);+ }+ times.sort_by(f64::total_cmp);+ // The index is built once per change of the view, apart from the answers themselves.+ println!(+ "hidden_joints={hidden:>6} questions={joints} median_ms={:.3} min_ms={:.3} max_ms={:.3} built_entries={}",+ times[times.len() / 2],+ times[0],+ times[times.len() - 1],+ visibility_work::built_take()+ );+ }+}+/// #1160: a design saved before joints could be hidden (format 19, no `hidden_joints`) opens/// with every joint shown; and a new joint that takes a deleted hidden joint's id starts shown.#[test]diff --git a/crates/engine/src/lib.rs b/crates/engine/src/lib.rsindex a860dd7edd..64ef16a918 100644--- a/crates/engine/src/lib.rs+++ b/crates/engine/src/lib.rs@@ -474,6 +474,10 @@ pub struct Session {/// (see [`Session::solve_joints`]).joints_settled: Option<(Arc<Document>, std::sync::Weak<solidcraft_doc::ModelState>)>,occurrence_visibility_cache: std::sync::Mutex<Option<occurrences::VisibilityCache>>,+ /// The hidden joints and folders of the view, as sets: the browser asks about every joint+ /// of the design on every frame (#3892), and an answer that walked the hidden lists made a+ /// frame cost grow with the size of the design.+ visibility_index_cache: std::sync::Mutex<Option<occurrences::VisibilityIndex>>,removed_later_cache: std::sync::Mutex<Option<occurrences::RemovedLaterCache>>,occurrence_bodies_cache: std::sync::Mutex<Option<occurrences::OccurrenceBodiesCache>>,placement_index_cache: std::sync::Mutex<Option<occurrences::PlacementIndex>>,@@ -679,6 +683,7 @@ impl Session {world_cache: Default::default(),joints_settled: None,occurrence_visibility_cache: Default::default(),+ visibility_index_cache: Default::default(),removed_later_cache: Default::default(),occurrence_bodies_cache: Default::default(),placement_index_cache: Default::default(),@@ -1548,6 +1553,7 @@ impl Session {world_cache: std::sync::Mutex::new(self.world_cache.lock().ok().and_then(|cache| cache.clone())),joints_settled: self.joints_settled.clone(),occurrence_visibility_cache: Default::default(),+ visibility_index_cache: Default::default(),removed_later_cache: Default::default(),occurrence_bodies_cache: Default::default(),placement_index_cache: Default::default(),diff --git a/crates/engine/src/occurrences.rs b/crates/engine/src/occurrences.rsindex 4ec5faed2c..a5cfae493e 100644--- a/crates/engine/src/occurrences.rs+++ b/crates/engine/src/occurrences.rs@@ -1,5 +1,5 @@//! Exact placement identity and session view/authoring state.-use crate::{EngineError, Result, Sel, Session};+use crate::{EngineError, Result, Sel, Session, Visibility};use solidcraft_doc::{FeatureKind, IDENTITY, Mat, PlaneRef, mat_mul};#[derive(Clone, Debug)]@@ -32,6 +32,139 @@ pub(crate) struct VisibilityCache {bodies: std::collections::BTreeSet<String>,}+/// The hidden joints and folders of the view, as sets: the browser asks about every joint+/// and every folder of the design on every frame, and an answer that walked the hidden+/// lists made a frame cost grow with the size of the design (#3892). The sets are built+/// when the view changes and are kept until it changes again.+pub(crate) struct VisibilityIndex {+ /// The revision of the design the sets were built for.+ revision: u64,+ /// Where the hidden joints were when the sets were built: the list is replaced (the+ /// whole view is replaced) or written in place (a joint is hidden or shown), and this+ /// says whether the list the sets were built from is still the list of the view.+ joints_at: ListId,+ /// The hidden joint ids.+ joint_ids: std::collections::HashSet<u64>,+ /// Where the hidden folders were when the sets were built.+ folders_at: ListId,+ /// The hidden folder keys.+ folder_keys: std::collections::HashSet<String>,+ /// The hidden joints and folders, as they were when the sets were built: they are+ /// compared with these to tell a change of the view from a change of the design alone+ /// (the design changes far more often than the view does).+ joints: Vec<u64>,+ folders: Vec<String>,+}++/// Where a list of hidden items was: the address of its buffer and how many items it held.+/// A list that is replaced gets a buffer of its own (the old one is still alive while the+/// new one is allocated, so the addresses differ), and a list that is written in place keeps+/// its buffer; the length then says how much of it is written.+#[derive(Clone, Copy, PartialEq, Eq)]+struct ListId {+ address: usize,+ length: usize,+}++impl ListId {+ fn of<T>(list: &[T]) -> ListId {+ ListId { address: list.as_ptr() as usize, length: list.len() }+ }+}++impl VisibilityIndex {+ /// The sets of the hidden joints and folders of a view.+ fn new(revision: u64, v: &Visibility) -> VisibilityIndex {+ let joint_ids: std::collections::HashSet<u64> = v.hidden_joints.iter().copied().collect();+ let folder_keys: std::collections::HashSet<String> = v.hidden_folders.iter().cloned().collect();+ visibility_work::built(joint_ids.len() as u64 + folder_keys.len() as u64);+ VisibilityIndex {+ revision,+ joints_at: ListId::of(&v.hidden_joints),+ joint_ids,+ folders_at: ListId::of(&v.hidden_folders),+ folder_keys,+ joints: v.hidden_joints.clone(),+ folders: v.hidden_folders.clone(),+ }+ }++ /// Do the sets still answer for this design and this view? The revision says the design+ /// changed; the lists say the view changed under it (the view is replaced outside the+ /// engine too, without a revision of its own).+ ///+ /// The first and last entries of each list are part of the answer: a buffer that was freed+ /// and handed out again holds another list, and the entries say so.+ fn matches(&self, revision: u64, v: &Visibility) -> bool {+ self.revision == revision+ && self.joints_at == ListId::of(&v.hidden_joints)+ && self.folders_at == ListId::of(&v.hidden_folders)+ && self.joints.first() == v.hidden_joints.first()+ && self.joints.last() == v.hidden_joints.last()+ && self.folders.first() == v.hidden_folders.first()+ && self.folders.last() == v.hidden_folders.last()+ }++ /// Are the hidden joints and folders the ones the sets were built from? A design that+ /// changed without touching the view leaves the sets alone.+ fn same_lists(&self, v: &Visibility) -> bool {+ self.joints == v.hidden_joints && self.folders == v.hidden_folders+ }++ /// The lists are the ones the sets were built from, at another revision.+ fn restamp(&mut self, revision: u64, v: &Visibility) {+ self.revision = revision;+ self.joints_at = ListId::of(&v.hidden_joints);+ self.folders_at = ListId::of(&v.hidden_folders);+ }+}++/// How many entries of the hidden lists a visibility answer had to walk, and how many the+/// index was built from. The browser asks about every joint of the design on every frame, so+/// the answer must not cost more as the design grows (#3892); the counters make that cost+/// visible to a test.+pub(crate) mod visibility_work {+ #[cfg(test)]+ use std::cell::Cell;++ // The counter of the thread that asks: the tests run beside each other, and each counts+ // its own work.+ #[cfg(test)]+ thread_local! {+ static WALKED: Cell<u64> = const { Cell::new(0) };+ static BUILT: Cell<u64> = const { Cell::new(0) };+ }++ /// The entries the answers of this thread walked since the last [`take`].+ #[cfg(test)]+ pub(crate) fn take() -> u64 {+ WALKED.with(|walked| walked.replace(0))+ }++ /// Count the entries an answer walked.+ pub(crate) fn add(walked: u64) {+ #[cfg(test)]+ WALKED.with(|count| count.set(count.get() + walked));+ #[cfg(not(test))]+ let _ = walked;+ }++ /// Count the entries the index was built from: the index is built once per change of the+ /// view, not once per answer, so this work is apart from the answers' own.+ pub(crate) fn built(built: u64) {+ #[cfg(test)]+ BUILT.with(|count| count.set(count.get() + built));+ #[cfg(not(test))]+ let _ = built;+ }++ /// The entries the index was built from since the last [`built_take`].+ #[cfg(test)]+ pub(crate) fn built_take() -> u64 {+ BUILT.with(|count| count.replace(0))+ }+}+/// [`Session::occurrence_bodies`] answers for one document, evaluated model and set of pending/// moves. The document and model are kept alive with the answers, so neither a new document nor/// a later allocation at the same address can reuse them.@@ -562,10 +695,36 @@ impl Session {Ok(count)}+ /// The hidden joints and folders of the view, as sets: the browser asks about every joint+ /// of the design on every frame, and an answer that walked the hidden lists made a frame+ /// cost grow with the size of the design (#3892). The sets are built when the view changes+ /// and are kept until it changes again.+ fn visibility_index(&self) -> Option<std::sync::MutexGuard<'_, Option<VisibilityIndex>>> {+ let mut cache = self.visibility_index_cache.lock().ok()?;+ let v = &self.visibility;+ if cache.as_ref().is_none_or(|c| !c.matches(self.revision, v)) {+ // The design or the view changed. The sets are rebuilt only when the hidden lists+ // themselves changed: a change of the design alone leaves them alone.+ if cache.as_ref().is_some_and(|c| c.same_lists(v)) {+ if let Some(c) = cache.as_mut() {+ c.restamp(self.revision, v);+ }+ } else {+ *cache = Some(VisibilityIndex::new(self.revision, v));+ }+ }+ Some(cache)+ }+/// Whether a joint's glyph is drawn (`browser.visibility`). Hiding a joint only hides its/// drawing: it still constrains and drives the parts./// Is the joint shown itself (its own eye; the Joints folder may still hide it)?pub fn joint_visible(&self, id: u64) -> bool {+ if let Some(cache) = self.visibility_index() {+ return cache.as_ref().is_none_or(|c| !c.joint_ids.contains(&id));+ }+ // The lock is poisoned: answer from the list itself.+ visibility_work::add(self.visibility.hidden_joints.len() as u64);!self.visibility.hidden_joints.contains(&id)}@@ -582,6 +741,11 @@ impl Session {/// Is a browser folder or group hidden itself (by tree key, see `Visibility::hidden_folders`)?pub fn folder_hidden(&self, key: &str) -> bool {+ if let Some(cache) = self.visibility_index() {+ return cache.as_ref().is_none_or(|c| c.folder_keys.contains(key));+ }+ // The lock is poisoned: answer from the list itself.+ visibility_work::add(self.visibility.hidden_folders.len() as u64);self.visibility.hidden_folders.iter().any(|k| k == key)}