Evaluation cache: charge each state only for the bodies it made, so setting a parameter back restores (#6897)

The evaluation cache's 64 MB budget charged every cached feature result the whole model, though a
state shares every body it did not change with the state before it. A design whose model is a few
MB kept only its last few dozen results, so setting a parameter back recomputed what had dropped
out. Each result is now charged for the bodies and sketches it does not share with the state it
started from.

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
SS-ENandClaude Opus 5.5 authored and GitHub committed 2026-10-26 11:14:57 +00:00
1 parent bd38bc40be
commit daecea6edf
2 files changed
+56 -2

No files matched your search

+22 -2
View File
@@ -956,6 +956,8 @@ pub struct EvalCache {
order: std::collections::VecDeque<u64>,
/// What the stored results cost together.
bytes: usize,
/// The byte budget (0: [`CACHE_BYTES`]).
pub(crate) budget: usize,
pub hits: usize,
}
@@ -997,7 +999,8 @@ impl EvalCache {
self.bytes = self.bytes.saturating_add(bytes);
// One result is always kept, so a single state larger than the budget cannot empty the
// cache and force a full recompute on every undo.
while self.order.len() > CACHE_ENTRIES || (self.bytes > CACHE_BYTES && self.order.len() > 1) {
let budget = if self.budget == 0 { CACHE_BYTES } else { self.budget };
while self.order.len() > CACHE_ENTRIES || (self.bytes > budget && self.order.len() > 1) {
if let Some(k) = self.order.pop_front()
&& let Some(old) = self.map.remove(&k)
{
@@ -1099,6 +1102,22 @@ impl ModelState {
bodies + sketches + self.sheets.len() * 4096 + self.threads.len() * 192 + self.construct.len() * 192
}
/// [`ModelState::retained_bytes`] of what this state holds that `before` does not: bodies
/// and sketches it shares with `before` are already paid for by the cached state that made
/// them. Charging every cached state its whole model let a design whose model is large next
/// to the budget keep only its last few states, so setting a parameter back recomputed what
/// had dropped out.
fn added_bytes(&self, before: &ModelState) -> usize {
let bodies: usize = self
.bodies
.iter()
.filter(|b| !before.bodies.iter().any(|o| o.body.same_shape(&b.body)))
.map(|b| if b.bytes > 0 { b.bytes } else { b.body.retained_bytes() })
.sum();
let sketches: usize = self.sketches.iter().filter(|s| !before.sketches.iter().any(|o| Arc::ptr_eq(o, s))).map(|s| s.retained_bytes()).sum();
bodies + sketches + self.sheets.len() * 4096 + self.threads.len() * 192 + self.construct.len() * 192
}
pub fn body(&self, name: &str) -> Option<&ModelBody> {
self.bodies.iter().find(|b| b.name == name)
}
@@ -2084,6 +2103,7 @@ impl Model {
continue;
}
recomputed += 1;
let before = state.clone();
let t0 = now();
let mut next = (*state).clone();
// The names the design must not use again travel with the state (#4957, #4302).
@@ -2173,7 +2193,7 @@ impl Model {
warning: warning.clone(),
boolean_diagnostics: boolean_diagnostics.clone(),
ms,
bytes: state.retained_bytes(),
bytes: state.added_bytes(&before),
state: state.clone(),
};
if let Some(k) = &self.kept.0 {
+34
View File
@@ -103,6 +103,40 @@ fn set_back_reports_restored_features() {
assert_eq!((m.last_recomputed, m.last_restored), (0, 0));
}
/// Setting a parameter back restores every feature on a design whose states are large next to
/// the cache's budget: each cached state shares the bodies it did not change with the states
/// before it, so it is charged only for what it made, not for the whole model again.
#[test]
fn set_back_restores_everything_when_states_share_bodies() {
let mut doc = Document::new("blocks");
doc.set_param("h", "10 mm", Some("mm"), None).unwrap();
for i in 0..30 {
let height = if i == 0 { "h" } else { "10" };
doc.add_feature(
FeatureKind::Box {
corner: point_expr(Vec3::new(20.0 * i as f64, 0.0, 0.0)),
length: "10".into(),
width: "10".into(),
height: height.into(),
operation: Operation::NewBody,
},
None,
)
.unwrap();
}
let mut m = Model::new();
m.evaluate(&doc);
// Room for a few whole models: enough for both timelines' new bodies, not for every state
// charged as a whole model.
m.cache.lock().unwrap().budget = 3 * m.state().retained_bytes();
doc.set_param("h", "12 mm", None, None).unwrap();
m.evaluate(&doc);
assert_eq!(m.last_recomputed, 30);
doc.set_param("h", "10 mm", None, None).unwrap();
m.evaluate(&doc);
assert_eq!((m.last_recomputed, m.last_restored), (0, 30));
}
/// #1989: what an evaluation ahead on a copy of the model computed is kept for the run that
/// follows on the model itself, which finds it there even once the evaluation cache has dropped
/// it (the cache keeps only a few states of a design with large bodies). Copies keep nothing.