Save: write the model cache manifest without copying the model's records (#10419)
* Find a saved model's sheets by their text, not a record built per state Saving a design with its model cache asked, for every state, for each sheet-metal body it holds, and built that sheet's JSON record each time only to find it written already. A design of 906 states with 300 sheets spent most of its save there. The sheet's text written straight from it is the key now; its record is built only the first time. The records and the file are the same. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * Write the model cache manifest without copying the model's records The manifest was built as a JSON tree, which copied the whole model's records (13 to 33 MB of records in large designs) before writing them, and a cache too big for the file wrote it again for each try at fewer states. The manifest is now written from the records where they are, its fields in the sorted order the tree wrote them, so the file is the same byte for byte. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
committed by
GitHub
parent
9475e0e1a4
commit
7e701aabc1
@@ -223,11 +223,15 @@ impl Writer {
|
||||
}
|
||||
|
||||
fn sheet(&mut self, sheet: &crate::sheet::SheetBody) -> Option<usize> {
|
||||
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)
|
||||
|
||||
@@ -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<String> = 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);
|
||||
|
||||
@@ -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::<Vec<_>>());
|
||||
}
|
||||
|
||||
/// #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.
|
||||
|
||||
@@ -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<Value>,
|
||||
build: String,
|
||||
document: String,
|
||||
evaluation: String,
|
||||
format: &'a str,
|
||||
model: &'a Value,
|
||||
payload: String,
|
||||
producer: &'a str,
|
||||
shares: Vec<usize>,
|
||||
slices: Vec<Value>,
|
||||
}
|
||||
|
||||
/// 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<std::sync::Arc<ModelState>> {
|
||||
|
||||
Reference in New Issue
Block a user