From 1e81e65e7e5667622c03a5438ecbf94b2f84000e Mon Sep 17 00:00:00 2001 From: raykholo Date: Sat, 24 Oct 2026 21:59:55 +0000 Subject: [PATCH] Browser tree hover stays fast on a nested STEP assembly (#3892) (#3894) * claim: #3892 * Browser tree hover stays fast on a nested STEP assembly (#3892) Hovering a placement row lit its whole subtree by walking it again (Session::occurrence_bodies) on every frame, and opening a STEP file makes its top placement active, so every highlight rebuild walked the whole assembly again for the active placement's bodies. On a nested assembly each walk resolves every placement below it, which cost 0.5 s or more per walk. - Session::occurrence_bodies remembers its answers per placement until the document, the evaluated model or the pending moves change (occurrence_bodies_shared hands out the shared list). - The highlight looks hovered bodies and faces up by name instead of scanning the hovered list once per body. --- crates/engine/src/lib.rs | 3 ++ crates/engine/src/occurrences.rs | 51 +++++++++++++++++++++++ crates/ui-egui/src/browser_model_tests.rs | 35 ++++++++++++++++ 3 files changed, 89 insertions(+) diff --git a/crates/engine/src/lib.rs b/crates/engine/src/lib.rs index 1ae002173..008b333ff 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>, occurrence_visibility_cache: std::sync::Mutex>, removed_later_cache: std::sync::Mutex>, + occurrence_bodies_cache: std::sync::Mutex>, /// 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>, @@ -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 d50a268d1..786380067 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, } +/// [`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, + model: std::sync::Arc, + pending_moves: std::collections::BTreeMap, + bodies: std::collections::HashMap<(Vec, 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) -> 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> { + 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> { + 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> { 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 0725d5dac..75594e8b8 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); +}