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<str>, 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 <noreply@anthropic.com>

---------

Co-authored-by: Markus Kaltenbrunner <kaltenbrunnermarkus@users.noreply.github.com>
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
kaltenbrunnermarkus
2026-11-01 09:46:17 +00:00
committed by GitHub
parent 7702316039
commit fbb00dc7b2
4 changed files with 29 additions and 5 deletions
+22
View File
@@ -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()));
}
+1 -1
View File
@@ -67,7 +67,7 @@ fn make(s: &mut Session, p: &Value, cmd: &str, decal: bool) -> Result<Value> {
id,
name: name.clone(),
format: format.into(),
data: base64_encode(&bytes),
data: base64_encode(&bytes).into(),
pixels,
plane,
center,