From fbb00dc7b2e2afd67bc793c405acb86f6e65bf93 Mon Sep 17 00:00:00 2001 From: kaltenbrunnermarkus <153593707+kaltenbrunnermarkus@users.noreply.github.com> Date: Sun, 1 Nov 2026 09:46:17 +0000 Subject: [PATCH] Canvas: store reference image data as shared text so edits stop copying it (#10489) * claim: Canvas: store reference image data as shared text so edits stop copying it * Canvas: store reference image data as shared text so edits stop copying it Every edit clones the document, because the undo history still holds the old one, and the canvas image was a plain String: each of the up to 200 undo states owned its own copy of the base64 text. Canvas::data is now Arc, like the shared STEP text, meshes and point clouds; serde's rc feature writes it as the same JSON string, so design files stay byte for byte the same. A 4.2 MB image with 250 edits: peak memory 1.09 GiB -> 42 MB, run time 1.16 s -> 0.12 s. Co-Authored-By: Claude Opus 5.5 --------- Co-authored-by: Markus Kaltenbrunner Co-authored-by: Claude Opus 5.5 --- crates/doc/src/canvas.rs | 6 ++++-- crates/engine/src/cmd/edit_tests.rs | 22 ++++++++++++++++++++++ crates/engine/src/cmd/sketch_canvas.rs | 2 +- crates/io/src/design.rs | 4 ++-- 4 files changed, 29 insertions(+), 5 deletions(-) diff --git a/crates/doc/src/canvas.rs b/crates/doc/src/canvas.rs index e3f3de5bb..e1d0fbb92 100644 --- a/crates/doc/src/canvas.rs +++ b/crates/doc/src/canvas.rs @@ -15,8 +15,10 @@ pub struct Canvas { pub name: String, /// `png` or `jpeg`. pub format: String, - /// The image file, base64. - pub data: String, + /// The image file, base64. Shared rather than owned: every edit clones the document (the undo + /// history keeps the old one), and an image of up to 16 MiB copied into each of the 200 undo + /// states would hold gigabytes. Serialized as the same JSON string as before. + pub data: std::sync::Arc, /// Pixel size of the image. pub pixels: [u32; 2], pub plane: PlaneRef, diff --git a/crates/engine/src/cmd/edit_tests.rs b/crates/engine/src/cmd/edit_tests.rs index 0cd451e37..a6390787c 100644 --- a/crates/engine/src/cmd/edit_tests.rs +++ b/crates/engine/src/cmd/edit_tests.rs @@ -1628,3 +1628,25 @@ fn timeline_joint_folders_reject_hostile_joint_references_atomically() { } } } + +/// A reference image is shared, not copied, by every edit after it: each undo state holds the +/// design as it was, and a copied image of up to 16 MiB per state would hold gigabytes. +#[test] +fn edits_after_a_canvas_share_its_image_with_every_undo_state() { + let mut s = Session::default(); + let mut png = b"\x89PNG\r\n\x1a\n\0\0\0\rIHDR".to_vec(); + png.extend([0, 0, 0, 4, 0, 0, 0, 2, 8, 6, 0, 0, 0, 0, 0, 0, 0]); + png.resize(64 << 10, 0); + run(&mut s, "canvas.insert", json!({"data": solidcraft_doc::canvas::base64_encode(&png), "plane": "XY", "width": 40})); + for i in 0..20 { + run(&mut s, "parameters.add", json!({"name": format!("p{i}"), "expression": "10 mm"})); + } + let image = s.doc.canvases[0].data.as_ptr(); + let copies = s.undo.iter().filter_map(|u| u.doc.canvases.first()).filter(|c| c.data.as_ptr() != image).count(); + assert_eq!(copies, 0, "undo states holding their own copy of the image"); + for _ in 0..20 { + run(&mut s, "edit.undo", json!({})); + } + assert_eq!(s.doc.canvases[0].data.as_ptr(), image, "undo brings back the same image"); + assert_eq!(s.doc.canvases[0].bytes().as_deref(), Some(png.as_slice())); +} diff --git a/crates/engine/src/cmd/sketch_canvas.rs b/crates/engine/src/cmd/sketch_canvas.rs index ed3775443..36e3a4636 100644 --- a/crates/engine/src/cmd/sketch_canvas.rs +++ b/crates/engine/src/cmd/sketch_canvas.rs @@ -67,7 +67,7 @@ fn make(s: &mut Session, p: &Value, cmd: &str, decal: bool) -> Result { id, name: name.clone(), format: format.into(), - data: base64_encode(&bytes), + data: base64_encode(&bytes).into(), pixels, plane, center, diff --git a/crates/io/src/design.rs b/crates/io/src/design.rs index 9e3a040e2..9a5719e49 100644 --- a/crates/io/src/design.rs +++ b/crates/io/src/design.rs @@ -863,7 +863,7 @@ fn read_document(reader: &mut PackageReader) -> Result solidcraft_doc::canvas::MAX_CANVAS_BYTES { return Err(invalid("canvas too large")); } - canvas.data = solidcraft_doc::canvas::base64_encode(bytes); + canvas.data = solidcraft_doc::canvas::base64_encode(bytes).into(); } for (index, cloud) in doc.point_clouds.iter_mut().enumerate() { if !cloud.points.is_empty() || !cloud.colors.is_empty() { @@ -1636,7 +1636,7 @@ mod tests { id, name: "Reference".into(), format: "png".into(), - data: solidcraft_doc::canvas::base64_encode(b"original image bytes"), + data: solidcraft_doc::canvas::base64_encode(b"original image bytes").into(), pixels: [1, 1], plane: solidcraft_doc::PlaneRef::Origin { name: "XY".into() }, center: Vec2::new(0.0, 0.0),