diff --git a/apps/solidcraft-cli/src/main.rs b/apps/solidcraft-cli/src/main.rs index 9b4b06e90..312e1fce7 100644 --- a/apps/solidcraft-cli/src/main.rs +++ b/apps/solidcraft-cli/src/main.rs @@ -723,9 +723,10 @@ pub(crate) fn cmd_bench_open(args: &[String]) -> Result<(), String> { let t = std::time::Instant::now(); let s = Session::opened(doc, saved.as_ref()); drop(read); - // Taken from the file: nothing evaluated (SOLIDCRAFT_NO_MODEL_CACHE evaluates anyway). + // Taken from the file: nothing evaluated (SOLIDCRAFT_NO_MODEL_CACHE evaluates anyway), or + // only the features whose state a thinned file left out. let adopted = saved.is_some() && s.model.last_recomputed == 0; - println!("{}", json!({"step": "evaluate", "ms": t.elapsed().as_secs_f64() * 1000.0, "adopted": adopted})); + println!("{}", json!({"step": "evaluate", "ms": t.elapsed().as_secs_f64() * 1000.0, "adopted": adopted, "recomputed": s.model.last_recomputed})); for r in &s.model.results { println!("{}", json!({"step": "feature", "id": r.id, "name": r.name, "ms": r.ms, "error": r.error})); } diff --git a/apps/solidcraft-cli/tests/model_cache.rs b/apps/solidcraft-cli/tests/model_cache.rs index 6d4bab4eb..42a91adae 100644 --- a/apps/solidcraft-cli/tests/model_cache.rs +++ b/apps/solidcraft-cli/tests/model_cache.rs @@ -7,7 +7,7 @@ use std::sync::Arc; use serde_json::{Value, json}; use solidcraft_engine::Session; -use solidcraft_engine::doc::{ModelBody, ModelState}; +use solidcraft_engine::doc::{ModelBody, ModelState, SavedModel}; fn corpus() -> Vec { let dir = Path::new(env!("CARGO_MANIFEST_DIR")).join("tests/corpus"); @@ -149,6 +149,87 @@ fn imported_assemblies_open_from_their_model_as_evaluated() { check("imported hinge", s); } +/// `built` saved with `saved`, a model of it that keeps only some states (as a file without room +/// for all of them does, #3220), opens as the design evaluated again: evaluating just the +/// features whose state the file left out, then after suppressing a feature at, before or after +/// each state it kept, after undoing that, and with the marker moved there and back. +fn check_thinned(name: &str, built: &Session, saved: &SavedModel) { + let design = built.design(); + let bytes = + solidcraft_engine::io::write_design_container_with_cache(&design, solidcraft_engine::io::DesignCompression::Fast, None, Some(saved)).unwrap(); + assert!(solidcraft_engine::io::read_design_geometry(&bytes).is_some(), "{name}: the file keeps the final state"); + let path = temp(&name.replace(' ', "-")); + std::fs::write(&path, &bytes).unwrap(); + let mut cached = open(&path); + let mut rebuilt = Session::new(solidcraft_engine::io::read_design(&bytes).unwrap()); + let features = saved.records["features"].as_array().unwrap(); + let unkept = features.iter().filter(|f| f["skipped"] == false && f.get("state").is_none()).count(); + assert!(unkept > 0, "{name}: some states left out"); + assert_eq!(cached.model.recomputed, unkept, "{name}: evaluated just the features whose state the file left out"); + assert_eq!(model(name, &cached), model(name, &rebuilt), "{name}: opened from the file's model"); + let n = cached.doc.features.len(); + let mut at: Vec = features + .iter() + .enumerate() + .filter(|(_, f)| f["skipped"] == false && f.get("state").is_some()) + .flat_map(|(k, _)| [k.saturating_sub(1), k, k + 1]) + .filter(|i| *i < n) + .collect(); + at.dedup(); + for i in at { + let edit = json!({"feature": cached.doc.features[i].id, "suppressed": true}); + let (a, b) = (cached.execute("timeline.suppress", &edit), rebuilt.execute("timeline.suppress", &edit)); + assert_eq!(a.is_ok(), b.is_ok(), "{name}: suppressing feature {i}"); + assert_eq!(model(name, &cached), model(name, &rebuilt), "{name}: feature {i} suppressed"); + if a.is_ok() { + cached.execute("edit.undo", &json!({})).unwrap(); + rebuilt.execute("edit.undo", &json!({})).unwrap(); + assert_eq!(model(name, &cached), model(name, &rebuilt), "{name}: suppressing feature {i} undone"); + } + for marker in [json!({"position": i}), json!({})] { + cached.execute("timeline.rollback", &marker).unwrap(); + rebuilt.execute("timeline.rollback", &marker).unwrap(); + assert_eq!(model(name, &cached), model(name, &rebuilt), "{name}: the marker at {marker}, from feature {i}"); + } + } + let _ = std::fs::remove_file(&path); +} + +/// A model too big for its file keeps the final state, then the slowest features' states as fit +/// (#3220). Both the smallest such model (the final state, and states that add no body to it) +/// and one that keeps more open as the design evaluated again. The placed components' design has +/// a cut whose fingerprint reads where the bodies before it lie, which a state left out does not +/// say until it is evaluated. +#[test] +fn thinned_models_open_as_evaluated() { + let corpus = Path::new(env!("CARGO_MANIFEST_DIR")).join("tests/corpus/components-arm.solidcraft"); + for sample in ["Sample Plate", "Plastic Enclosure", "components-arm"] { + let built = match solidcraft_engine::sample::samples().into_iter().find(|s| s.name == sample) { + Some(s) => { + let mut built = Session::default(); + for c in (s.script)()["commands"].as_array().unwrap() { + built.execute(c["command"].as_str().unwrap(), &c["params"]).unwrap(); + } + built + } + None => open(&corpus), + }; + let saved = built.model.saved(&built.design()).unwrap(); + // Every body a megabyte (the records fit in the room left over): each budget keeps one more + // body's worth of states. + let body = 1 << 20; + let pieces: Vec> = (0..saved.bodies.len()).map(|i| vec![(i, body)]).collect(); + let thinned: Vec = + (0..=saved.bodies.len()).filter_map(|n| saved.thinned(&pieces, n * body + body - 1).map(|(m, _, _)| m)).collect(); + let kept = |m: &SavedModel| m.records["features"].as_array().unwrap().iter().filter(|f| f.get("state").is_some()).count(); + let smallest = thinned.first().unwrap(); + assert!(smallest.records["states"].as_array().unwrap().len() < saved.records["states"].as_array().unwrap().len(), "{sample}"); + let more = thinned.iter().find(|m| kept(m) > kept(smallest) && kept(m) < built.doc.features.len()).unwrap(); + check_thinned(&format!("{sample} smallest"), &built, smallest); + check_thinned(&format!("{sample} thinned"), &built, more); + } +} + /// Opened from its model, a design evaluates only from the first feature an edit changes: the /// saved states before it are kept as they were read (the same states, not evaluated again). #[test] diff --git a/crates/doc/src/eval.rs b/crates/doc/src/eval.rs index d8a982a67..f08180adf 100644 --- a/crates/doc/src/eval.rs +++ b/crates/doc/src/eval.rs @@ -1023,6 +1023,11 @@ pub struct FeatureResult { pub stale: bool, #[serde(skip)] fingerprint: u64, + /// Taken from a saved model that did not keep this feature's state (#3220): `state` only + /// stands in for it until [`Model::evaluate`] evaluates the feature again, which + /// [`Model::adopt`] does before the model is used. + #[serde(skip)] + unkept: bool, #[serde(skip)] pub state: Arc, } @@ -1792,11 +1797,13 @@ impl Model { let cancelled = watched.step(i); let fp = chain_fingerprint(doc, &vals, f, prev_fp, rolled_back, &self.step_digests, Some(&state)); prev_fp = fp; + let current = |old: &&FeatureResult| old.id == f.id && old.fingerprint == fp && !old.stale; + // A state the saved model did not keep is evaluated again from the one before it; the + // results after it that the model did keep still stand (#3220). + let unkept = reuse && self.results.get(i).filter(current).is_some_and(|old| old.unkept); if reuse - && let Some(old) = self.results.get(i) - && old.id == f.id - && old.fingerprint == fp - && !old.stale + && !unkept + && let Some(old) = self.results.get(i).filter(current) { if !old.skipped { state = old.state.clone(); @@ -1804,7 +1811,7 @@ impl Model { out.push(old.clone()); continue; } - reuse = false; + reuse = unkept && !cancelled; if f.suppressed || rolled_back || cancelled { out.push(FeatureResult { id: f.id, @@ -1816,6 +1823,7 @@ impl Model { skipped: true, stale: false, fingerprint: fp, + unkept: false, state: state.clone(), }); continue; @@ -1833,6 +1841,8 @@ impl Model { if c.error.is_none() { state = c.state.clone(); } + // A kept result after a failed one would not be this evaluation's. + reuse &= c.error.is_none(); out.push(FeatureResult { id: f.id, name: f.name.clone(), @@ -1843,6 +1853,7 @@ impl Model { skipped: false, stale: false, fingerprint: fp, + unkept: false, state: state.clone(), }); continue; @@ -1853,7 +1864,8 @@ impl Model { if r.error.is_none() { state = r.state.clone(); } - out.push(FeatureResult { state: state.clone(), ..r.clone() }); + reuse &= r.error.is_none(); + out.push(FeatureResult { state: state.clone(), unkept: false, ..r.clone() }); continue; } recomputed += 1; @@ -1985,6 +1997,7 @@ impl Model { m.put(key, e); } } + reuse &= error.is_none(); out.push(FeatureResult { id: f.id, name: f.name.clone(), @@ -1995,6 +2008,7 @@ impl Model { skipped: false, stale: false, fingerprint: fp, + unkept: false, state: state.clone(), }); } diff --git a/crates/doc/src/model_cache.rs b/crates/doc/src/model_cache.rs index 2a9639bb3..b5062aa50 100644 --- a/crates/doc/src/model_cache.rs +++ b/crates/doc/src/model_cache.rs @@ -6,7 +6,8 @@ //! hold (one that later features pass through unchanged) is recorded once, as are its face names //! and the solved sketches the states share. Face names are kept as worked out, not as the //! history they were worked out from: a model taken from a file names every face as the model -//! that was saved did. +//! that was saved did. A file without room for every state keeps fewer +//! ([`SavedModel::thinned`]); opening evaluates the features whose state it left out again. use super::*; use crate::history_names::{FaceInfo, Names}; @@ -359,12 +360,163 @@ impl SavedModel { None => Ok(Arc::new(ModelState::default())), } } + + /// This model with fewer states, for a design file without room for all of them (#3220): the + /// final state (what the design shows, kept whatever it costs), then the others, the slowest + /// feature's first (the ones opening would take longest to evaluate again; of equal ones the + /// later first), each while what it adds still fits in `budget` bytes: the bodies no state kept + /// so far holds, and the records of the state, those bodies, their names and sketches. + /// `pieces[i]` is what keeping body `i` stores, as ids and bytes: a piece that several bodies + /// or states need counts once. A feature whose state is left out keeps none, and opening + /// evaluates it again ([`Model::adopt`]). With the model, the index here of each body it keeps, + /// and the bytes it counted: over `budget` when the final state alone is. `None` only for + /// records it cannot read. + pub fn thinned(&self, pieces: &[Vec<(usize, usize)>], budget: usize) -> Option<(SavedModel, Vec, usize)> { + let list = |key: &str| self.records.get(key).and_then(Json::as_array); + let (features, states, model_bodies) = (list("features")?, list("states")?, list("model_bodies")?); + let (names, sketches) = (list("names")?, list("sketches")?); + let at = |v: Option<&Json>, len: usize| v.and_then(Json::as_u64).and_then(|i| usize::try_from(i).ok()).filter(|i| *i < len); + // The indices a state's runs list. + let held = |s: &Json, key: &str, len: usize| -> Option> { + let mut out = Vec::new(); + for run in s.get(key)?.as_array()? { + let first = at(run.get(0), len)?; + out.extend(first..first.checked_add(at(run.get(1), len.saturating_add(1))?).filter(|end| *end <= len)?); + } + Some(out) + }; + let state_bodies = states.iter().map(|s| held(s, "bodies", model_bodies.len())).collect::>>()?; + let state_sketches = states.iter().map(|s| held(s, "sketches", sketches.len())).collect::>>()?; + let body_of = model_bodies.iter().map(|r| at(r.get("body"), self.bodies.len())).collect::>>()?; + let names_of = model_bodies.iter().map(|r| at(r.get("names"), names.len())).collect::>>()?; + // What each record takes in the file. + let bytes = |list: &[Json]| list.iter().map(|r| serde_json::to_vec(r).map_or(usize::MAX, |b| b.len().saturating_add(1))).collect::>(); + let (state_bytes, body_bytes) = (bytes(states.as_slice()), bytes(model_bodies.as_slice())); + let (name_bytes, sketch_bytes) = (bytes(names.as_slice()), bytes(sketches.as_slice())); + // What each state's feature took to evaluate, and the final state. + let mut ms = vec![0.0f64; states.len()]; + let mut last = None; + for f in features.iter().filter(|f| f.get("skipped").and_then(Json::as_bool) == Some(false)) { + let s = at(f.get("state"), states.len())?; + if let Some(t) = ms.get_mut(s) { + *t += f.get("ms").and_then(Json::as_f64).filter(|t| t.is_finite() && *t > 0.0).unwrap_or(0.0); + } + last = Some(s); + } + let last = last?; + let time = |s: &usize| ms.get(*s).copied().unwrap_or(0.0); + let mut order: Vec = (0..states.len()).filter(|s| *s != last).collect(); + order.sort_by(|a, b| time(b).total_cmp(&time(a)).then(b.cmp(a))); + let mut keep = vec![false; states.len()]; + // The records and pieces kept so far, and their bytes. + let mut kept_bodies = vec![false; model_bodies.len()]; + let (mut names_used, mut sketches_used) = (vec![false; names.len()], vec![false; sketches.len()]); + let mut stored = std::collections::HashSet::new(); + let mut used = 0usize; + for s in std::iter::once(last).chain(order) { + let (mut adds, mut new_names, mut new_sketches) = (HashMap::new(), std::collections::HashSet::new(), std::collections::HashSet::new()); + let mut records = *state_bytes.get(s)?; + for &m in state_bodies.get(s)? { + if kept_bodies.get(m) == Some(&false) { + records = records.saturating_add(*body_bytes.get(m)?); + let n = *names_of.get(m)?; + if names_used.get(n) == Some(&false) && new_names.insert(n) { + records = records.saturating_add(*name_bytes.get(n)?); + } + for &(piece, bytes) in pieces.get(*body_of.get(m)?)? { + if !stored.contains(&piece) { + adds.insert(piece, bytes); + } + } + } + } + for &k in state_sketches.get(s)? { + if sketches_used.get(k) == Some(&false) && new_sketches.insert(k) { + records = records.saturating_add(*sketch_bytes.get(k)?); + } + } + let total = adds.values().fold(used.saturating_add(records), |t, b| t.saturating_add(*b)); + if total > budget && s != last { + continue; + } + used = total; + stored.extend(adds.into_keys()); + for &m in state_bodies.get(s)? { + *kept_bodies.get_mut(m)? = true; + } + for n in new_names { + *names_used.get_mut(n)? = true; + } + for k in new_sketches { + *sketches_used.get_mut(k)? = true; + } + *keep.get_mut(s)? = true; + } + // What the states kept refer to, numbered again in the order it had. + let mut kernel_used = vec![false; self.bodies.len()]; + for m in (0..model_bodies.len()).filter(|m| kept_bodies.get(*m) == Some(&true)) { + *kernel_used.get_mut(*body_of.get(m)?)? = true; + } + let renumber = |used: &[bool]| -> Vec> { + let mut next = 0; + used.iter() + .map(|&u| { + u.then(|| { + next += 1; + next - 1 + }) + }) + .collect() + }; + let (new_body, new_kernel, new_names) = (renumber(&kept_bodies), renumber(&kernel_used), renumber(&names_used)); + let (new_sketch, new_state) = (renumber(&sketches_used), renumber(&keep)); + let new = |map: &[Option], i: usize| map.get(i).copied().flatten(); + let kept_of = |list: &[Json], used: &[bool]| list.iter().zip(used).filter(|(_, u)| **u).map(|(v, _)| v.clone()).collect::>(); + let mut out_states = Vec::new(); + for (s, st) in states.iter().enumerate().filter(|(s, _)| keep.get(*s) == Some(&true)) { + let bodies = state_bodies.get(s)?.iter().map(|m| new(&new_body, *m)).collect::>>()?; + let sketches = state_sketches.get(s)?.iter().map(|i| new(&new_sketch, *i)).collect::>>()?; + let mut st = st.clone(); + let o = st.as_object_mut()?; + o.insert("bodies".into(), json!(runs(&bodies))); + o.insert("sketches".into(), json!(runs(&sketches))); + out_states.push(st); + } + let mut out_bodies = Vec::new(); + for (m, r) in model_bodies.iter().enumerate().filter(|(m, _)| kept_bodies.get(*m) == Some(&true)) { + let (body, named) = (new(&new_kernel, *body_of.get(m)?)?, new(&new_names, at(r.get("names"), names.len())?)?); + let mut r = r.clone(); + let o = r.as_object_mut()?; + o.insert("body".into(), json!(body)); + o.insert("names".into(), json!(named)); + out_bodies.push(r); + } + let mut out_features = Vec::with_capacity(features.len()); + for f in features { + let state = new(&new_state, at(f.get("state"), states.len())?); + let mut f = f.clone(); + let o = f.as_object_mut()?; + match state { + Some(s) => o.insert("state".into(), json!(s)), + None => o.remove("state"), + }; + out_features.push(f); + } + let records = json!({ + "features": out_features, "states": out_states, "model_bodies": out_bodies, "names": kept_of(names.as_slice(), &names_used), + "sketches": kept_of(sketches.as_slice(), &sketches_used), + }); + let kept: Vec = (0..self.bodies.len()).filter(|i| kernel_used.get(*i) == Some(&true)).collect(); + let bodies = kept.iter().filter_map(|i| self.bodies.get(*i).cloned()).collect(); + Some((SavedModel { records, bodies }, kept, used)) + } } impl Model { /// The model a design file kept for `doc` ([`Model::saved`]), taken as `doc`'s evaluation: - /// every feature's result as saved, nothing evaluated. Refused (an error) unless every - /// feature's fingerprint is the one `doc` gives it now, and every record is well formed. + /// every feature's result as saved, nothing evaluated but the features whose state the file + /// did not keep ([`SavedModel::thinned`]). Refused (an error) unless every feature's + /// fingerprint is the one `doc` gives it now, and every record is well formed. pub fn adopt(doc: &Document, saved: &SavedModel) -> Result { let states = saved.states()?; let records = &saved.records; @@ -381,16 +533,24 @@ impl Model { let step_digests = StepDigests::of(doc, &StepDigests::default()); let marker = doc.marker.unwrap_or(usize::MAX); let ctx = context_digest(doc, &vals); - let mut cache = EvalCache::default(); let mut before = Arc::new(ModelState::default()); + // Whether `before` is the state before the feature: one the file left out only stands in. + let mut exact = true; let mut prev = 0; let mut results = Vec::with_capacity(features.len()); for (i, (f, r)) in doc.features.iter().zip(features).enumerate() { - prev = chain_fingerprint(doc, &vals, f, prev, i >= marker, &step_digests, Some(&before)); - if r.get("id").and_then(Json::as_u64) != Some(f.id) || unhex(r.get("fingerprint").unwrap_or(&Json::Null))? != prev { + let saved = unhex(r.get("fingerprint").unwrap_or(&Json::Null))?; + // A fingerprint reads the state before its feature (where the bodies it takes lie). + // After a state the file left out, the saved one is taken: `evaluate` works that state + // out, checks the fingerprints after it, and evaluates again from one that differs. + prev = if exact { chain_fingerprint(doc, &vals, f, prev, i >= marker, &step_digests, Some(&before)) } else { saved }; + if r.get("id").and_then(Json::as_u64) != Some(f.id) || saved != prev { return Err(bad("saved model: the document changed")); } let ms = r.get("ms").and_then(Json::as_f64).filter(|ms| ms.is_finite()).ok_or_else(|| bad("saved model: time"))?; + // A file without room for every state keeps some features without one (#3220). + let state = + r.get("state").map(|s| states.get(index(Some(s), states.len())?).cloned().ok_or_else(|| bad("saved model: state"))).transpose()?; let result = FeatureResult { id: f.id, name: f.name.clone(), @@ -404,27 +564,48 @@ impl Model { skipped: r.get("skipped").and_then(Json::as_bool).ok_or_else(|| bad("saved model: skipped"))?, stale: false, fingerprint: prev, - state: states.get(index(r.get("state"), states.len())?).cloned().ok_or_else(|| bad("saved model: state"))?, + unkept: state.is_none(), + state: state.unwrap_or_else(|| before.clone()), }; - // Each result where an evaluation would have put it, so an edit undone (or set back) - // finds it again rather than evaluating the feature anew. if !result.skipped { - let local = fingerprint(0, f, &vals, false, &step_digests) ^ placement_digest(doc, f, Some(&before)); - cache.put( - cache_key(f, prev, local, ctx, &before), - CachedEval { - error: None, - warning: result.warning.clone(), - boolean_diagnostics: result.boolean_diagnostics.clone(), - ms, - bytes: result.state.retained_bytes(), - state: result.state.clone(), - }, - ); + exact = !result.unkept; before = result.state.clone(); } results.push(result); } - Ok(Model { results, param_errors, step_digests, cache: Arc::new(std::sync::Mutex::new(cache)), ..Model::default() }) + let mut model = Model { results, param_errors, step_digests, ..Model::default() }; + let unkept = model.results.iter().filter(|r| r.unkept).count(); + if unkept > 0 { + // The features whose state the file did not keep, each evaluated again from the state + // before it: nothing else in the model is missing then. + model.evaluate(doc); + log::info!( + "the saved model kept the states of {} of {} features: {} evaluated again", + model.results.len().saturating_sub(unkept), + model.results.len(), + model.last_recomputed + ); + } + // Each result where an evaluation would have put it, so an edit undone (or set back) + // finds it again rather than evaluating the feature anew. + let mut before = Arc::new(ModelState::default()); + if let Ok(mut cache) = model.cache.lock() { + for (f, r) in doc.features.iter().zip(&model.results).filter(|(_, r)| !r.skipped && r.error.is_none()) { + let local = fingerprint(0, f, &vals, false, &model.step_digests) ^ placement_digest(doc, f, Some(&before)); + cache.put( + cache_key(f, r.fingerprint, local, ctx, &before), + CachedEval { + error: None, + warning: r.warning.clone(), + boolean_diagnostics: r.boolean_diagnostics.clone(), + ms: r.ms, + bytes: r.state.retained_bytes(), + state: r.state.clone(), + }, + ); + before = r.state.clone(); + } + } + Ok(model) } } diff --git a/crates/io/src/design.rs b/crates/io/src/design.rs index 139c637bf..c0e7847b9 100644 --- a/crates/io/src/design.rs +++ b/crates/io/src/design.rs @@ -92,12 +92,17 @@ impl PackageWriter { /// Would `entries` more optional parts of `extra` bytes, stored uncompressed, still keep /// the file within `budget` and its expanded size within the reader's budget? fn room(&self, entries: usize, extra: usize, budget: usize) -> bool { + self.names.len().saturating_add(entries) <= MAX_ENTRIES && extra <= self.space(entries, budget) + } + /// The bytes `entries` more optional parts may hold together ([`PackageWriter::room`]). + fn space(&self, entries: usize, budget: usize) -> usize { + if self.names.len().saturating_add(entries) > MAX_ENTRIES { + return 0; + } // The bytes written so far, plus what the open compressed part may still hold back. let written = self.zip.get_ref().map_or(usize::MAX, |c| c.get_ref().len()).saturating_add(1 << 20); let directory = self.names.len().saturating_add(entries).saturating_mul(256).saturating_add(65_536); - self.names.len().saturating_add(entries) <= MAX_ENTRIES - && written.saturating_add(extra).saturating_add(directory) <= budget - && self.expanded.saturating_add(extra) <= MAX_EXPANDED + budget.saturating_sub(written.saturating_add(directory)).min(MAX_EXPANDED.saturating_sub(self.expanded)) } fn blob(&mut self, bytes: &[u8], extension: &str, compressed: bool) -> Result { self.resolved = @@ -269,15 +274,9 @@ fn write_container( let metadata = serde_json::to_vec(&metadata).map_err(invalid)?; writer.part("document.json", &metadata, true)?; writer.part("container.json", &serde_json::to_vec(&json!({"format": VERSION})).map_err(invalid)?, false)?; - // The model cache is optional: one that does not fit the container is left out, never a - // failed save. - let cache = geometry.and_then(|g| native_cache::encode_model(g, &metadata).ok()).filter(|(manifest, data)| { - let fits = writer.room(2, manifest.len().saturating_add(data.len()), budget); - if !fits { - log::info!("the model cache ({} MB) does not fit the design file: saved without it", (manifest.len() + data.len()) >> 20); - } - fits - }); + // The model cache is optional: one that does not fit the container keeps fewer states, or is + // left out, never a failed save. + let cache = geometry.and_then(|g| native_cache::encode_model(g, &metadata, writer.space(2, budget)).ok()); if let Some((manifest, data)) = cache { let [manifest_entry, payload_entry] = native_cache::ENTRIES; // Deflated whatever the policy: the encoded bodies shrink about fourfold to tenfold, faster @@ -885,6 +884,143 @@ mod tests { assert!(read_design_geometry(&bytes).is_some()); } + /// A saved model of four features (5, 50, 20 and 1 ms) whose states each hold the eight + /// bodies all of them share and eight of their own (a body each feature replaces). No + /// document evaluated it. + fn timeline_model() -> solidcraft_doc::SavedModel { + let bodies: Vec<_> = (0..40) + .map(|i| solidcraft_kernel::box_solid(solidcraft_geom::Vec3::ZERO, solidcraft_geom::Vec3::new(10. + f64::from(i), 20., 30.)).unwrap()) + .collect(); + let records: Vec = (0..40) + .map(|i| json!({"name": format!("Body{i}"), "feature": 1 + i / 8, "component": 0, "key": "0000000000000001", "names": 0, "body": i})) + .collect(); + // As runs of consecutive bodies, which the first state's two groups make one of. + let states: Vec = (0..4) + .map(|i| { + let bodies = if i == 0 { json!([[0, 16]]) } else { json!([[0, 8], [8 * (i + 1), 8]]) }; + json!({ + "bodies": bodies, "sketches": [], "body_counter": 7, "sheets": [], "construct": [], "threads": [], + "sketch_keys": [], "blend_edges": {}, "fitted": {}, "tapered": {}, + }) + }) + .collect(); + let features: Vec = [5., 50., 20., 1.] + .iter() + .enumerate() + .map(|(i, ms)| json!({"id": i + 1, "fingerprint": "0000000000000000", "ms": ms, "skipped": false, "state": i})) + .collect(); + solidcraft_doc::SavedModel { + records: json!({"features": features, "states": states, "model_bodies": records, "names": [{"faces": [], "info": []}], "sketches": []}), + bodies, + } + } + + /// Which features keep their state in `model`. + fn kept_states(model: &Value) -> Vec { + model["features"].as_array().unwrap().iter().map(|f| f.get("state").is_some()).collect() + } + + /// Every body a megabyte, so the records (a few kilobytes) fit in the room left over. + const BODY: usize = 1 << 20; + + /// A thinned model keeps the final state, then the slowest features' states while the bodies + /// they add fit, each body counted once however many states hold it (#3220). + #[test] + fn a_thinned_model_keeps_the_final_state_then_the_slowest_that_fit() { + let model = timeline_model(); + let pieces: Vec> = (0..40).map(|i| vec![(i, BODY)]).collect(); + let thinned = |bodies: usize| { + let budget = bodies * BODY + BODY / 2; + model.thinned(&pieces, budget).map(|(m, kept, counted)| (kept_states(&m.records), kept, m, counted <= budget)).unwrap() + }; + let (states, kept, all, within) = thinned(40); + assert_eq!((states, kept.len(), within), (vec![true; 4], 40, true)); + assert_eq!(all.records, model.records); + // The final state and the slowest other (50 ms): the shared bodies count once, else + // these two states would need 32 bodies' room. + for bodies in [24, 31] { + let (states, kept, two, _) = thinned(bodies); + assert_eq!(states, vec![false, true, false, true], "room for {bodies} bodies"); + assert_eq!(kept, (0..8).chain(16..24).chain(32..40).collect::>()); + assert_eq!(two.records["model_bodies"].as_array().unwrap().len(), 24); + let last = two.final_state().unwrap(); + assert_eq!( + last.bodies.iter().map(|b| b.name.as_str()).collect::>(), + (0..8).chain(32..40).map(|i| format!("Body{i}")).collect::>() + ); + assert_eq!(last.body_counter, 7); + } + assert_eq!(thinned(32).0, vec![false, true, true, true]); + // The final state only: within the budget, then kept over it (nothing smaller is left). + for (bodies, within) in [(16, true), (23, true), (15, false)] { + let (states, kept, one, fits) = thinned(bodies); + assert_eq!((states, fits), (vec![false, false, false, true], within), "room for {bodies} bodies"); + assert_eq!(kept, (0..8).chain(32..40).collect::>()); + assert_eq!( + one.records["states"], + json!([{ + "bodies": [[0, 16]], "sketches": [], "body_counter": 7, "sheets": [], "construct": [], "threads": [], + "sketch_keys": [], "blend_edges": {}, "fitted": {}, "tapered": {}, + }]) + ); + } + } + + /// A model cache too big for the file keeps fewer states rather than none (#3220): the final + /// state and as many of the slowest others as fit, then the final state only, and nothing + /// only when not even that fits. + #[test] + fn a_model_cache_too_big_for_the_file_keeps_fewer_states() { + let model = timeline_model(); + let document = b"{}"; + let states = |space| { + let (manifest, payload) = native_cache::encode_model(&model, document, space).ok()?; + assert!(manifest.len() + payload.len() <= space); + Some(kept_states(&serde_json::from_slice::(&manifest).unwrap()["model"])) + }; + // The cache of the states room for so many bodies picks, at its own size. + let pieces: Vec> = (0..40).map(|i| vec![(i, BODY)]).collect(); + let cache = |bodies: usize| { + let (thin, _, _) = model.thinned(&pieces, bodies * BODY + BODY / 2).unwrap(); + let (manifest, payload) = native_cache::encode_model(&thin, document, usize::MAX).unwrap(); + (manifest.len(), payload.len()) + }; + let encoded = |i: usize| solidcraft_kernel::encode_native_body(&model.bodies[i]).unwrap().len(); + // What a state's own bodies add: more than the records of the caches below, so the sizes + // below pick one set of states each. + let own = (8..40).step_by(8).map(|s| (s..s + 8).map(encoded).sum::()).min().unwrap(); + let (all, two, one) = (cache(40), cache(24), cache(16)); + assert!(own > two.0, "a state's bodies ({own} bytes) outweigh the records ({} bytes)", two.0); + assert_eq!(states(all.0 + all.1), Some(vec![true; 4])); + assert_eq!(states(all.0 + all.1 - 1), Some(vec![false, true, true, true]), "the fastest left out first"); + assert_eq!(states(two.0 + two.1), Some(vec![false, true, false, true])); + assert_eq!(states(one.0 + one.1), Some(vec![false, false, false, true])); + assert_eq!(states(one.0 + one.1 - 1), None); + } + + /// A model whose bodies are all one part stores it once: its records, not its bodies, fill the + /// room. When those of every state do not fit, the final state is still kept if it fits. + #[test] + fn a_model_cache_of_records_too_big_for_the_file_keeps_the_final_state() { + let mut model = timeline_model(); + let part = model.bodies[0].clone(); + model.bodies.iter_mut().for_each(|b| *b = part.clone()); + let document = b"{}"; + let size = |m: &solidcraft_doc::SavedModel| { + let (manifest, payload) = native_cache::encode_model(m, document, usize::MAX).unwrap(); + (manifest.len() + payload.len(), serde_json::from_slice::(&manifest).unwrap()["slices"].as_array().unwrap().len()) + }; + let pieces: Vec> = (0..40).map(|i| vec![(i, BODY)]).collect(); + let (last, _, _) = model.thinned(&pieces, 16 * BODY + BODY / 2).unwrap(); + let ((all, slices), (one, _)) = (size(&model), size(&last)); + assert_eq!(slices, 1, "the part stored once"); + // Room for the final state's cache and a little more, not for the records of all. + let space = one + 256; + assert!(all > space); + let (manifest, _) = native_cache::encode_model(&model, document, space).unwrap(); + assert_eq!(kept_states(&serde_json::from_slice::(&manifest).unwrap()["model"]), vec![false, false, false, true]); + } + #[test] fn native_cache_mismatch_is_only_a_cache_miss() { let doc = document(); diff --git a/crates/io/src/design/native_cache.rs b/crates/io/src/design/native_cache.rs index 7005349f0..1701d6729 100644 --- a/crates/io/src/design/native_cache.rs +++ b/crates/io/src/design/native_cache.rs @@ -5,7 +5,7 @@ use super::*; use solidcraft_doc::{ModelState, SavedModel}; -const FORMAT: &str = "solidcraft-model-cache/2"; +const FORMAT: &str = "solidcraft-model-cache/3"; const PRODUCER: &str = "solidcraft-truck-adapter/3"; const MANIFEST: &str = "cache/model.json"; const PAYLOAD: &str = "cache/bodies.bin"; @@ -26,61 +26,168 @@ fn matrix(t: &[[f64; 4]; 4]) -> Vec { t.iter().flatten().copied().collect() } -/// Encode a saved model. Identical bodies (the same part placed many times) are stored once; a -/// placed copy of a part records the part and its matrix, so it shows that part's display as it -/// did before saving. -pub(super) fn encode_model(model: &SavedModel, document: &[u8]) -> Result<(Vec, Vec)> { - if model.bodies.is_empty() || model.bodies.len() > MAX_RECORDS { - return Err(invalid("cached body count")); +/// Encode a saved model in `space` bytes (manifest and payload together). Identical bodies (the +/// same part placed many times) are stored once; a placed copy of a part records the part and its +/// matrix, so it shows that part's display as it did before saving. A model too big for `space` +/// keeps fewer states, then its final state only ([`SavedModel::thinned`], #3220); an error when +/// not even that fits. +pub(super) fn encode_model(model: &SavedModel, document: &[u8], space: usize) -> Result<(Vec, Vec)> { + let encoded = Encoded::of(model)?; + let total = encoded.slices.iter().fold(0usize, |n, s| n.saturating_add(s.len())); + let fits = |manifest: &[u8], payload: &[u8]| manifest.len() <= MAX_MANIFEST && manifest.len().saturating_add(payload.len()) <= space; + if total <= space { + let every: Vec = (0..model.bodies.len()).collect(); + if let Ok((manifest, data)) = encoded.cache(&model.records, &every, document) + && fits(&manifest, &data) + { + return Ok((manifest, data)); + } } - // What to encode: every body, and the part every placed copy shows. - let mut shares: Vec = Vec::new(); - let mut share_index = std::collections::HashMap::new(); - for b in &model.bodies { - if let Some((share, _)) = b.display_share() { - share_index.entry(share.id()).or_insert_with(|| { - shares.push(share.clone()); - shares.len() - 1 + // Fewer states. The bodies are encoded already: a state costs the slices it adds. + let size = |s: &usize| encoded.slices.get(*s).map_or(0, Vec::len); + let pieces: Vec> = encoded + .bodies + .iter() + .map(|(s, placed)| std::iter::once(s).chain(placed.as_ref().and_then(|(part, _)| encoded.parts.get(*part))).map(|s| (*s, size(s))).collect()) + .collect(); + let mut budget = space.min(MAX_DESIGN_BYTES); + for _ in 0..16 { + let Some((thin, kept, counted)) = model.thinned(&pieces, budget) else { break }; + // Only the final state, which is kept whatever it costs: nothing smaller is left to try. What + // it counted is about what its cache holds, so over `space` it does not fit. + let smallest = counted > budget; + if smallest && counted > space { + break; + } + let Ok((manifest, data)) = encoded.cache(&thin.records, &kept, document) else { + if smallest { + break; + } + budget = counted / 2; + continue; + }; + if fits(&manifest, &data) { + let features = thin.records.get("features").and_then(Value::as_array).map_or(&[][..], Vec::as_slice); + log::info!( + "the model cache ({} MB) does not fit the design file: saved the states of {} of {} features ({} MB), opening evaluates the others again", + total >> 20, + features.iter().filter(|f| f.get("state").is_some()).count(), + features.len(), + manifest.len().saturating_add(data.len()) >> 20 + ); + return Ok((manifest, data)); + } + if smallest { + break; + } + // What the choice did not count (the bodies' table, the slices) took the room: choose again + // within that much less than it counted, so each try keeps less. + let over = manifest.len().saturating_add(data.len()).saturating_sub(space).max(manifest.len().saturating_sub(MAX_MANIFEST)); + budget = counted.saturating_sub(over).saturating_sub(1); + } + log::info!("the model cache ({} MB) does not fit the design file: saved without it", total >> 20); + Err(invalid("model cache size")) +} + +/// A model's bodies, each encoded once and identical ones stored once (keyed by the SHA-256 of +/// their encoding), in the order a cache of the whole model stores them: every body, then the part +/// every placed copy shows. +struct Encoded { + /// Each stored body's encoding (kept apart: only what a cache keeps is copied into its payload). + slices: Vec>, + /// Each model body's slice, with the part (its index in `parts`) and the matrix of a placed copy. + bodies: Vec<(usize, Option<(usize, [[f64; 4]; 4])>)>, + /// Each placed part's slice. + parts: Vec, +} + +impl Encoded { + fn of(model: &SavedModel) -> Result { + // What to encode: every body, and the part every placed copy shows. + let mut shares: Vec = Vec::new(); + let mut share_index = std::collections::HashMap::new(); + let mut placed = Vec::with_capacity(model.bodies.len()); + for b in &model.bodies { + placed.push(b.display_share().map(|(share, t)| { + let part = *share_index.entry(share.id()).or_insert_with(|| { + shares.push(share.clone()); + shares.len() - 1 + }); + (part, t) + })); + } + let bodies: Vec<&solidcraft_kernel::Body> = model.bodies.iter().chain(shares.iter().map(|s| s.original())).collect(); + let encoded = solidcraft_kernel::map_on_threads(&bodies, 16, |b| solidcraft_kernel::encode_native_body(b).ok()); + let mut slices = Vec::new(); + let mut by_digest = std::collections::HashMap::new(); + let mut index = Vec::with_capacity(encoded.len()); + for bytes in encoded { + let bytes = bytes.flatten().ok_or_else(|| invalid("body not stored exactly"))?; + let next = slices.len(); + let i = *by_digest.entry(Sha256::digest(&bytes)).or_insert(next); + if i == next { + slices.push(bytes); + } + index.push(i); + } + let (body_slices, part_slices) = index.split_at_checked(model.bodies.len()).ok_or_else(|| invalid("cached body count"))?; + Ok(Encoded { slices, bodies: body_slices.iter().copied().zip(placed).collect(), parts: part_slices.to_vec() }) + } + + /// The cache of a model with these `records`, whose bodies are these of the model's (indices + /// here): its manifest and payload. Refused when it holds too many bodies or its payload is + /// too large. + fn cache(&self, records: &Value, bodies: &[usize], document: &[u8]) -> Result<(Vec, Vec)> { + if bodies.is_empty() || bodies.len() > MAX_RECORDS { + return Err(invalid("cached body count")); + } + // The slices stored, in the order first needed: the bodies', then their parts'. + let mut order = Vec::new(); + let mut renumbered = vec![None; self.slices.len()]; + let mut slice = |s: usize| -> Result { + let n = renumbered.get_mut(s).ok_or_else(|| invalid("cached body"))?; + Ok(*n.get_or_insert_with(|| { + order.push(s); + order.len() - 1 + })) + }; + let mut parts = Vec::new(); + let mut part_index = vec![None; self.parts.len()]; + let mut table = Vec::with_capacity(bodies.len()); + for &b in bodies { + let (s, placed) = self.bodies.get(b).ok_or_else(|| invalid("cached body"))?; + let s = slice(*s)?; + table.push(match placed { + Some((part, t)) => { + let p = *part_index.get_mut(*part).ok_or_else(|| invalid("display share"))?.get_or_insert_with(|| { + parts.push(*part); + parts.len() - 1 + }); + json!([s, p, matrix(t)]) + } + None => json!([s]), }); } - } - let bodies: Vec<&solidcraft_kernel::Body> = model.bodies.iter().chain(shares.iter().map(|s| s.original())).collect(); - let encoded = solidcraft_kernel::map_on_threads(&bodies, 16, |b| solidcraft_kernel::encode_native_body(b).ok()); - let mut data = Vec::new(); - let mut slices = Vec::new(); - let mut by_digest = std::collections::HashMap::new(); - let mut index = Vec::with_capacity(encoded.len()); - for bytes in encoded { - let bytes = bytes.flatten().ok_or_else(|| invalid("body not stored exactly"))?; - let next = slices.len(); - let i = *by_digest.entry(Sha256::digest(&bytes)).or_insert(next); - if i == next { - if data.len().saturating_add(bytes.len()) > MAX_DESIGN_BYTES || slices.len() >= MAX_UNIQUE { - return Err(invalid("model cache size")); - } - slices.push(json!([data.len(), bytes.len()])); - data.extend(bytes); + let shares = parts.iter().map(|p| slice(*self.parts.get(*p).ok_or_else(|| invalid("display share"))?)).collect::>>()?; + let stored = order.iter().map(|s| self.slices.get(*s).ok_or_else(|| invalid("cached body"))).collect::>>()?; + let length = stored.iter().fold(0usize, |n, bytes| n.saturating_add(bytes.len())); + if length > MAX_DESIGN_BYTES || stored.len() > MAX_UNIQUE { + return Err(invalid("model cache size")); } - index.push(i); + let mut data = Vec::with_capacity(length); + let mut slices = Vec::with_capacity(stored.len()); + for bytes in stored { + slices.push(json!([data.len(), bytes.len()])); + data.extend_from_slice(bytes); + } + let manifest = serde_json::to_vec(&json!({ + "format": FORMAT, "producer": PRODUCER, "app": env!("CARGO_PKG_VERSION"), "build": build(), + "document": digest(document), "payload": digest(&data), + "slices": slices, "shares": shares, "bodies": table, "model": records, + })) + .map_err(invalid)?; + Ok((manifest, data)) } - let (body_index, share_body) = index.split_at(model.bodies.len()); - let mut table = Vec::with_capacity(model.bodies.len()); - for (b, i) in model.bodies.iter().zip(body_index) { - table.push(match b.display_share() { - Some((share, t)) => json!([i, share_index.get(&share.id()).ok_or_else(|| invalid("display share"))?, matrix(&t)]), - None => json!([i]), - }); - } - let manifest = serde_json::to_vec(&json!({ - "format": FORMAT, "producer": PRODUCER, "app": env!("CARGO_PKG_VERSION"), "build": build(), - "document": digest(document), "payload": digest(&data), - "slices": slices, "shares": share_body, "bodies": table, "model": model.records, - })) - .map_err(invalid)?; - if manifest.len() > MAX_MANIFEST { - return Err(invalid("model cache manifest size")); - } - Ok((manifest, data)) } /// The final state of the model a design keeps, when this build computed it for exactly this diff --git a/docs/design-container.md b/docs/design-container.md index c435fdc58..e4e8c9d84 100644 --- a/docs/design-container.md +++ b/docs/design-container.md @@ -119,8 +119,9 @@ Engine Save/Save As write the evaluated model with the design by default carry it. Saving never forces placement, evaluation or tessellation: the model is the session's own evaluation, and only when it is the evaluation of the document being saved (`Model::is_current`). An active sketch, uncaptured moves, feature -errors, mesh bodies or exceeded budgets leave the cache out; the design saves as -before. +errors or mesh bodies leave the cache out; the design saves as before. A cache +over its budgets keeps fewer states (below) and is left out only when not even +the final state fits. The model is the whole evaluation: every feature's result (warning, boolean diagnostics, whether it was skipped, its fingerprint) and the state after it @@ -146,7 +147,7 @@ SHA-256 of their encoding), and a placed copy of a repeated part (an instance fr a STEP assembly) records the part and its matrix, so it shows that part's display again instead of being meshed on its own. -`cache/model.json` records `solidcraft-model-cache/2`, the producer revision +`cache/model.json` records `solidcraft-model-cache/3`, the producer revision `solidcraft-truck-adapter/3`, the application version, the build (source commit, CPU architecture and OS), the SHA-256 of the exact `document.json` (whose blob references pin every STEP, mesh and image payload) and of `cache/bodies.bin`, the @@ -159,9 +160,20 @@ The fixed budgets of earlier caches (4,096 bodies, 100,000 faces, 64 MiB) scale with the design now: up to 200,000 body records, 100,000 distinct bodies, a 32 MiB manifest and a payload within the container's part budget (256 MiB), leaving room for the document. Individual bodies keep the codec's own limits. The archive-wide -budgets (256 MiB per part, 512 MiB expanded, 10,000 entries) are unchanged, and the -cache is left out when it would push the container near them. Decoding runs under -`kernel::guard` and is linear in the payload's bytes. +budgets (256 MiB per part, 512 MiB expanded, 10,000 entries) are unchanged. +Decoding runs under `kernel::guard` and is linear in the payload's bytes. + +A model whose cache would push the container past them keeps fewer states +(`SavedModel::thinned`, #3220): the final state (what the design shows), then the +other states, the slowest feature's first (the evaluations opening would otherwise +repeat for longest), each while what it adds still fits: the bodies no state kept +so far holds (a body is stored once however many states hold it) and the records +of the state, those bodies, their names and sketches. When the rest of the +manifest (the bodies' table, the slices) then leaves too little room, the choice +is made again within that much less, and the final state alone is always tried. +A feature whose state is left out records none. The cache is left out (and the +save logs it) only when not even the final state fits; a thinned cache logs how +many features kept their state. ### Opening from the model @@ -174,9 +186,18 @@ evaluates from the first feature it changes, as after any evaluation: a feature appended or edited at the end evaluates from the saved final state, an edit to feature k from the saved state before k. Moving the timeline marker, undo and redo reuse the saved results whose fingerprints come back. `SOLIDCRAFT_NO_MODEL_CACHE` -set evaluates anyway. Any failure (a key or fingerprint mismatch, a malformed -record, a body that does not decode) is a miss: the design is evaluated from its -timeline as before, and only that. +set evaluates anyway. + +A thinned cache is taken the same way, and the features whose state it left out +are evaluated while opening, each from the state before it (kept or just +evaluated); the states it kept after them stand. Opening then costs the +evaluations of those features only, and the model is whole afterwards: nothing +else in the application sees a missing state. Should one of them fail now, the +features after it are evaluated too, as a full evaluation would. + +Any failure (a key or fingerprint mismatch, a malformed record, a body that does +not decode) is a miss: the design is evaluated from its timeline as before, and +only that. This replaces the earlier rule that a cache never supplies evaluated session state. It is safe because the cache is the result of evaluating exactly this