Skip to content

SolidCraft: an open-source parametric 3D CAD application built in pure Rust. Desktop, web, and automation through MCP.

History and parameter changes: the run here takes every result computed ahead (#7002)

Kai Evansffbcdc614c58
diff --git a/crates/ui-egui/src/deferred.rs b/crates/ui-egui/src/deferred.rs
index 312df3a80c..26b45d38bb 100644
--- a/crates/ui-egui/src/deferred.rs
+++ b/crates/ui-egui/src/deferred.rs
@@ -33,6 +33,10 @@ pub struct Deferred {
command: (String, Value),
/// The design's revision it was computed for: a design changed meanwhile computes it again.
revision: u64,
+ /// Every result the evaluation ahead computed, whatever the evaluation cache dropped since,
+ /// for the run here (a parameter change evaluates twice: the first evaluation is not the
+ /// one `evaluated` holds).
+ kept: Option<std::sync::Arc<solidcraft_engine::doc::KeptResults>>,
}
impl Deferred {
@@ -53,7 +57,7 @@ enum Start {
/// It computes off the window thread.
Later(Deferred),
/// It runs now, with what was evaluated for it ahead (if anything).
- Now(Option<solidcraft_engine::Evaluated>),
+ Now(Option<solidcraft_engine::Evaluated>, Option<std::sync::Arc<solidcraft_engine::doc::KeptResults>>),
}
/// A delete's check computing off the window thread: each check command's reply, in order.
@@ -127,16 +131,23 @@ impl SolveApp {
self.deferred = Some(job);
Ok(Value::Null)
}
- Start::Now(evaluated) => self.run_evaluated(id, params, evaluated.as_ref()),
+ Start::Now(evaluated, kept) => self.run_evaluated(id, params, evaluated.as_ref(), kept),
}
}
- /// Run a command for real, taking the features `evaluated` computed for it ahead.
- fn run_evaluated(&mut self, id: &str, params: Value, evaluated: Option<&solidcraft_engine::Evaluated>) -> Result<Value, String> {
+ /// Run a command for real, taking the features `evaluated` computed for it ahead, and any
+ /// other result that evaluation `kept`, so the window thread computes none of them again.
+ fn run_evaluated(
+ &mut self,
+ id: &str,
+ params: Value,
+ evaluated: Option<&solidcraft_engine::Evaluated>,
+ kept: Option<std::sync::Arc<solidcraft_engine::doc::KeptResults>>,
+ ) -> Result<Value, String> {
if let Some(evaluated) = evaluated {
self.session.prime(evaluated);
}
- let r = self.run(id, params);
+ let r = self.run_with_kept(kept, |app| app.run(id, params));
self.session.unprime();
r
}
@@ -215,7 +226,7 @@ impl SolveApp {
// The design changed meanwhile: computed again for the design as it is now.
let _ = self.run_heavy(&id, params);
} else if !superseded {
- let _ = self.run_evaluated(&id, params, job.evaluated.flatten().as_ref());
+ let _ = self.run_evaluated(&id, params, job.evaluated.flatten().as_ref(), job.kept);
}
}
}
@@ -238,11 +249,12 @@ impl SolveApp {
/// done within the wait.
#[cfg(not(target_arch = "wasm32"))]
fn begin_deferred(&self, id: &str, params: &Value) -> Start {
- let Some(wait) = self.apply_off_thread else { return Start::Now(None) };
+ let Some(wait) = self.apply_off_thread else { return Start::Now(None, None) };
if self.file_load.busy() || self.file_picker.is_some() {
- return Start::Now(None);
+ return Start::Now(None, None);
}
let ahead = self.session.ahead();
+ let kept = ahead.kept();
let command = (id.to_string(), params.clone());
let commands = vec![command.clone()];
let (tx, rx) = std::sync::mpsc::channel();
@@ -250,22 +262,22 @@ impl SolveApp {
let _ = tx.send(ahead.evaluate(&commands).ok());
});
if spawned.is_err() {
- return Start::Now(None);
+ return Start::Now(None, None);
}
// Zero explicitly requests off-thread application, even if a fast worker
// finishes before this thread is scheduled again. Apply on the next frame.
if !wait.is_zero() {
match rx.recv_timeout(wait) {
- Ok(evaluated) => return Start::Now(evaluated),
- Err(std::sync::mpsc::RecvTimeoutError::Disconnected) => return Start::Now(None),
+ Ok(evaluated) => return Start::Now(evaluated, Some(kept)),
+ Err(std::sync::mpsc::RecvTimeoutError::Disconnected) => return Start::Now(None, Some(kept)),
Err(std::sync::mpsc::RecvTimeoutError::Timeout) => {}
}
}
- Start::Later(Deferred { done: rx, evaluated: None, started: crate::now_ms(), command, revision: self.session.revision })
+ Start::Later(Deferred { done: rx, evaluated: None, started: crate::now_ms(), command, revision: self.session.revision, kept: Some(kept) })
}
#[cfg(target_arch = "wasm32")]
fn begin_deferred(&self, _id: &str, _params: &Value) -> Start {
- Start::Now(None)
+ Start::Now(None, None)
}
}
diff --git a/crates/ui-egui/src/history_off_thread_tests.rs b/crates/ui-egui/src/history_off_thread_tests.rs
index f573ccb0c2..30d2ae041e 100644
--- a/crates/ui-egui/src/history_off_thread_tests.rs
+++ b/crates/ui-egui/src/history_off_thread_tests.rs
@@ -85,6 +85,41 @@ fn a_command_computed_off_the_thread_is_not_evaluated_again() {
assert!((v - (12500.0 - hole)).abs() < 1.0, "{v}");
}
+/// A parameter change computed off the window thread runs here without computing a feature
+/// again, even with the evaluation cache emptied meanwhile (a design whose states pass its
+/// budget): it evaluates once before its sketch stores the new solution and once after, and the
+/// first of those is kept for the run here too, not only the last.
+#[test]
+fn a_parameter_change_computed_off_the_thread_is_not_evaluated_again() {
+ let mut h = Harness::new();
+ h.app.home.open = false;
+ for (id, p) in [
+ ("parameters.add", json!({"name": "W", "expression": "40 mm"})),
+ ("sketch.create", json!({"plane": "XY", "name": "Base"})),
+ ("sketch.rectangle.two_point", json!({"p0": [0, 0], "p1": [40, 20]})),
+ ("sketch.dimension", json!({"entities": ["l1"], "value": "W"})),
+ ("sketch.finish", json!({})),
+ ("solid.extrude", json!({"distance": 10})),
+ ("solid.box", json!({"corner": [5, 5, 10], "length": 10, "width": 10, "height": 5, "operation": "join"})),
+ ] {
+ h.app.run(id, p).unwrap();
+ }
+ h.app.apply_off_thread = Some(Duration::ZERO);
+ h.app.run_heavy("parameters.change", json!({"name": "W", "expression": "50 mm"})).unwrap();
+ for _ in 0..500 {
+ if h.app.deferred.as_mut().is_some_and(crate::deferred::Deferred::finished) {
+ break;
+ }
+ std::thread::sleep(Duration::from_millis(10));
+ }
+ h.app.session.model.cache = Default::default();
+ let before = h.app.session.model.recomputed;
+ until_settled(&mut h);
+ assert_eq!(h.app.session.model.recomputed - before, 0, "nothing computed again on the window thread, in either evaluation");
+ let v = h.app.run("inspect.measure", json!({})).unwrap()["total"]["volume_mm3"].as_f64().unwrap();
+ assert!((v - (50.0 * 20.0 * 10.0 + 500.0)).abs() < 1.0, "{v}");
+}
+
#[test]
fn a_marker_moved_again_replaces_the_move_still_waiting() {
let mut h = block_boss_hole();