From 74eda80b5d2c0c0df7a57f1e821a32af49d43b18 Mon Sep 17 00:00:00 2001 From: kaltenbrunnermarkus <153593707+kaltenbrunnermarkus@users.noreply.github.com> Date: Sun, 1 Nov 2026 09:43:06 +0000 Subject: [PATCH] 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 --------- Co-authored-by: Markus Kaltenbrunner Co-authored-by: Claude Opus 5.5 --- crates/ui-egui/src/dialogs.rs | 49 +++++++++++++++++++- crates/ui-egui/src/smart_extrude.rs | 3 ++ crates/ui-egui/src/smart_extrude_ui_tests.rs | 21 +++++++++ 3 files changed, 72 insertions(+), 1 deletion(-) diff --git a/crates/ui-egui/src/dialogs.rs b/crates/ui-egui/src/dialogs.rs index a261d14c2..0cf27ab74 100644 --- a/crates/ui-egui/src/dialogs.rs +++ b/crates/ui-egui/src/dialogs.rs @@ -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, ¶ms) }; + 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, ¶ms); + 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, ¶ms); + 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 = 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 { diff --git a/crates/ui-egui/src/smart_extrude.rs b/crates/ui-egui/src/smart_extrude.rs index 677142f45..ec8989d77 100644 --- a/crates/ui-egui/src/smart_extrude.rs +++ b/crates/ui-egui/src/smart_extrude.rs @@ -40,6 +40,9 @@ pub struct Smart { pub shown: Option>, /// 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 diff --git a/crates/ui-egui/src/smart_extrude_ui_tests.rs b/crates/ui-egui/src/smart_extrude_ui_tests.rs index aa15dbb94..cac58c391 100644 --- a/crates/ui-egui/src/smart_extrude_ui_tests.rs +++ b/crates/ui-egui/src/smart_extrude_ui_tests.rs @@ -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"); +}