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:
4 files changed
+29
-5
No files matched your search
@@ -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<str>,
|
||||
/// Pixel size of the image.
|
||||
pub pixels: [u32; 2],
|
||||
pub plane: PlaneRef,
|
||||
|
||||
@@ -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()));
|
||||
}
|
||||
@@ -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,
|
||||
|
||||
@@ -863,7 +863,7 @@ fn read_document<R: Read + Seek>(reader: &mut PackageReader<R>) -> Result<Docume
|
||||
if bytes.len() > 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),
|
||||
|
||||
Reference in new issue
Block a user