diff --git a/crates/doc/src/eval.rs b/crates/doc/src/eval.rs index fab66f79c..85d7aa1c2 100644 --- a/crates/doc/src/eval.rs +++ b/crates/doc/src/eval.rs @@ -8306,26 +8306,39 @@ impl PlacedBodies { /// Bodies [`state_in_frame_at`] moved into a feature's frame, by source body and matrix: a /// sketch on a face of a turned component is evaluated in that component's frame, and without /// this every edit of it moved (and so meshed again) every body placed differently from it. -/// Held by the [`Model`] across evaluations; each `evaluate` starts a new generation, and an -/// entry neither it nor the evaluation before it used is dropped, so the cache holds only the -/// placements in use. +/// Held by the [`Model`] across evaluations; each `evaluate` that moves a body starts a new +/// generation, and an entry neither it nor the last such evaluation before it used is dropped, +/// so the cache holds only the placements in use. An evaluation that moves nothing (every +/// result reused, or taken from the cache: an undo, the refreshes a command makes after its +/// edit) leaves the generations alone: counting it, two of them in a row dropped every moved +/// body, and the next edit of the sketch moved and meshed them all again. #[derive(Debug, Default)] pub(crate) struct FrameBodies { now: PlacedBodies, before: PlacedBodies, + /// An evaluation began and has not used the cache yet: its first use starts the generation. + pending: bool, } impl FrameBodies { /// At most this many bodies are kept per generation. const CAP: usize = 4096; + /// An evaluation begins; its generation starts when it first moves a body. pub(crate) fn next_generation(&mut self) { - self.before = std::mem::take(&mut self.now); + self.pending = true; + } + + fn begin(&mut self) { + if std::mem::take(&mut self.pending) { + self.before = std::mem::take(&mut self.now); + } } /// The moved body for `b` under `m`, when a call in this or the previous generation moved /// the same body by the same matrix. fn reused(&mut self, b: &ModelBody, m: &crate::Mat) -> Option { + self.begin(); if let Some(placed) = self.now.reused(b, m) { return Some(placed.clone()); } @@ -8340,6 +8353,7 @@ impl FrameBodies { } fn keep(&mut self, b: &ModelBody, m: &crate::Mat, moved: &ModelBody) { + self.begin(); if self.now.by_source.len() < Self::CAP { self.now.by_source.insert(placed_key(b, m), (b.clone(), moved.clone())); } diff --git a/crates/doc/src/eval_cost_tests.rs b/crates/doc/src/eval_cost_tests.rs index c2af067f9..146b57416 100644 --- a/crates/doc/src/eval_cost_tests.rs +++ b/crates/doc/src/eval_cost_tests.rs @@ -226,8 +226,10 @@ fn a_sketch_on_a_face_still_sees_the_placed_bodies() { assert_eq!(work.digested, 0); } -#[test] -fn editing_a_sketch_on_a_turned_occurrence_reuses_its_frame_bodies() { +/// A sketch on a face of a plate placed turned (its own frame, so the plate is moved into it), +/// evaluated once: the design, its model, the plate's component, the sketch, its anchor and the +/// face picked. +fn sketch_on_a_turned_occurrence() -> (Document, Model, u64, u64, FaceAnchor, Plane) { use solidcraft_sketch::{LinkKind, LinkSource}; let mut doc = Document::new("turned face"); @@ -263,6 +265,12 @@ fn editing_a_sketch_on_a_turned_occurrence_reuses_its_frame_bodies() { let first = counted(|| model.evaluate(&doc)); assert!(first.moved > 0, "{first:?}"); assert!(model.result(id).unwrap().error.is_none(), "{:?}", model.result(id).unwrap().error); + (doc, model, comp, id, anchor, picked) +} + +#[test] +fn editing_a_sketch_on_a_turned_occurrence_reuses_its_frame_bodies() { + let (mut doc, model, comp, id, anchor, picked) = sketch_on_a_turned_occurrence(); let reopened = Model::adopt(&doc, &model.saved(&doc).unwrap()).unwrap(); for mut model in [model, reopened] { let before = state_in_face_anchor_frame(&doc, &model.state_before(id), &anchor, 0).unwrap().bodies.first().unwrap().mesh(); @@ -292,6 +300,27 @@ fn editing_a_sketch_on_a_turned_occurrence_reuses_its_frame_bodies() { } } +/// Evaluations between two edits of the sketch that move nothing (a delete refreshes the model +/// more than once, an undo can take its sketch from the cache) leave its frame bodies in place: +/// they once aged them out, and the next edit moved and meshed every placed body again, which +/// in a design of thousands of imported bodies took a minute per edit. +#[test] +fn evaluations_that_move_nothing_keep_the_frame_bodies() { + let (mut doc, mut model, _, id, _, _) = sketch_on_a_turned_occurrence(); + let edit = |doc: &mut Document, to: Vec2| doc.sketch_mut(id).unwrap().points.get_mut(1).unwrap().pos = to; + edit(&mut doc, Vec2::new(4., 5.)); + let work = counted(|| model.evaluate(&doc)); + assert_eq!(work.moved, 0, "{work:?}"); + for _ in 0..3 { + let work = counted(|| model.evaluate(&doc)); + assert_eq!((work.moved, model.last_recomputed), (0, 0), "{work:?}"); + } + edit(&mut doc, Vec2::new(6., 7.)); + let work = counted(|| model.evaluate(&doc)); + assert_eq!(model.last_recomputed, 1); + assert_eq!(work.moved, 0, "the edit after idle evaluations must reuse the moved plate: {work:?}"); +} + #[test] fn an_imported_file_is_fingerprinted_by_its_text() { let doc = placed_imports(2);