Inspect: bodies measured on all cores, and once while they stay the same (#6412)

document.inspect with measure (what inspect_design and the
solidcraft://design resource ask for) measured every body one after
the other, and again on every call: on a 1,280-body design each call
took 25 to 60 s. It now measures them on all cores, as
inspect.measure does, and each body keeps its measurement in a cell
shared by its clones (with the body it was measured on, as the outline
box is kept), so a design inspected again measures only the bodies that
changed. Placements taken as before keep their placed body's cell too.

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
semmelrinde
2026-10-25 01:55:23 +00:00
committed by GitHub
parent b6a5a9166a
commit 91549d29fb
3 changed files with 81 additions and 4 deletions
+18
View File
@@ -65,6 +65,9 @@ pub struct ModelBody {
/// [`Body::outline_bounds`] of `body` (with the body it was measured on), worked out once
/// and shared by clones.
outline: Arc<OnceLock<(Body, solidcraft_geom::Aabb3)>>,
/// [`kernel::measure`] of `body` (with the body it was measured on), worked out once and
/// shared by clones: inspecting a design again measures only the bodies that changed.
measured: Arc<OnceLock<(Body, kernel::Result<kernel::BodyMeasure>)>>,
}
impl ModelBody {
@@ -89,6 +92,7 @@ impl ModelBody {
bytes: 0,
source_key: 0,
outline: Arc::new(OnceLock::new()),
measured: Arc::new(OnceLock::new()),
}
}
@@ -114,6 +118,19 @@ impl ModelBody {
}
}
/// [`kernel::measure`] of the body (volume, area, centre of mass, box and topology), worked
/// out once: snapshots and passed-through copies of this geometry share it.
pub fn measure(&self) -> kernel::Result<kernel::BodyMeasure> {
let measure = || {
#[cfg(test)]
cost_tests::count(|c| c.measures += 1);
kernel::measure(&self.body)
};
let (body, m) = self.measured.get_or_init(|| (self.body.clone(), measure()));
// The body was replaced in this cell's holder since: measure it again.
if body.same(&self.body) { m.clone() } else { measure() }
}
/// Identity of this body's geometry where it stands: the same for a body evaluated or
/// placed again from the same inputs at the same place, so work measured on it can be
/// reused (none for a body the evaluation cache does not key).
@@ -7711,6 +7728,7 @@ impl PlacedBodies {
Some(placed) => {
let mut fresh = ModelBody::new(name, placed.body.clone(), b.feature);
fresh.mesh = placed.mesh.clone();
fresh.measured = placed.measured.clone();
fresh.mesh_placement = placed.mesh_placement.clone();
fresh.plan = placed.plan.clone();
fresh.plan_mesh = placed.plan_mesh.clone();
+56 -2
View File
@@ -14,16 +14,18 @@ use solidcraft_sketch::Sketch;
use crate::*;
/// Work counted on this thread: bodies moved, STEP texts digested, joint faces measured.
/// Work counted on this thread: bodies moved, STEP texts digested, joint faces and bodies
/// measured.
#[derive(Clone, Copy, Debug, Default, PartialEq)]
pub(super) struct Counts {
pub moved: usize,
pub digested: usize,
pub joint_faces: usize,
pub measures: usize,
}
thread_local! {
static COUNTS: Cell<Counts> = const { Cell::new(Counts { moved: 0, digested: 0, joint_faces: 0 }) };
static COUNTS: Cell<Counts> = const { Cell::new(Counts { moved: 0, digested: 0, joint_faces: 0, measures: 0 }) };
}
pub(super) fn count(f: impl FnOnce(&mut Counts)) {
@@ -421,3 +423,55 @@ fn a_tool_cutting_another_part_cuts_again_when_that_part_moves() {
assert_eq!(model.last_recomputed, 0);
assert!((volume(&model, &body) - (400.0 - 16.0)).abs() < 1e-6, "{}", volume(&model, &body));
}
#[test]
fn a_body_is_measured_once_while_it_stays_the_same() {
let mut doc = Document::new("measured once");
let cylinder = |doc: &mut Document, x: f64| {
let kind = FeatureKind::Cylinder {
base: point_expr(Vec3::new(x, 0.0, 0.0)),
axis: Dir(Vec3::Z),
radius: "2".into(),
height: "10".into(),
operation: Operation::NewBody,
};
doc.add_feature(kind, None).unwrap();
};
cylinder(&mut doc, 0.0);
cylinder(&mut doc, 10.0);
let mut model = Model::new();
model.evaluate(&doc);
let measure_all = |st: &ModelState| {
for b in &st.bodies {
b.measure().unwrap();
}
};
let first = model.state();
assert_eq!(counted(|| measure_all(&first)).measures, 2);
assert_eq!(counted(|| measure_all(&first)).measures, 0, "measured again");
// A feature added: only its body is new.
cylinder(&mut doc, 20.0);
model.evaluate(&doc);
let st = model.state();
assert_eq!(counted(|| measure_all(&st)).measures, 1);
// A body replaced in its holder is measured as it is now; the others keep theirs.
let mut replaced = st.bodies[0].clone();
replaced.body = kernel::box_solid(Vec3::ZERO, Vec3::new(1.0, 2.0, 3.0)).unwrap();
assert!((replaced.measure().unwrap().volume - 6.0).abs() < 1e-9);
assert!((st.bodies[0].measure().unwrap().volume - first.bodies[0].measure().unwrap().volume).abs() < 1e-12);
}
#[test]
fn placed_bodies_taken_as_before_keep_their_measure() {
let mut doc = Document::new("placed measure");
let component = doc.add_component(Some("Part"), 0).unwrap();
doc.body_components.insert("Pin".into(), component);
doc.occurrences[0].transform = rigid(Vec3::new(10.0, 0.0, 0.0), Vec3::ZERO, Vec3::Y, 0.3);
let mut state = ModelState::default();
state.bodies.push(ModelBody::new("Pin".into(), kernel::cylinder(Vec3::ZERO, Vec3::Z, 2.0, 8.0).unwrap(), 1));
let mut cache = PlacedBodies::default();
let before = world_state_reusing(&doc, &state, &mut cache);
let centroid = before.bodies[0].measure().unwrap().centroid;
let again = world_state_reusing(&doc, &state, &mut cache);
assert_eq!(counted(|| assert_eq!(again.bodies[0].measure().unwrap().centroid, centroid)).measures, 0);
}
+7 -2
View File
@@ -54,7 +54,7 @@ pub static COMMANDS: &[CommandSpec] = &[
];
pub fn measure_json(b: &solidcraft_doc::ModelBody) -> Value {
match solidcraft_kernel::measure(&b.body) {
match b.measure() {
Ok(m) => json!({
"name": b.name,
"volume_mm3": m.volume,
@@ -248,11 +248,16 @@ fn inspect(s: &mut Session, p: &Value) -> Result<Value> {
})
.collect();
let with_measure = bool_(p, "measure").unwrap_or(false);
// Each body is measured on its own: on all cores, in body order (as `inspect.measure`).
let mut measured = if with_measure { solidcraft_kernel::map_on_threads(&st.bodies, 16, measure_json) } else { Vec::new() }.into_iter();
let bodies: Vec<Value> = st
.bodies
.iter()
.map(|b| {
let mut v = if with_measure { measure_json(b) } else { json!({"name": b.name, "feature": b.feature}) };
let mut v = match measured.next() {
Some(m) => m.unwrap_or_else(|| json!({"name": b.name, "error": "measuring this body failed"})),
None => json!({"name": b.name, "feature": b.feature}),
};
if let Some(o) = v.as_object_mut() {
o.insert("visible".into(), json!(s.body_visible(&b.name)));
if s.body_removed_later(&b.name) {