Redefine: the features after the edited one are computed once (#7076)

timeline.redefine evaluated every later feature in its trial session, then again in the session:
the evaluation cache keeps only the last few states of a design with large bodies, so the trial's
results were gone by then. The trial keeps them in the session's kept results (or its own), and
the session's evaluation takes them from there. EvalCache::with_budget gives tests a small cache.
This commit is contained in:
Kai Evans
2026-10-27 12:35:32 +00:00
committed by GitHub
parent 22ede1bde3
commit 17339cf65c
4 changed files with 32 additions and 1 deletions
+6
View File
@@ -1047,6 +1047,12 @@ const CACHE_ENTRIES: usize = 4096;
const CACHE_BYTES: usize = 64 << 20;
impl EvalCache {
/// A cache that keeps `bytes` of results (always at least the last one): a small budget
/// stands in for a design whose states are large.
pub fn with_budget(bytes: usize) -> Self {
EvalCache { budget: bytes, ..EvalCache::default() }
}
fn get(&mut self, key: u64) -> Option<CachedEval> {
let r = self.map.get(&key).cloned();
if r.is_some() {
+1 -1
View File
@@ -73,7 +73,6 @@ pub use assembly::{
mat_mul, rigid,
};
pub use document::*;
pub use eval::KeptResults;
pub use eval::{
EdgeRef, FeatureResult, Model, ModelBody, ModelState, PlacedBodies, RevolveExtent, SavedModel, SolvedSketch, ThreadInfo, curve_pick_error,
edge_names_for, edge_refs, face_point_names, face_point_state, hole_direction_at, import_body_names, map_wire, metric_thread_sizes,
@@ -81,6 +80,7 @@ pub use eval::{
sketch_line_plane, state_in_frame, state_in_frame_at, suggested_thread, tangent_face_chain, through_all_distance, world_state,
world_state_reusing,
};
pub use eval::{EvalCache, KeptResults};
pub use expr::Expr;
pub use names::{BodyName, FeatureId, FeatureName, SketchId, copy_name};
pub use params::{ParamRow, ParamUser, cycles};
+7
View File
@@ -1440,6 +1440,11 @@ fn redefine(s: &mut Session, p: &Value) -> Result<Value> {
// Run the constructor on a scratch session: failed edits must not disturb sketch mode,
// selections, component authoring state or the original feature's definition.
let mut trial = s.scratch();
// What the trial computes is kept for the session's own evaluation at the end: the cache may
// hold only the last few states of a large design, which would be computed twice.
let kept = s.model.kept();
let shared = kept.clone().unwrap_or_default();
trial.model.keep(Some(shared.clone()));
trial.active_component = old.component;
if let FeatureKind::Emboss { face: Some(face), .. } = &old.kind {
trial.active_occurrence_path = (!face.author_path.is_empty()).then(|| face.author_path.clone());
@@ -1549,7 +1554,9 @@ fn redefine(s: &mut Session, p: &Value) -> Result<Value> {
}
s.pending_moves = trial.pending_moves;
s.doc = trial.doc;
s.model.keep(Some(shared));
s.refresh();
s.model.keep(kept);
if let Some(e) = s.model.result(id).and_then(|r| r.error.clone()) {
return Err(EngineError::Other(e));
}
+18
View File
@@ -144,6 +144,24 @@ fn redefining_a_blend_replaces_persistent_edge_selection_and_survives_resize() {
assert!(s.model.result(old.id).unwrap().warning.is_none());
}
/// A redefine computes the features after the edited one once: its trial's results are kept
/// for the session's own evaluation, though the cache holds only the last state (a large
/// design's).
#[test]
fn redefining_computes_the_later_features_once() {
let mut s = Session::default();
run(&mut s, "solid.box", json!({"length": 20, "width": 10, "height": 5, "name": "Base"}));
for x in [30, 40, 50] {
run(&mut s, "solid.box", json!({"length": 5, "width": 5, "height": 5, "corner": [x, 0, 0]}));
}
*s.model.cache.lock().unwrap() = solidcraft_doc::EvalCache::with_budget(1);
let reply =
run(&mut s, "timeline.redefine", json!({"feature": "Base", "command": "solid.box", "params": {"length": 25, "width": 10, "height": 5}}));
assert_eq!(reply["recomputed"], 0, "{reply}");
let volume = run(&mut s, "inspect.measure", json!({}))["total"]["volume_mm3"].as_f64().unwrap();
assert!((volume - (25.0 * 10.0 * 5.0 + 3.0 * 125.0)).abs() < 1e-6, "{volume}");
}
#[test]
fn redefining_preserves_body_names_participants_and_marker_without_temporary_features() {
let mut s = Session::default();