SolidCraft: an open-source parametric 3D CAD application built in pure Rust. Desktop, web, and automation through MCP.
Saving: designs with many threaded holes keep all saved states (#10601)
kaltenbrunnermarkusdc33c4a24d94
diff --git a/crates/doc/src/model_cache.rs b/crates/doc/src/model_cache.rsindex 6301061540..663dbe6792 100644--- a/crates/doc/src/model_cache.rs+++ b/crates/doc/src/model_cache.rs@@ -30,6 +30,39 @@ const MAX_HELD: usize = 50_000_000;/// evaluated again on every open (#8541).const MAX_TEXT: usize = 1 << 24;+/// Whether two tap markings are the same to the bit: then they write the same record. (`==` takes+/// 0.0 and -0.0 for the same number, whose records differ.) Every field is named, so a new one+/// cannot be left out.+fn same_marking(a: &ThreadInfo, b: &ThreadInfo) -> bool {+ let numbers = |t: &ThreadInfo| {+ let ThreadInfo {+ feature: _,+ body: _,+ designation: _,+ major_diameter,+ radius,+ pitch,+ internal: _,+ axis_point,+ axis,+ start,+ end,+ form: _,+ hand: _,+ starts: _,+ } = t;+ [*major_diameter, *radius, *pitch, axis_point.x, axis_point.y, axis_point.z, axis.x, axis.y, axis.z, *start, *end]+ };+ a.feature == b.feature+ && a.body == b.body+ && a.designation == b.designation+ && a.internal == b.internal+ && a.form == b.form+ && a.hand == b.hand+ && a.starts == b.starts+ && numbers(a).iter().zip(numbers(b).iter()).all(|(x, y)| x.to_bits() == y.to_bits())+}+fn hex(v: u64) -> String {format!("{v:016x}")}@@ -120,6 +153,14 @@ struct Writer {/// share them (writing them with every state made a sheet-metal design's model too large to keep).sheets: Vec<Json>,sheet_index: HashMap<String, usize>,+ /// Tap markings (cosmetic threads) by their record, as the sheets: every state lists the+ /// markings of all the features before it, so writing them with each state grew with the+ /// square of the features (400 marked cylinders: 34 of a 37 MB manifest, past its limit).+ threads: Vec<Json>,+ thread_index: HashMap<String, usize>,+ /// The state written last and the places of its markings: a state mostly keeps the markings+ /// of the one before it, which are found again without writing them as text.+ last_threads: Option<(Arc<ModelState>, Vec<usize>)>,states: Vec<Json>,state_index: HashMap<usize, usize>,/// Each kernel body's place in `bodies`, by its record.@@ -258,6 +299,32 @@ impl Writer {Some(self.sheets.len() - 1)}+ /// The places of `st`'s markings among the written ones. A marking at the same place in the+ /// state written last, the same to the bit, is that one (no text built); any other is keyed by+ /// its text, as a sheet is.+ fn threads(&mut self, st: &Arc<ModelState>) -> Option<Vec<usize>> {+ let mut out = Vec::with_capacity(st.threads.len());+ for (i, t) in st.threads.iter().enumerate() {+ let before = self.last_threads.as_ref().and_then(|(s, at)| Some((s.threads.get(i)?, *at.get(i)?)));+ if let Some((_, at)) = before.filter(|(b, _)| same_marking(b, t)) {+ out.push(at);+ continue;+ }+ let key = serde_json::to_string(t).ok()?;+ let at = match self.thread_index.get(&key) {+ Some(at) => *at,+ None => {+ self.threads.push(serde_json::to_value(t).ok()?);+ self.thread_index.insert(key, self.threads.len() - 1);+ self.threads.len() - 1+ }+ };+ out.push(at);+ }+ self.last_threads = Some((st.clone(), out.clone()));+ Some(out)+ }+fn state(&mut self, st: &Arc<ModelState>) -> Option<usize> {let id = Arc::as_ptr(st) as usize;if let Some(i) = self.state_index.get(&id) {@@ -266,11 +333,12 @@ impl Writer {let bodies = st.bodies.iter().map(|b| self.model_body(b)).collect::<Option<Vec<_>>>()?;let sketches = st.sketches.iter().map(|s| self.sketch(s)).collect::<Option<Vec<_>>>()?;let sheets = st.sheets.iter().map(|s| self.sheet(s)).collect::<Option<Vec<_>>>()?;+ let threads = self.threads(st)?;let r = json!({"bodies": runs(&bodies), "sketches": runs(&sketches), "body_counter": st.body_counter,"reserved_body_names": st.reserved_body_names.iter().collect::<Vec<_>>(),"sheets": sheets, "construct": serde_json::to_value(&st.construct).ok()?,- "threads": serde_json::to_value(&st.threads).ok()?,+ "threads": runs(&threads),"sketch_key_set": self.sketch_key_set(&st.sketch_keys),"blend_edges": serde_json::to_value(&st.blend_edges).ok()?, "fitted": serde_json::to_value(&st.fitted).ok()?,"tapered": serde_json::to_value(&st.tapered).ok()?,@@ -306,7 +374,8 @@ impl Model {// The check mode the results were evaluated under: they are another mode's results.let records = json!({"features": features, "states": w.states, "model_bodies": w.model_bodies, "names": w.names, "sketches": w.sketches,- "sheets": w.sheets, "validation": self.validation().id(), "rounds": w.rounds, "sketch_key_sets": w.sketch_key_sets,+ "sheets": w.sheets, "threads": w.threads, "validation": self.validation().id(), "rounds": w.rounds,+ "sketch_key_sets": w.sketch_key_sets,});Ok(SavedModel { records, bodies: w.bodies })})@@ -388,6 +457,10 @@ impl SavedModel {for s in list("sheets")? {sheets.push(serde_json::from_value::<crate::sheet::SheetBody>(s.clone()).map_err(|_| bad("saved model: sheet"))?);}+ let mut threads = Vec::new();+ for t in list("threads")? {+ threads.push(serde_json::from_value::<ThreadInfo>(t.clone()).map_err(|_| bad("saved model: tap marking"))?);+ }// The sketch-key sets, each the changes from an earlier one (`base`): a short record can// stand for a large set, so what they hold counts towards MAX_HELD as they are built.let mut key_sets: Vec<BTreeMap<u64, u64>> = Vec::new();@@ -413,6 +486,12 @@ impl SavedModel {}key_sets.push(keys);}+ // Running totals of what each marking costs to clone (its items and text), by place.+ let mut thread_weight = vec![0usize];+ for t in &threads {+ let w = 1usize.saturating_add((t.body.len().saturating_add(t.designation.len())) / 64);+ thread_weight.push(thread_weight.last().copied().unwrap_or(0).saturating_add(w));+ }let records = list("states")?;// What every state holds, counted from its runs and its sketch-key set before any is// built (a hostile file cannot make the states clone more than MAX_HELD items).@@ -422,6 +501,14 @@ impl SavedModel {held = held.saturating_add(run.get(1).and_then(Json::as_u64).and_then(|n| usize::try_from(n).ok()).unwrap_or(usize::MAX));}}+ // A marking is cloned deeply into every state that lists it, so each counts by its+ // text, not as one item: a run of a large marking repeated cannot exhaust memory.+ for run in s.get("threads").and_then(Json::as_array).ok_or_else(|| bad("saved model: state"))? {+ let first = index(run.get(0), threads.len())?;+ let count = run.get(1).and_then(Json::as_u64).and_then(|n| usize::try_from(n).ok()).ok_or_else(|| bad("saved model: run"))?;+ let end = first.checked_add(count).filter(|end| *end <= threads.len()).ok_or_else(|| bad("saved model: run"))?;+ held = held.saturating_add(thread_weight[end].saturating_sub(thread_weight[first]));+ }held = held.saturating_add(s.get("sheets").and_then(Json::as_array).map_or(0, Vec::len));held = held.saturating_add(key_sets.get(index(s.get("sketch_key_set"), key_sets.len())?).map_or(0, BTreeMap::len));if held > MAX_HELD {@@ -465,7 +552,7 @@ impl SavedModel {.map(|i| index(Some(i), sheets.len()).and_then(|i| sheets.get(i).cloned().ok_or_else(|| bad("saved model: sheet")))).collect::<Result<Vec<_>>>()?,construct: serde_json::from_value(field("construct")?).map_err(invalid)?,- threads: serde_json::from_value(field("threads")?).map_err(invalid)?,+ threads: ids("threads", threads.len())?.into_iter().filter_map(|i| threads.get(i).cloned()).collect(),sketch_keys,blend_edges: serde_json::from_value(field("blend_edges")?).map_err(invalid)?,fitted: serde_json::from_value(field("fitted")?).map_err(invalid)?,@@ -613,10 +700,11 @@ impl SavedModel {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();- // The sheet bodies, each written once and named by place in the states: all kept.+ // The sheet bodies and tap markings, each written once and named by place in the states: all kept.let sheets = list("sheets").cloned().unwrap_or_default();+ let threads = list("threads").cloned().unwrap_or_default();let key_sets = list("sketch_key_sets").cloned().unwrap_or_default();- let mut used = bytes(sheets.as_slice()).iter().chain(bytes(key_sets.as_slice()).iter()).fold(0usize, |t, b| t.saturating_add(*b));+ let mut used = [&sheets, &threads, &key_sets].iter().flat_map(|l| bytes(l.as_slice())).fold(0usize, |t, b| t.saturating_add(b));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)?;@@ -722,7 +810,8 @@ impl SavedModel {}let mut 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), "sheets": sheets, "rounds": out_rounds, "sketch_key_sets": key_sets,+ "sketches": kept_of(sketches.as_slice(), &sketches_used), "sheets": sheets, "threads": threads, "rounds": out_rounds,+ "sketch_key_sets": key_sets,});// The check mode the results were evaluated under, as the model records it (a model// that records none stays without one: adopting it is refused either way).diff --git a/crates/engine/src/cmd/file.rs b/crates/engine/src/cmd/file.rsindex 4be66d8b8e..274bdc54c8 100644--- a/crates/engine/src/cmd/file.rs+++ b/crates/engine/src/cmd/file.rs@@ -1904,6 +1904,79 @@ mod tests {std::fs::remove_file(path).unwrap();}+ /// A design's saved model writes each tap marking once, and every state names the markings+ /// it holds by place: every state listed the markings of all the features before it, so a+ /// design of many marked cylinders grew with the square of its features (400 cylinders: 34 of+ /// a 37 MB manifest), passed the cache's size limit and kept only some of its states. Opened+ /// from the file, every state holds the markings it was evaluated with, to the bit.+ #[test]+ fn a_saved_model_writes_each_tap_marking_once() {+ let design = |cylinders: usize| {+ let mut s = Session::new(Document::new("Marked"));+ let run = |s: &mut Session, id: &str, p: Value| s.execute(id, &p).unwrap();+ for i in 0..cylinders {+ let x = 10 * i;+ run(&mut s, "solid.cylinder", json!({"radius":3,"height":6,"base":[x,0,0]}));+ run(&mut s, "solid.thread", json!({"face":[x + 3,0,2],"designation":"M6","length":4}));+ }+ s+ };+ let size = |s: &Session| serde_json::to_vec(&saved_geometry(s, &s.doc).unwrap().records).unwrap().len();+ let (few, s) = (design(4), design(12));+ let markings = &s.model.state().threads;+ assert_eq!(markings.len(), 12, "one marking per cylinder");+ let per_cylinder = (size(&s) - size(&few)) / 8;+ let all = serde_json::to_vec(markings).unwrap().len();+ assert!(per_cylinder * 2 < all, "each later cylinder adds {per_cylinder} bytes, its markings are {all}");+ let saved = saved_geometry(&s, &s.doc).unwrap();+ let records = &saved.records;+ let written = records["threads"].as_array().unwrap();+ let texts: std::collections::HashSet<String> = written.iter().map(Value::to_string).collect();+ assert_eq!((texts.len(), written.len()), (12, 12), "each marking once in the manifest");+ for (r, feature) in s.model.results.iter().zip(records["features"].as_array().unwrap()) {+ let state = &records["states"][feature["state"].as_u64().unwrap() as usize];+ let named: Vec<Value> = state["threads"]+ .as_array()+ .unwrap()+ .iter()+ .flat_map(|run| {+ let first = run[0].as_u64().unwrap() as usize;+ (first..first + run[1].as_u64().unwrap() as usize).map(|i| written[i].clone())+ })+ .collect();+ assert_eq!(Value::Array(named), serde_json::to_value(&r.state.threads).unwrap(), "{}: its own markings", r.name);+ }+ // Opened from the file as saved: nothing evaluated, every state as evaluated.+ let bytes = solidcraft_io::write_design_container_with_cache(&s.doc, solidcraft_io::DesignCompression::Fast, None, Some(&saved)).unwrap();+ let kept = solidcraft_io::DesignContainerReader::new(std::io::Cursor::new(&bytes)).unwrap().geometry().unwrap();+ let reopened = Session::opened(solidcraft_io::read_design(&bytes).unwrap(), Some(&kept));+ assert_eq!(reopened.model.last_recomputed, 0, "taken from the file, not evaluated");+ assert_eq!(reopened.model.results.len(), s.model.results.len());+ for (a, b) in reopened.model.results.iter().zip(&s.model.results) {+ assert_eq!(a.ms, b.ms, "{}: taken from the file", b.name);+ assert_eq!(serde_json::to_string(&a.state.threads).unwrap(), serde_json::to_string(&b.state.threads).unwrap(), "{}", b.name);+ let names = |r: &solidcraft_doc::FeatureResult| r.state.bodies.iter().map(|b| (b.name.clone(), b.feature)).collect::<Vec<_>>();+ assert_eq!(names(a), names(b), "{}", b.name);+ }+ }++ /// A saved model whose states name one large tap marking over and over is refused: every+ /// state holds its own copy of each marking it lists, so a marking counts by its text.+ #[test]+ fn a_saved_model_repeating_a_large_tap_marking_is_refused() {+ let mut s = Session::new(Document::new("Marked"));+ s.execute("solid.cylinder", &json!({"radius":3,"height":6,"base":[0,0,0]})).unwrap();+ s.execute("solid.thread", &json!({"face":[3,0,2],"designation":"M6","length":4})).unwrap();+ let saved = saved_geometry(&s, &s.doc).unwrap();+ assert!(solidcraft_doc::Model::adopt(&s.doc, &saved).is_ok());+ let mut hostile = solidcraft_doc::SavedModel { records: saved.records.clone(), bodies: saved.bodies.clone() };+ hostile.records["threads"][0]["designation"] = json!("M".repeat(1 << 20));+ let repeated: Vec<Value> = (0..4000).map(|_| json!([0, 1])).collect();+ let last = hostile.records["states"].as_array().unwrap().len() - 1;+ hostile.records["states"][last]["threads"] = Value::Array(repeated);+ assert!(solidcraft_doc::Model::adopt(&s.doc, &hostile).is_err());+ }+#[test]#[ignore = "manual native cache save benchmark; generate issue303-step.json first"]fn benchmark_native_cache_save() {diff --git a/crates/io/src/design.rs b/crates/io/src/design.rsindex 6a75d23bc6..bc2a69621e 100644--- a/crates/io/src/design.rs+++ b/crates/io/src/design.rs@@ -1244,7 +1244,7 @@ mod tests {records: json!({"features": [{"id": 1, "fingerprint": "0000000000000000", "ms": 0.0, "skipped": false, "state": 0}],"states": [state], "model_bodies": records, "names": [{"faces": [], "info": []}], "sketches": [], "sheets": [],- "rounds": [], "sketch_key_sets": [{"set": [], "drop": []}],+ "threads": [], "rounds": [], "sketch_key_sets": [{"set": [], "drop": []}],}),bodies,}@@ -1499,7 +1499,7 @@ mod tests {solidcraft_doc::SavedModel {records: json!({"features": features, "states": states, "model_bodies": records, "names": [{"faces": [], "info": []}], "sketches": [],- "sheets": [], "validation": "each_feature", "rounds": [], "sketch_key_sets": [{"set": [], "drop": []}],+ "sheets": [], "threads": [], "validation": "each_feature", "rounds": [], "sketch_key_sets": [{"set": [], "drop": []}],}),bodies,}@@ -1627,18 +1627,23 @@ mod tests {assert!(read_design_preview(&bytes).is_none());assert_eq!(crate::read_design(&bytes).unwrap().to_json(), doc.to_json());}- // A cache of the format before sketch-key sets (/4) is a miss, and so are records of- // that shape (each state's keys inline) under the current format: never misread.+ // A cache of the format before sketch-key sets (/4) or before the tap markings were+ // written once (/8) is a miss, and so are records of those shapes (each state's keys or+ // markings inline) under the current format: never misread.let manifest: Value = serde_json::from_slice(&original["cache/model.json"]).unwrap();let mut older = manifest.clone();older["format"] = json!("solidcraft-model-cache/4");+ let mut before_markings = manifest.clone();+ before_markings["format"] = json!("solidcraft-model-cache/8");+ let mut markings_inline = manifest.clone();+ markings_inline["model"].as_object_mut().unwrap().remove("threads");let mut inline = manifest.clone();let model = inline["model"].as_object_mut().unwrap();model.remove("sketch_key_sets");let state = model["states"][0].as_object_mut().unwrap();state.remove("sketch_key_set");state.insert("sketch_keys".into(), json!([]));- for manifest in [older, inline] {+ for manifest in [older, before_markings, markings_inline, inline] {let mut entries = original.clone();entries.insert("cache/model.json".into(), serde_json::to_vec(&manifest).unwrap());let bytes = package(&entries);diff --git a/crates/io/src/design/native_cache.rs b/crates/io/src/design/native_cache.rsindex 413300a549..3440819f78 100644--- a/crates/io/src/design/native_cache.rs+++ b/crates/io/src/design/native_cache.rs@@ -6,7 +6,7 @@use super::*;use solidcraft_doc::{ModelState, SavedModel};-const FORMAT: &str = "solidcraft-model-cache/8";+const FORMAT: &str = "solidcraft-model-cache/9";const PRODUCER: &str = "solidcraft-truck-adapter/4";const MANIFEST: &str = "cache/model.json";const PAYLOAD: &str = "cache/bodies.bin";diff --git a/docs/design-container.md b/docs/design-container.mdindex 884d514936..c3b5575ddf 100644--- a/docs/design-container.md+++ b/docs/design-container.md@@ -162,7 +162,7 @@ Before #9170 one mesh body left the cache out for the whole model. As for a B-remesh body is kept only when its slice reads back as the same vertices, bit for bit, thesame triangles, colour and paint; the picture shown while opening draws its triangles.-`cache/model.json` records `solidcraft-model-cache/8`, the producer revision+`cache/model.json` records `solidcraft-model-cache/9`, the producer revision`solidcraft-truck-adapter/4`, the application version, the build (source commit,CPU architecture and OS), the evaluation source (below, with the CPU architectureand OS), the geometry kernel that computed it (`solidcore`; earlier builds could write `smlib`, a cache this build evaluates again), the SHA-256 of the exact `document.json` (whose blob references pin every@@ -198,7 +198,10 @@ repeat for longest), each while what it adds still fits: the bodies no state kepso far holds (a body is stored once however many states hold it) and the recordsof the state, those bodies, their names and sketches. Sheet bodies, like sketches, arewritten once and named by place in the states (most features leave the sheets as they were); a-thinned cache keeps all of them. So are the states' sketch keys (the evaluation key of each+thinned cache keeps all of them. So are the tap markings (cosmetic threads): a state lists the+markings of every feature before it, so writing them with each state grew with the square of the+features (400 marked cylinders: 34 of a 37 MB manifest); each marking is written once and a state+names its markings as runs of places, and a thinned cache keeps all of them. So are the states' sketch keys (the evaluation key of eachsketch before the state): each distinct set is written once, as the keys it sets and dropsrelative to the set written before it, and a state names its set by place; a thinned cachekeeps all of them too. When the rest of the