Extrude dialog: work out the automatic operation once, not every frame (#10491)

* claim: Extrude dialog: work out the automatic operation once, not every frame

* Extrude dialog: work out the automatic operation once, not every frame

An open Extrude window with the automatic operation on asked the engine
for `auto_operation` every frame. That probes the bodies with
`Mesh::contains`, rays through every triangle, so a large part cost tens
of milliseconds per frame with nothing changing.

The window now keeps the answer with a key over what it depends on: the
extrude's parameters (participants included), the design's revision and
id, and the active component and occurrence path. The latch still runs
every frame as before.

Open Extrude over a 1M-triangle mesh body: 29.8 -> 0.87 ms per frame
(headless harness, median of 3).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

---------

Co-authored-by: Markus Kaltenbrunner <kaltenbrunnermarkus@users.noreply.github.com>
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
kaltenbrunnermarkus
2026-11-01 09:43:06 +00:00
committed by GitHub
parent ea63eec845
commit 74eda80b5d
3 changed files with 72 additions and 1 deletions
+48 -1
View File
@@ -3105,7 +3105,29 @@ fn auto_operation(app: &SolveApp, d: &mut Dialog) {
nothing_shown = shown.is_empty();
params["participants"] = json!(shown);
}
let fresh = if nothing_shown { Some("new") } else { solidcraft_engine::auto_operation(&app.session, &params) };
let fresh = if nothing_shown {
Some("new")
} else {
// Asked again only when the parameters or the design change: it casts rays through
// every triangle of the bodies, milliseconds a frame on a large part.
let key = auto_operation_key(app, &params);
let cached = match &d.kind {
Kind::Extrude { smart, .. } => smart.auto.filter(|(k, _)| *k == key).map(|(_, op)| op),
_ => None,
};
match cached {
Some(op) => op,
None => {
#[cfg(test)]
AUTO_OPERATION_ASKED.with(|n| n.set(n.get() + 1));
let op = solidcraft_engine::auto_operation(&app.session, &params);
if let Kind::Extrude { smart, .. } = &mut d.kind {
smart.auto = Some((key, op));
}
op
}
}
};
let Some(fresh) = fresh.and_then(|op| OPS.iter().position(|o| *o == op)) else {
return;
};
@@ -3114,6 +3136,31 @@ fn auto_operation(app: &SolveApp, d: &mut Dialog) {
}
}
/// What [`solidcraft_engine::auto_operation`] depends on besides its parameters: the design
/// (its revision counts every change, shown bodies too), which design it is, and the active
/// component the extrude goes into.
fn auto_operation_key(app: &SolveApp, params: &Value) -> u64 {
use std::hash::{Hash, Hasher};
let mut h = std::collections::hash_map::DefaultHasher::new();
params.to_string().hash(&mut h);
app.session.revision.hash(&mut h);
app.session.document_id.hash(&mut h);
app.session.active_component.hash(&mut h);
app.session.active_occurrence_path.hash(&mut h);
h.finish()
}
#[cfg(test)]
thread_local! {
static AUTO_OPERATION_ASKED: std::cell::Cell<usize> = const { std::cell::Cell::new(0) };
}
/// How often the extrude window asked the engine for its automatic operation on this thread.
#[cfg(test)]
pub(crate) fn auto_operation_asked() -> usize {
AUTO_OPERATION_ASKED.with(std::cell::Cell::get)
}
/// The one-side direction a new extrude takes for its first pick: toward the side of a sketch
/// profile (or curve) the camera looks from; None for picks that keep their own way.
fn picked_side(app: &SolveApp, d: &Dialog, first: &Sel) -> Option<usize> {
+3
View File
@@ -40,6 +40,9 @@ pub struct Smart {
pub shown: Option<Vec<String>>,
/// Picks off the profile plane become the To Object target (Preferences › Design).
pub auto_to_object: bool,
/// The engine's automatic operation for the last parameters and design: a key over all it
/// depends on, and its answer. It probes the bodies' triangles, too slow to ask every frame.
pub auto: Option<(u64, Option<&'static str>)>,
}
/// One-side direction for a new pick: 0 (along the normal) or 1 (reversed). `ray` is the view
@@ -243,3 +243,24 @@ fn pulling_a_face_down_into_the_body_cuts() {
h.frames(4);
assert_eq!(state(&h).1, CUT, "pushed into the box: Cut");
}
/// The automatic operation probes the bodies' triangles: an open window asks for it once per
/// change of its inputs or the design, not every frame.
#[test]
fn an_open_extrude_asks_for_its_operation_only_when_something_changes() {
let mut h = box_with_top_extrude(false);
let asked = crate::dialogs::auto_operation_asked();
h.frames(20);
assert_eq!(crate::dialogs::auto_operation_asked(), asked, "nothing changed: not asked again");
drag_to(&mut h, 5.0);
assert_eq!(state(&h), (0, CUT, true), "into the box: Cut");
let after_drag = crate::dialogs::auto_operation_asked();
assert!(after_drag > asked, "a new distance is asked about");
h.frames(20);
assert_eq!(crate::dialogs::auto_operation_asked(), after_drag, "held still: not asked again");
// A change to the design (another body, out of the extrude's way) is asked about again.
h.app.run("solid.box", json!({"length":5,"width":5,"height":5,"corner":[50,0,0]})).unwrap();
h.frames(2);
assert!(crate::dialogs::auto_operation_asked() > after_drag, "a changed design is asked about");
assert_eq!(state(&h), (0, CUT, true), "still into the box: Cut");
}