From 614e006eacf3311eba10a963205bf81c6d71decf Mon Sep 17 00:00:00 2001 From: Jackson Oursland Date: Tue, 27 Oct 2026 03:33:53 +0000 Subject: [PATCH] Engine: keep every placed body's frame with the world state (#7411) Each command that names a body (appearance, material, move, ...) mapped it to its component's own name by working out every placed body's world name and frame, so a command on a large assembly cost a walk of the whole design. The list is now kept with the world-placement cache while it is current. Co-authored-by: Claude Opus 5.5 (1M context) --- crates/engine/src/frames.rs | 7 ++---- crates/engine/src/lib.rs | 21 ++++++++++++++++ .../engine/src/world_placement_cache_tests.rs | 25 +++++++++++++++++++ 3 files changed, 48 insertions(+), 5 deletions(-) diff --git a/crates/engine/src/frames.rs b/crates/engine/src/frames.rs index d7564358c..e55b6eaca 100644 --- a/crates/engine/src/frames.rs +++ b/crates/engine/src/frames.rs @@ -101,7 +101,7 @@ fn table(id: &str) -> &'static [(&'static str, K)] { struct Frames { /// Worked out on first use: most commands pick no body, and a large assembly's frames cost /// a walk of every body on every command. - bodies: std::cell::OnceCell>, + bodies: std::cell::OnceCell, active: Mat, /// The frame of the sketch being edited (its component), else the active component's. sketch: Mat, @@ -117,10 +117,7 @@ fn frames(s: &Session) -> Frames { impl Frames { /// World name, the component's own body name, and its frame, for every body. fn bodies(&self, s: &Session) -> &[(String, String, Mat)] { - self.bodies.get_or_init(|| { - let st = s.model.state(); - s.placed_document().body_frames_with_source(&st).into_iter().map(|(world, own, _, _, m)| (world, own, m)).collect() - }) + self.bodies.get_or_init(|| s.body_frames()) } } diff --git a/crates/engine/src/lib.rs b/crates/engine/src/lib.rs index 31d4a0b00..e9824c440 100644 --- a/crates/engine/src/lib.rs +++ b/crates/engine/src/lib.rs @@ -264,8 +264,14 @@ struct WorldCache { world: Arc, /// The moved components' bodies as placed for `world`, reused by the next world state. placed: Arc>, + /// Every placed body's world name, own name and frame for `world` ([`Session::body_frames`]), + /// worked out when a command first maps a body name. + frames: Arc>, } +/// Every placed body's world name, its component's own name for it, and its frame. +pub(crate) type BodyFrames = Arc>; + impl WorldCache { fn matches(&self, session: &Session, model: &Arc) -> bool { Arc::ptr_eq(&self.doc, &session.doc) @@ -795,11 +801,26 @@ impl Session { active_occurrence_path: self.active_occurrence_path.clone(), world: w.clone(), placed, + frames: Arc::default(), }); } w } + /// Every placed body's world name, own name and frame. Kept with the world state while it is + /// current, so commands naming a body in a large assembly do not each name every body again. + pub(crate) fn body_frames(&self) -> BodyFrames { + let st = self.model.state(); + let compute = || -> BodyFrames { + Arc::new(self.placed_document().body_frames_with_source(&st).into_iter().map(|(world, own, _, _, m)| (world, own, m)).collect()) + }; + let cell = self.world_cache.lock().ok().and_then(|c| c.as_ref().filter(|c| c.matches(self, &st)).map(|c| c.frames.clone())); + match cell { + Some(cell) => cell.get_or_init(compute).clone(), + None => compute(), + } + } + pub fn refresh(&mut self) { self.model.evaluate(&self.doc); #[cfg(test)] diff --git a/crates/engine/src/world_placement_cache_tests.rs b/crates/engine/src/world_placement_cache_tests.rs index f1528176f..5c6457cfc 100644 --- a/crates/engine/src/world_placement_cache_tests.rs +++ b/crates/engine/src/world_placement_cache_tests.rs @@ -262,3 +262,28 @@ fn joint_drive_reuses_reference_tessellation_across_rigid_poses() { } assert_eq!(local.bounds(), bounds, "old snapshots stay immutable"); } + +/// Commands map world body names through every body's frame: worked out once while the world +/// state is current, and again when a placement moves or a body is added. +#[test] +fn body_frames_are_kept_with_the_world_state() { + let mut session = Session::default(); + let first = run(&mut session, "component.create", json!({"name":"Part"}))["occurrence"].as_u64().unwrap(); + run(&mut session, "solid.box", json!({"length":2,"width":3,"height":4,"body_name":"Block","operation":"new"})); + let second = run(&mut session, "occurrence.copy", json!({"occurrence":first}))["occurrence"].as_u64().unwrap(); + let _ = session.world_state(); + let frames = session.body_frames(); + assert!(Arc::ptr_eq(&frames, &session.body_frames()), "kept while the world state is current"); + let copy = session.occurrence_placement(&[second]).unwrap().bodies[0].world.clone(); + assert!(frames.iter().any(|(world, own, _)| *world == copy && own == "Block"), "a copy maps back to its definition"); + run(&mut session, "occurrence.move", json!({"occurrence":second,"translate":[10,0,0],"capture":false})); + let _ = session.world_state(); + let moved = session.body_frames(); + assert!(!Arc::ptr_eq(&frames, &moved), "a moved placement works them out again"); + let at = |frames: &crate::BodyFrames| frames.iter().find(|(world, ..)| *world == copy).map(|(_, _, m)| m[3][0]); + assert_eq!(at(&moved), Some(10.0), "with the placement's new frame"); + assert_eq!(at(&frames), Some(0.0)); + run(&mut session, "solid.box", json!({"length":1,"width":1,"height":1,"body_name":"Other","operation":"new"})); + let _ = session.world_state(); + assert!(session.body_frames().iter().any(|(world, ..)| world.starts_with("Other")), "a new body is in them"); +}