Skip to content

PhotoCraft: An open-source, photo editor written by scratch in pure Rust

perf(format): drop unhelpful Zstandard workspace reuse

Rene Leonhardtf64a143c5593
diff --git a/crates/format/src/store.rs b/crates/format/src/store.rs
index 96647d096..7da5dd6b5 100644
--- a/crates/format/src/store.rs
+++ b/crates/format/src/store.rs
@@ -26,7 +26,9 @@ fn blob_path(h: &str) -> String {
format!("blobs/{h}.zst")
}
-type Compressor<'a> = ruzstd::encoding::FrameCompressor<std::io::Cursor<std::borrow::Cow<'a, [u8]>>, Vec<u8>, ruzstd::encoding::MatchGeneratorDriver>;
+fn compress(data: &[u8]) -> Vec<u8> {
+ ruzstd::encoding::compress_to_vec(data, ruzstd::encoding::CompressionLevel::Fastest)
+}
/// Decodes a zstd frame of at most `max` bytes (`LimitExceeded` beyond). With `exact`, the
/// decoded length must be exactly that (`Corrupt` otherwise).
@@ -414,16 +416,8 @@ enum Object {
}
impl Object {
- fn compressed<'a>(&'a self, compressor: &mut Compressor<'a>) -> Vec<u8> {
- let bytes = self.content();
- // ruzstd's pooled suffix tables retain the first block's size.
- if !bytes.len().is_multiple_of(128 << 10) {
- return ruzstd::encoding::compress_to_vec(bytes.as_ref(), ruzstd::encoding::CompressionLevel::Fastest);
- }
- compressor.set_source(std::io::Cursor::new(bytes));
- compressor.set_drain(Vec::new());
- compressor.compress();
- compressor.set_drain(Vec::new()).unwrap_or_default()
+ fn compressed(&self) -> Vec<u8> {
+ compress(&self.content())
}
/// The bytes this object stores (tiles little-endian).
@@ -695,20 +689,18 @@ type Compressed<'a> = (&'a String, Arc<Vec<u8>>);
fn par_compress<'a>(objects: &[(&'a String, &'a Object)]) -> Result<Vec<Compressed<'a>>> {
let threads = if cfg!(target_arch = "wasm32") { 1 } else { std::thread::available_parallelism().map_or(1, |n| n.get()).clamp(1, 32) };
if threads < 2 || objects.len() < 2 {
- let mut compressor = Compressor::new(ruzstd::encoding::CompressionLevel::Fastest);
- return Ok(objects.iter().map(|(p, o)| (*p, Arc::new(o.compressed(&mut compressor)))).collect());
+ return Ok(objects.iter().map(|(p, o)| (*p, Arc::new(o.compressed()))).collect());
}
let next = std::sync::atomic::AtomicUsize::new(0);
std::thread::scope(|sc| {
let hs: Vec<_> = (0..threads.min(objects.len()))
.map(|_| {
sc.spawn(|| {
- let mut compressor = Compressor::new(ruzstd::encoding::CompressionLevel::Fastest);
let mut out = Vec::new();
loop {
let i = next.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
let Some((p, o)) = objects.get(i) else { break };
- out.push((*p, Arc::new(o.compressed(&mut compressor))));
+ out.push((*p, Arc::new(o.compressed())));
}
out
})
@@ -801,7 +793,7 @@ impl PcraftWriter {
Object::Tile(..) => stats.tiles_written += 1,
Object::Blob(_) => stats.blobs_written += 1,
}
- fresh.remove(path).unwrap_or_else(|| Arc::new(obj.compressed(&mut Compressor::new(ruzstd::encoding::CompressionLevel::Fastest))))
+ fresh.remove(path).unwrap_or_else(|| Arc::new(obj.compressed()))
}
};
z.add(path, &data)?;
@@ -836,7 +828,6 @@ impl PcraftWriter {
None => 0,
};
let recheck = self.rolling_recheck(&p.objects);
- let mut compressor = Compressor::new(ruzstd::encoding::CompressionLevel::Fastest);
for (path, obj) in &p.objects {
let object_path = dir.join(path);
// bundle_root and list_objects validated the fixed parent directories once. Check
@@ -871,7 +862,7 @@ impl PcraftWriter {
Object::Tile(..) => stats.tiles_written += 1,
Object::Blob(_) => stats.blobs_written += 1,
}
- write_bundle_file(dir, &object_path, &obj.compressed(&mut compressor))?;
+ write_bundle_file(dir, &object_path, &obj.compressed())?;
let written = path_signature(&object_path)?;
if let Some(cache) = &mut self.verified_directory {
match written {
@@ -1020,53 +1011,3 @@ pub(crate) fn write_atomic(path: &Path, data: &[u8]) -> Result<()> {
crate::atomic::atomic_write(path, data)?;
Ok(())
}
-
-#[cfg(test)]
-mod tests {
- use super::*;
-
- #[test]
- #[ignore = "manual 24 MP RGBA8-equivalent release benchmark"]
- fn benchmark_workspace_reuse() {
- use std::{hint::black_box, time::Instant};
- let data: Vec<_> = (0..367).map(|seed| Arc::new((0..262_144).map(|i| ((i * 37 + i / 97 + seed * 13) % 251) as u8).collect())).collect();
- let objects: Vec<_> = data.into_iter().map(Object::Blob).collect();
- for trial in 0..6 {
- let mut paths = ["main", "split"];
- if trial % 2 == 1 {
- paths.reverse();
- }
- for name in paths {
- let t = Instant::now();
- let mut compressor = Compressor::new(ruzstd::encoding::CompressionLevel::Fastest);
- let bytes: usize = objects
- .iter()
- .map(|o| {
- let encoded = if name == "main" {
- ruzstd::encoding::compress_to_vec(o.content().as_ref(), ruzstd::encoding::CompressionLevel::Fastest)
- } else {
- o.compressed(&mut compressor)
- };
- black_box(encoded).len()
- })
- .sum();
- println!("workspace,24mp-rgba8,{trial},{name},{:.6},{bytes}", t.elapsed().as_secs_f64() * 1000.0);
- }
- }
- }
-
- #[test]
- fn reused_compressor_produces_independent_frames() {
- let data: Vec<Arc<Vec<u8>>> = [0, 1, 8, 131_072, 0, 262_144, 262_147, 131_072, 1]
- .into_iter()
- .map(|n| Arc::new((0..n).map(|i| ((i * 37 + i / 97) % 251) as u8).collect()))
- .collect();
- let objects: Vec<_> = data.iter().cloned().map(Object::Blob).collect();
- let mut compressor = Compressor::new(ruzstd::encoding::CompressionLevel::Fastest);
- for (object, bytes) in objects.iter().zip(&data) {
- let encoded = object.compressed(&mut compressor);
- assert_eq!(encoded, ruzstd::encoding::compress_to_vec(bytes.as_slice(), ruzstd::encoding::CompressionLevel::Fastest));
- assert_eq!(decompress(&encoded, bytes.len(), "test").unwrap(), **bytes);
- }
- }
-}
diff --git a/docs/zstd-workspaces-performance.md b/docs/zstd-workspaces-performance.md
index 9952edc64..25b7e0485 100644
--- a/docs/zstd-workspaces-performance.md
+++ b/docs/zstd-workspaces-performance.md
@@ -1,37 +1,33 @@
# Rust Zstandard workspace reuse
-Reuse the existing pure-Rust compressor workspace per save worker. Partial-block objects keep the fresh-compressor fallback. No new backend or C dependency.
-
-Fresh real-document saves are close to neutral. Reusing compressor allocation does not establish a meaningful end-to-end save improvement on these inputs. Every bundle remains byte-identical.
-
-Measured on 2026-10-19 against main `d95fdf3181c01ad41e95456091a394b105a9afb2`, independently of the other splits. Apple M4 Max, 16 CPUs, 128 GiB, macOS arm64, rustc 1.99.1; release O3, LTO off, 16 codegen units, debug off. Builds used nice 10 and warm APFS-cloned caches; measurements used default priority without concurrent builds or profiling. Five alternating paired samples follow a warmup; confirmation and fix runs use nine pairs. Time change compares the two medians; intervals resample paired ratios. Negative time change means faster. Small differences are not automatically established improvements or regressions.
-
-The full-resolution CC0 Pixls photographs are P1000475.RW2 (24 MP foliage centre crop, no resampling) and IMG_4059.CR2 (12 MP city), each converted to U8/U16/F32 with the ProPhoto RGB profile. F32 derives from 16-bit development, not HDR capture. MIT psd-tools masks.psd supplies a 640 x 1394 web layout with 22 imported layers and three masks; mask-index.psd supplies a 1000 x 867 illustrated card with a mask. The sources and hashes are pinned by the existing corpus. These are the available authored layered files, not a large layered-photo corpus. No generated noise, repeating ramps, enlarged charts or duplicated photo layers are used.
-
-The shared harness times in-memory operations. Input loading, image conversion, thumbnail rendering, hashes and equivalence checks are outside timers. Every save uses a fresh PcraftWriter and a real rendered thumbnail. Common reads use main-encoded files; own-output reads are separate. PSD exports use ZIP merged images at U8/U16, Raw merged images at F32, and normal RLE layer channels at U8/U16 / ZIP prediction at F32. Results do not measure filesystem throughput or browser performance.
-
-| Document | Operation | Main ms | Branch ms | Time change | Paired 95% interval | Main / branch bytes |
-|---|---|---:|---:|---:|---:|---:|
-| panasonic-8 | save | 163.156 | 162.549 | -0.4% | -1.4..+0.1% | 49,643,217 / 49,643,217 |
-| panasonic-8 | load-own | 396.986 | 397.099 | +0.0% | -0.2..+1.1% | 49,643,217 / 49,643,217 |
-| panasonic-16 | save | 355.760 | 353.995 | -0.5% | -0.9..+0.2% | 175,681,852 / 175,681,852 |
-| panasonic-16 | load-own | 386.774 | 386.373 | -0.1% | -0.9..+1.0% | 175,681,852 / 175,681,852 |
-| panasonic-32 | save | 627.383 | 630.287 | +0.5% | -0.6..+2.2% | 252,231,671 / 252,231,671 |
-| panasonic-32 | load-own | 1320.923 | 1320.769 | -0.0% | -0.1..+0.7% | 252,231,671 / 252,231,671 |
-| canon-8 | save | 86.768 | 86.708 | -0.1% | -0.9..+1.8% | 26,151,631 / 26,151,631 |
-| canon-8 | load-own | 221.917 | 224.088 | +1.0% | -1.2..+1.2% | 26,151,631 / 26,151,631 |
-| canon-16 | save | 185.739 | 186.190 | +0.2% | -1.5..+0.9% | 88,750,003 / 88,750,003 |
-| canon-16 | load-own | 198.518 | 198.190 | -0.2% | -0.8..+1.0% | 88,750,003 / 88,750,003 |
-| canon-32 | save | 335.712 | 338.159 | +0.7% | -1.1..+2.0% | 120,957,984 / 120,957,984 |
-| canon-32 | load-own | 787.610 | 786.982 | -0.1% | -1.8..+2.3% | 120,957,984 / 120,957,984 |
-| layout-masks | save | 18.907 | 19.230 | +1.7% | -1.7..+4.0% | 433,949 / 433,949 |
-| layout-masks | load-own | 16.310 | 16.411 | +0.6% | -2.6..+3.3% | 433,949 / 433,949 |
-| layout-mask-index | save | 7.598 | 7.446 | -2.0% | -7.7..+6.3% | 344,892 / 344,892 |
-| layout-mask-index | load-own | 6.848 | 6.840 | -0.1% | -2.6..+3.7% | 344,892 / 344,892 |
-
-Every saved real bundle is byte-identical to main and complete documents round-trip equal. The committed unit oracle compares reused frames with fresh compression, including partial blocks and size transitions.
-
-Local raw evidence and the shared harness: `.local/perf-real-261019/` in the repository checkout (ignored). This comparison supersedes the earlier synthetic performance table. Small synthetic unit fixtures remain correctness checks; ignored synthetic timing tests were not run.
+Removed locally: complete layered-photo ZIP save changes range from -1.9% to +0.2%. An apparent 4.8% gain in one 16-bit APFS directory save failed independent confirmation, which was 3.2% slower. The other directory cases are neutral. There is no repeatable meaningful whole-operation gain, so store.rs, including its added workspace tests and synthetic microbenchmark, is restored byte for byte to main. The original local tip is recoverable at refs/archive/perf/zstd-workspaces-PRE-LAYERED-REVIEW. Only the performance report remains different from main on this branch.
+
+Measured on 2026-10-19 against frozen main `d95fdf3181c01ad41e95456091a394b105a9afb2`, independently for each branch. Apple M4 Max, 16 CPUs, 128 GiB, macOS arm64, rustc 1.99.1. Release O3, LTO off, 16 codegen units, debug off; builds/tests at nice 10 with warm caches. Timed operations run at normal priority without concurrent builds or profiling. Persistent processes alternate execution order after a warmup. Time change compares the two medians; paired bootstrap intervals from these small samples describe variation, not guarantees.
+
+CC0 Pixls P1000475.RW2 supplies a 6000 x 4000 centre crop (24 MP, no resampling); IMG_4059.CR2 supplies the native 4000 x 3000 city photograph. U8/U16/F32 documents retain ProPhoto RGB; F32 derives from 16-bit development and is not native HDR. No generated noise, repeated ramps, enlarged charts or duplicated photo layers are timed. Source pins and attribution remain in the existing corpus.
+
+Each constructed photo edit has four visible layers: the real raster photograph; global Levels (black 2/255, white 250/255, gamma 1.08); Curves with master points (0,0), (0.25,0.22), (0.5,0.52), (0.75,0.80), (1,1) plus red midtone 0.5→0.515; and foreground Levels (gamma 1.15) through a smooth grayscale mask fading from zero to one over 35–75% of image height. These are authored benchmark edits of licensed photos, not Photoshop-authored oracle files. Saved previews were inspected.
+
+Complete full-resolution CPU rendering and fresh ZIP saves use seven alternating pairs. Thumbnail rendering, hashing and document equality are outside timers. Every render must match main float bit for float bit; every save must match main byte for byte and reload to the complete source document. Directory saves also use seven pairs and fresh directories with a fresh writer; an independent confirmation uses nine pairs. Directory creation/removal, output hashing and full reload equality are outside the timer. These directory timings include APFS writes and fsync, unlike the in-memory ZIP timings. These are CPU/core measurements, not GPU or browser performance claims.
+
+| Document | Operation | Main ms | Branch ms | Change | Paired 95% interval |
+|---|---|---:|---:|---:|---:|
+| panasonic-8 | save | 167.893 | 165.025 | -1.7% | -5.3..-0.6% |
+| panasonic-16 | save | 371.857 | 364.765 | -1.9% | -3.0..+1.8% |
+| panasonic-32 | save | 641.312 | 635.064 | -1.0% | -3.0..+4.3% |
+| canon-8 | save | 93.469 | 93.592 | +0.1% | -0.8..+1.0% |
+| canon-16 | save | 185.048 | 185.385 | +0.2% | -0.2..+4.3% |
+| canon-32 | save | 327.223 | 326.519 | -0.2% | -0.2..+2.4% |
+| panasonic-8 | save-dir | 5537.227 | 5534.940 | -0.0% | -7.1..+2.2% |
+| panasonic-16 | save-dir | 7077.156 | 6739.019 | -4.8% | -5.3..-0.3% |
+| canon-32 | save-dir | 3979.360 | 3974.353 | -0.1% | -0.9..+1.6% |
+| panasonic-16 | save-dir (confirm) | 6483.861 | 6691.230 | +3.2% | +1.5..+9.2% |
+
+The earlier eight flat-photo/authored-design comparisons were also close to neutral for fresh ZIP saves. All candidate bundles and directory contents match main byte for byte. Retention required a repeatable improvement of at least 3% in a complete operation, with a paired interval excluding zero and no material new regression. Kernel-only differences do not satisfy that criterion.
+
+Post-removal affected checks passed: three RGB ZIP depth round trips; directory round trips across the existing mode/depth matrix; ZIP incremental saves; directory incremental saves and garbage collection (six tests total). Strict all-target format Clippy, affected format WASM and formatting checks also passed. No unrelated full-suite or synthetic timing run was repeated.
+
+Harness, raw observations, exact inputs and binary hashes: `.local/perf-followup-261019/` (ignored local evidence). Previous flat-photo and authored-design results remain in `.local/perf-real-261019/`. Nothing was pushed and no PR was changed.
## Earlier split validation