Placing and meshing: no threads started for bodies that are done already (#6406)
Every command in a design with placed components places their bodies again on up to 16 threads, though nearly all of them are taken as they were placed before (the same body by the same matrix). Those are now taken on the calling thread and only the rest are placed on threads. Warming the meshes (and display plans) of a state likewise starts threads only for the bodies that have none yet. Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
+35
-19
@@ -1091,7 +1091,10 @@ impl ModelState {
|
||||
/// each keeps its own mesh, so the meshes are those `ModelBody::mesh` makes one by one).
|
||||
/// Stops starting bodies once `go_on` says no.
|
||||
pub fn warm_meshes(&self, go_on: impl Fn() -> bool + Sync) {
|
||||
kernel::for_each_on_threads(&self.bodies, 16, |b| {
|
||||
// Only bodies without a mesh: starting threads just to find every mesh made costs more
|
||||
// than looking (most bodies are unchanged from one command to the next).
|
||||
let cold: Vec<_> = self.bodies.iter().filter(|b| b.mesh.get().is_none()).collect();
|
||||
kernel::for_each_on_threads(&cold, 16, |b| {
|
||||
if go_on() {
|
||||
let _ = b.mesh();
|
||||
}
|
||||
@@ -1100,7 +1103,10 @@ impl ModelState {
|
||||
/// [`ModelState::warm_meshes`], and every body's display plan at the current display quality
|
||||
/// (what a GPU viewport draws).
|
||||
pub fn warm_display(&self) {
|
||||
kernel::for_each_on_threads(&self.bodies, 16, |b| {
|
||||
let q = crate::display_quality::current();
|
||||
let planned = |b: &ModelBody| b.plan.get(q.index()).is_none_or(|cell| cell.get().is_some());
|
||||
let cold: Vec<_> = self.bodies.iter().filter(|b| b.mesh.get().is_none() || !planned(b)).collect();
|
||||
kernel::for_each_on_threads(&cold, 16, |b| {
|
||||
let _ = b.mesh();
|
||||
let _ = b.display_plan();
|
||||
});
|
||||
@@ -7825,26 +7831,31 @@ impl PlacedBodies {
|
||||
|
||||
/// The body `b` placed by `m`, from an earlier call when `b` is the same body as then.
|
||||
fn placed(&self, b: &ModelBody, m: &crate::Mat, name: String) -> ModelBody {
|
||||
match self.reused(b, m) {
|
||||
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();
|
||||
if let Some(placed) = self.taken(b, m, &name) {
|
||||
return placed;
|
||||
}
|
||||
match kernel::place_matrix(&b.body, *m) {
|
||||
Ok(body) => {
|
||||
let mut fresh = ModelBody::new(name, body, b.feature);
|
||||
fresh.mesh_placement = placed_mesh::PlacedMesh::new(b, m).map(Arc::new);
|
||||
fresh
|
||||
}
|
||||
None => match kernel::place_matrix(&b.body, *m) {
|
||||
Ok(body) => {
|
||||
let mut fresh = ModelBody::new(name, body, b.feature);
|
||||
fresh.mesh_placement = placed_mesh::PlacedMesh::new(b, m).map(Arc::new);
|
||||
fresh
|
||||
}
|
||||
Err(_) => ModelBody::new(name, b.body.clone(), b.feature),
|
||||
},
|
||||
Err(_) => ModelBody::new(name, b.body.clone(), b.feature),
|
||||
}
|
||||
}
|
||||
|
||||
/// The body `b` placed by `m` as an earlier call placed it, when `b` is the same body as then.
|
||||
fn taken(&self, b: &ModelBody, m: &crate::Mat, name: &str) -> Option<ModelBody> {
|
||||
self.reused(b, m).map(|placed| {
|
||||
let mut fresh = ModelBody::new(name.to_string(), 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();
|
||||
fresh
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
/// Bodies [`state_in_frame_at`] moved into a feature's frame, by source body and matrix: a
|
||||
@@ -7953,7 +7964,12 @@ pub fn world_state_reusing(doc: &Document, st: &ModelState, placed_before: &mut
|
||||
}
|
||||
}
|
||||
}
|
||||
let mut moved = kernel::map_on_threads(&jobs, 16, |(b, bm, name)| placed_before.placed(b, bm, name.clone())).into_iter().zip(&jobs);
|
||||
// Bodies placed as before are taken here: starting threads only to copy them cost every
|
||||
// command in a design with placed components more than the copies.
|
||||
let reused: Vec<_> = jobs.iter().map(|(b, bm, name)| placed_before.taken(b, bm, name)).collect();
|
||||
let fresh: Vec<_> = jobs.iter().zip(&reused).filter(|(_, r)| r.is_none()).map(|(job, _)| job).collect();
|
||||
let mut placed_fresh = kernel::map_on_threads(&fresh, 16, |(b, bm, name)| placed_before.placed(b, bm, name.clone())).into_iter();
|
||||
let mut moved = reused.into_iter().map(|r| r.or_else(|| placed_fresh.next().flatten())).zip(&jobs);
|
||||
for (comp, path, m) in placements {
|
||||
let primary = path == first_path(comp);
|
||||
let ident = crate::is_identity(&m);
|
||||
|
||||
@@ -297,6 +297,33 @@ mod tests {
|
||||
assert_ne!(new.bounds(), moved.bodies[0].mesh().bounds());
|
||||
assert!(Arc::ptr_eq(&old, &before.bodies[0].mesh()), "editing does not mutate older snapshots");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn placements_taken_as_before_and_placed_again_keep_their_order() {
|
||||
let mut doc = crate::Document::new("instances");
|
||||
let component = doc.add_component(Some("Part"), 0).unwrap();
|
||||
doc.body_components.insert("Pin".into(), component);
|
||||
doc.occurrences[0].transform = crate::rigid(Vec3::new(10., 0., 0.), Vec3::ZERO, Vec3::Y, 0.3);
|
||||
let second = doc.add_occurrence(component, 0, crate::rigid(Vec3::new(0., 20., 0.), Vec3::ZERO, Vec3::X, 0.6)).unwrap();
|
||||
doc.add_occurrence(component, 0, crate::rigid(Vec3::new(0., -20., 5.), Vec3::ZERO, Vec3::Z, 1.1)).unwrap();
|
||||
let mut state = crate::ModelState::default();
|
||||
state.bodies.push(ModelBody::new("Pin".into(), kernel::cylinder(Vec3::ZERO, Vec3::Z, 2., 8.).unwrap(), 1));
|
||||
let mut cache = super::super::PlacedBodies::default();
|
||||
let before = super::super::world_state_reusing(&doc, &state, &mut cache);
|
||||
let meshes: Vec<_> = before.bodies.iter().map(|b| b.mesh()).collect();
|
||||
// The middle placement moves: the other two are taken as they were, it is placed again.
|
||||
doc.occurrences.iter_mut().find(|o| o.id == second).unwrap().transform = crate::rigid(Vec3::new(0., 40., 0.), Vec3::ZERO, Vec3::X, 0.2);
|
||||
let after = super::super::world_state_reusing(&doc, &state, &mut cache);
|
||||
let fresh = super::super::world_state_reusing(&doc, &state, &mut super::super::PlacedBodies::default());
|
||||
let names = |st: &crate::ModelState| st.bodies.iter().map(|b| b.name.clone()).collect::<Vec<_>>();
|
||||
assert_eq!(names(&after), names(&fresh));
|
||||
assert_eq!(names(&after), names(&before));
|
||||
for (i, (a, f)) in after.bodies.iter().zip(&fresh.bodies).enumerate() {
|
||||
assert_eq!(a.mesh().bounds(), f.mesh().bounds(), "body {i} is where a fresh placement puts it");
|
||||
}
|
||||
assert!(Arc::ptr_eq(&after.bodies[0].mesh(), &meshes[0]) && Arc::ptr_eq(&after.bodies[2].mesh(), &meshes[2]));
|
||||
assert!(!Arc::ptr_eq(&after.bodies[1].mesh(), &meshes[1]), "the moved placement is placed again");
|
||||
}
|
||||
#[test]
|
||||
fn only_proper_rigid_affine_matrices_reuse_reference_meshes() {
|
||||
let source = ModelBody::new("Part".into(), kernel::box_solid(Vec3::ZERO, Vec3::new(2., 3., 4.)).unwrap(), 1);
|
||||
|
||||
Reference in New Issue
Block a user