Skip to content

SolidCraft: an open-source parametric 3D CAD application built in pure Rust. Desktop, web, and automation through MCP.

Browser tree hover stays fast on a nested STEP assembly (#3892) (#3894)

raykholo1e81e65e7e56
diff --git a/crates/engine/src/lib.rs b/crates/engine/src/lib.rs
index 1ae0021732..008b333ff6 100644
--- a/crates/engine/src/lib.rs
+++ b/crates/engine/src/lib.rs
@@ -288,6 +288,7 @@ pub struct Session {
world_cache: std::sync::Mutex<Option<WorldCache>>,
occurrence_visibility_cache: std::sync::Mutex<Option<occurrences::VisibilityCache>>,
removed_later_cache: std::sync::Mutex<Option<occurrences::RemovedLaterCache>>,
+ occurrence_bodies_cache: std::sync::Mutex<Option<occurrences::OccurrenceBodiesCache>>,
/// Pairs `inspect.clearance` measured, by the bodies' geometry where they stood (shared with
/// scratch copies): a body that did not change or move is not measured again.
clearance_cache: std::sync::Arc<std::sync::Mutex<cmd::clearance::PairCache>>,
@@ -455,6 +456,7 @@ impl Session {
world_cache: Default::default(),
occurrence_visibility_cache: Default::default(),
removed_later_cache: Default::default(),
+ occurrence_bodies_cache: Default::default(),
clearance_cache: Default::default(),
body_renames: Vec::new(),
contact_meshes: Default::default(),
@@ -1066,6 +1068,7 @@ impl Session {
world_cache: std::sync::Mutex::new(self.world_cache.lock().ok().and_then(|cache| cache.clone())),
occurrence_visibility_cache: Default::default(),
removed_later_cache: Default::default(),
+ occurrence_bodies_cache: Default::default(),
clearance_cache: self.clearance_cache.clone(),
body_renames: Vec::new(),
contact_meshes: self.contact_meshes.clone(),
diff --git a/crates/engine/src/occurrences.rs b/crates/engine/src/occurrences.rs
index d50a268d1d..786380067d 100644
--- a/crates/engine/src/occurrences.rs
+++ b/crates/engine/src/occurrences.rs
@@ -32,6 +32,25 @@ pub(crate) struct VisibilityCache {
bodies: std::collections::BTreeSet<String>,
}
+/// [`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.
+pub(crate) struct OccurrenceBodiesCache {
+ doc: std::sync::Arc<solidcraft_doc::Document>,
+ model: std::sync::Arc<solidcraft_doc::ModelState>,
+ pending_moves: std::collections::BTreeMap<u64, Mat>,
+ bodies: std::collections::HashMap<(Vec<u64>, bool), std::sync::Arc<[String]>>,
+}
+
+/// How many placements' answers [`OccurrenceBodiesCache`] keeps before it starts again.
+const OCCURRENCE_BODIES_CACHED: usize = 256;
+
+impl OccurrenceBodiesCache {
+ fn matches(&self, session: &Session, model: &std::sync::Arc<solidcraft_doc::ModelState>) -> bool {
+ std::sync::Arc::ptr_eq(&self.doc, &session.doc) && std::sync::Arc::ptr_eq(&self.model, model) && self.pending_moves == session.pending_moves
+ }
+}
+
/// [`Session::body_removed_later`] for one revision, sketch and history marker.
pub(crate) struct RemovedLaterCache {
revision: u64,
@@ -141,6 +160,38 @@ impl Session {
/// Bounded descendant expansion. Never return an incomplete selection or bounds list.
pub fn occurrence_bodies(&self, path: &[u64], descendants: bool) -> Result<Vec<String>> {
+ Ok(self.occurrence_bodies_shared(path, descendants)?.to_vec())
+ }
+
+ /// [`Self::occurrence_bodies`], shared and remembered until the document, the evaluated
+ /// model or the pending moves change: a nested placement's subtree is walked once, not on
+ /// every frame that lights it (a browser row under the pointer, the active placement; #3892).
+ pub fn occurrence_bodies_shared(&self, path: &[u64], descendants: bool) -> Result<std::sync::Arc<[String]>> {
+ let model = self.model.state();
+ let key = (path.to_vec(), descendants);
+ if let Ok(cache) = self.occurrence_bodies_cache.lock()
+ && let Some(bodies) = cache.as_ref().filter(|c| c.matches(self, &model)).and_then(|c| c.bodies.get(&key))
+ {
+ return Ok(bodies.clone());
+ }
+ let bodies: std::sync::Arc<[String]> = self.expand_occurrence_bodies(path, descendants)?.into();
+ if let Ok(mut cache) = self.occurrence_bodies_cache.lock() {
+ if cache.as_ref().is_none_or(|c| !c.matches(self, &model) || c.bodies.len() >= OCCURRENCE_BODIES_CACHED) {
+ *cache = Some(OccurrenceBodiesCache {
+ doc: self.doc.clone(),
+ model,
+ pending_moves: self.pending_moves.clone(),
+ bodies: std::collections::HashMap::new(),
+ });
+ }
+ if let Some(cache) = cache.as_mut() {
+ cache.bodies.insert(key, bodies.clone());
+ }
+ }
+ Ok(bodies)
+ }
+
+ fn expand_occurrence_bodies(&self, path: &[u64], descendants: bool) -> Result<Vec<String>> {
let mut stack = vec![self.occurrence_placement(path)?];
let mut out = Vec::new();
let mut count = 0;
diff --git a/crates/ui-egui/src/browser_model_tests.rs b/crates/ui-egui/src/browser_model_tests.rs
index 0725d5daca..75594e8b88 100644
--- a/crates/ui-egui/src/browser_model_tests.rs
+++ b/crates/ui-egui/src/browser_model_tests.rs
@@ -106,3 +106,38 @@ fn a_face_picked_in_the_viewport_opens_the_browser_to_its_placements_body() {
assert!(h.app.tree.order.contains(&format!("b:{second}")), "the row is drawn");
assert!(!h.app.tree.order.contains(&format!("b:{first}")), "the other placement's row stays closed away");
}
+
+/// Hovering a placement row lights its whole subtree: the subtree is walked once per model, not
+/// on every frame the pointer stays on the row (#3892: a nested STEP assembly cost a frame per
+/// thousand placements while the pointer was over the tree, even with the rows collapsed).
+#[test]
+fn hovering_a_collapsed_nested_placement_walks_its_subtree_once() {
+ let mut h = crate::scenario::Harness::new();
+ let sub = h.app.run("component.create", json!({"name":"Sub"})).unwrap();
+ let (sub_component, sub_occurrence) = (sub["component"].as_u64().unwrap(), sub["occurrence"].as_u64().unwrap());
+ let leaf = h.app.run("component.create", json!({"name":"Leaf"})).unwrap()["component"].as_u64().unwrap();
+ h.app.run("solid.box", json!({"length":10,"width":10,"height":10})).unwrap();
+ h.app.run("component.activate", json!({"component":sub_component})).unwrap();
+ for i in 1..6 {
+ h.app.run("occurrence.copy", json!({"component":leaf,"translate":[i as f64 * 20.0, 0, 0]})).unwrap();
+ }
+ h.app.run("component.activate", json!({"component":0})).unwrap();
+ h.app.run("occurrence.copy", json!({"component":sub_component,"translate":[0, 40, 0]})).unwrap();
+ let path = vec![sub_occurrence];
+ h.app.tree.collapsed.insert(super::occurrence_key(&path));
+ h.frames(3);
+ let at = crate::scenario::handle_at(&format!("row:occurrence:{sub_occurrence}")).unwrap();
+ h.call("ui.move", json!({"x":at.x,"y":at.y}));
+ let walked = h.app.session.occurrence_bodies_shared(&path, true).unwrap();
+ assert_eq!(walked.len(), 6, "{walked:?}");
+ assert_eq!(h.app.viewport.hover_bodies, walked.to_vec(), "the row lights its placement's bodies and every one below it");
+ h.frames(10);
+ assert_eq!(h.app.viewport.hover_bodies, walked.to_vec());
+ assert!(Arc::ptr_eq(&walked, &h.app.session.occurrence_bodies_shared(&path, true).unwrap()), "frames on the same row walk nothing again");
+ // An edit is a new model: the row lights the subtree that is there now.
+ h.app.run("component.activate", json!({"component":sub_component})).unwrap();
+ h.app.run("occurrence.copy", json!({"component":leaf,"translate":[0, 80, 0]})).unwrap();
+ h.app.run("component.activate", json!({"component":0})).unwrap();
+ h.call("ui.move", json!({"x":at.x,"y":at.y + 1.0}));
+ assert_eq!(h.app.viewport.hover_bodies.len(), 7);
+}