diff --git a/crates/doc/src/model_cache.rs b/crates/doc/src/model_cache.rs index 95ab45149..233477ceb 100644 --- a/crates/doc/src/model_cache.rs +++ b/crates/doc/src/model_cache.rs @@ -223,11 +223,15 @@ impl Writer { } fn sheet(&mut self, sheet: &crate::sheet::SheetBody) -> Option { - let record = serde_json::to_value(sheet).ok()?; - let key = record.to_string(); + // Keyed by the sheet written as text, straight from the sheet: every state asks for each + // of its sheets, and building the record (a JSON tree) each time only to find it written + // already took most of a sheet-metal design's save (906 states, 300 sheets: 0.8 s). Two + // sheets are the same text exactly when they are the same record. + let key = serde_json::to_string(sheet).ok()?; if let Some(i) = self.sheet_index.get(&key) { return Some(*i); } + let record = serde_json::to_value(sheet).ok()?; self.sheets.push(record); self.sheet_index.insert(key, self.sheets.len() - 1); Some(self.sheets.len() - 1) diff --git a/crates/engine/src/cmd/file.rs b/crates/engine/src/cmd/file.rs index d3dc965be..95100ebd4 100644 --- a/crates/engine/src/cmd/file.rs +++ b/crates/engine/src/cmd/file.rs @@ -1425,6 +1425,41 @@ mod tests { assert!(d.rejected.is_empty()); } + /// The model a save keeps writes each distinct sheet-metal body once, and every state names + /// the records of exactly the sheets it holds (found by the sheet's text, not by building its + /// record again for every state, which took most of a large sheet-metal design's save). + #[test] + fn a_saved_model_writes_each_sheet_once_for_the_states_that_hold_it() { + let mut s = Session::new(Document::new("Sheets")); + let sheet_rule = json!({"name": "AL2", "thickness": "2 mm", "k_factor": "0.5", "bend_radius": "1 mm", "active": true}); + s.execute("sheet.manage_rules", &sheet_rule).unwrap(); + for (i, x) in [0.0, 200.0, 400.0].into_iter().enumerate() { + s.execute("sketch.create", &json!({"plane": "XY", "name": format!("Base{i}")})).unwrap(); + s.execute("sketch.rectangle.two_point", &json!({"p0": [x, 0], "p1": [x + 150.0, 100]})).unwrap(); + s.execute("sketch.finish", &json!({})).unwrap(); + s.execute("sheet.flange", &json!({"sketch": format!("Base{i}")})).unwrap(); + s.execute("sheet.flange", &json!({"edges": [[x + 75.0, 0, 2]], "height": 20})).unwrap(); + } + let saved = saved_geometry(&s, &s.doc).unwrap(); + let records = &saved.records; + let sheets = records["sheets"].as_array().unwrap(); + let texts: std::collections::HashSet = sheets.iter().map(Value::to_string).collect(); + assert_eq!(texts.len(), sheets.len(), "each sheet once"); + let mut held = std::collections::HashSet::new(); + 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 = state["sheets"].as_array().unwrap(); + assert_eq!(named.len(), r.state.sheets.len()); + for (i, sheet) in named.iter().zip(&r.state.sheets) { + let written = &sheets[i.as_u64().unwrap() as usize]; + assert_eq!(*written, serde_json::to_value(sheet).unwrap(), "{}: the record of its own sheet", r.name); + held.insert(written.to_string()); + } + } + assert_eq!(held, texts, "every record written is one a state holds"); + assert!(sheets.len() >= 6, "each part's base, then with its flange: {}", sheets.len()); + } + /// A design whose saved model would give a body back with other topology opens with the /// bodies it was saved with. A pattern copy cut by an automatic extrude came back from the /// native codec with two loose vertices more (10 faces, 18 edges, 12 vertices instead of 10); diff --git a/crates/io/src/design.rs b/crates/io/src/design.rs index 683ed07c9..3a293b8cb 100644 --- a/crates/io/src/design.rs +++ b/crates/io/src/design.rs @@ -1190,6 +1190,25 @@ mod tests { } } + /// The model cache's manifest, written straight from the model's records (not copied into a + /// JSON tree first), is byte for byte what writing it as a tree wrote: the same keys in the + /// same (sorted) order, the same numbers. + #[test] + fn the_model_cache_manifest_is_what_a_json_tree_writes() { + use solidcraft_geom::Vec3; + let part = solidcraft_kernel::box_solid(Vec3::ZERO, Vec3::new(10., 20., 30.)).unwrap(); + let share = solidcraft_kernel::DisplayShare::new(part.clone()); + let t = [[1., 0., 0., 0.], [0., 1., 0., 0.], [0., 0., 1., 0.], [50.5, -0.25, 1e-7, 1.]]; + let moved = solidcraft_kernel::transform(&part, Vec3::new(50.5, -0.25, 1e-7), Vec3::ZERO, Vec3::Z, 0.).unwrap(); + let model = saved_model(vec![part.clone(), moved.with_display_share(&share, t)]); + let bytes = write_design_container_with_cache(&document(), DesignCompression::Fast, None, Some(&model)).unwrap(); + let manifest = &parts(&bytes)["cache/model.json"]; + let tree: Value = serde_json::from_slice(manifest).unwrap(); + assert_eq!(serde_json::to_vec(&tree).unwrap(), *manifest); + assert_eq!(tree["model"], model.records); + assert_eq!(tree.as_object().unwrap().len(), 11, "{:?}", tree.as_object().unwrap().keys().collect::>()); + } + /// #9170: a mesh body (an imported STL) is kept in the model cache with the rest of the /// model, as its own triangles, colour and paint. One mesh body left the whole cache out, so /// opening the design evaluated every feature again. diff --git a/crates/io/src/design/native_cache.rs b/crates/io/src/design/native_cache.rs index 72b82b732..9f4c2b197 100644 --- a/crates/io/src/design/native_cache.rs +++ b/crates/io/src/design/native_cache.rs @@ -289,16 +289,44 @@ impl Encoded { 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(), - "evaluation": evaluation(), "document": digest(document), "payload": digest(&data), - "slices": slices, "shares": shares, "bodies": table, "model": records, - })) + let manifest = serde_json::to_vec(&Manifest { + app: env!("CARGO_PKG_VERSION"), + bodies: table, + build: build(), + document: digest(document), + evaluation: evaluation(), + format: FORMAT, + model: records, + payload: digest(&data), + producer: PRODUCER, + shares, + slices, + }) .map_err(invalid)?; Ok((manifest, data)) } } +/// A model cache's manifest as written: the model's records are written where they are, not +/// copied into a JSON tree first (copying a large model's records took as long as writing them, +/// and a cache too big for the file is written again for each try at fewer states). The fields +/// are in the order a JSON object keeps its keys (sorted), so the manifest is the one a tree +/// wrote, byte for byte. +#[derive(serde::Serialize)] +struct Manifest<'a> { + app: &'a str, + bodies: Vec, + build: String, + document: String, + evaluation: String, + format: &'a str, + model: &'a Value, + payload: String, + producer: &'a str, + shares: Vec, + slices: Vec, +} + /// The final state of the model a design keeps, when this build's evaluation source computed it /// for exactly this document. Any failure is a miss, never a document error. pub fn read_design_geometry(bytes: &[u8]) -> Option> {