perf(format): avoid buffering compressed ZIP slices

Decode archive slices directly to remove a 32 KiB input allocation and its compressed-data copies. Preserve size limits and CRC checks, with an equivalence and corruption test. Remove the unused synthetic timing test and refresh the performance notes with public x86 evidence.
This commit is contained in:
Rene Leonhardt committed 2026-10-20 17:41:28 +02:00
1 parent a37198d4fa
commit 51788e6650
3 files changed
+55 -62

No files matched your search

+29 -1
View File
@@ -214,7 +214,7 @@ impl<'a> ZipReader<'a> {
// small forged archive must not reserve gigabytes up front, so the
// preallocation is capped (DEFLATE expands at most ~1032x).
let mut out = Vec::with_capacity(e.uncompressed.min(raw.len().saturating_mul(1032)).min(64 << 20));
flate2::read::DeflateDecoder::new(raw)
flate2::bufread::DeflateDecoder::new(raw)
.take((max as u64).saturating_add(1))
.read_to_end(&mut out)
.map_err(|err| FormatError::corrupt(format!("zip entry `{}`: {err}", e.name)))?;
@@ -327,6 +327,34 @@ mod tests {
assert!(r.read_by_name("a", 100).is_err());
}
#[test]
fn slice_deflate_matches_buffered_reader_and_preserves_checks() {
use std::io::Write as _;
let content = b"PhotoCraft ZIP payload".repeat(5000);
let mut encoder = flate2::write::DeflateEncoder::new(Vec::new(), flate2::Compression::none());
encoder.write_all(&content).unwrap();
let compressed = encoder.finish().unwrap();
assert!(compressed.len() > 32 * 1024);
let mut previous = Vec::new();
flate2::read::DeflateDecoder::new(compressed.as_slice()).read_to_end(&mut previous).unwrap();
let mut writer = ZipWriter::new();
writer.add("payload", &compressed).unwrap();
let bytes = writer.finish().unwrap();
let reader = ZipReader::new(&bytes).unwrap();
let mut entry = reader.entries[0].clone();
entry.method = 8;
entry.uncompressed = content.len();
entry.crc = crc32(&content);
assert_eq!(reader.read(&entry, content.len()).unwrap(), previous);
assert!(matches!(reader.read(&entry, content.len() - 1), Err(FormatError::LimitExceeded(_))));
entry.crc ^= 1;
assert!(matches!(reader.read(&entry, content.len()), Err(FormatError::Corrupt(_))));
entry.crc ^= 1;
entry.compressed /= 2;
assert!(matches!(reader.read(&entry, content.len()), Err(FormatError::Corrupt(_))));
}
/// A valid DEFLATE-compressed bundle may be read with an effectively
/// unlimited caller budget (e.g. corruption/round-trip checks). On 64-bit
/// targets `usize::MAX as u64 + 1` overflows, so the decoder must saturate.
+1 -49
View File
@@ -1,7 +1,7 @@
//! Backend compatibility, including native-format tile data rather than ZIP assembly alone.
mod common;
use std::{hint::black_box, io::Read, time::Instant};
use std::io::Read;
use photocraft_codecs::{ChannelLayout, Format, Image, SampleType as CodecSample};
use photocraft_color::{ColorMode, SampleType};
@@ -73,51 +73,3 @@ fn png_psd_and_pcraft_decode_identically() {
check_png(&read_thumbnail(&bytes).unwrap().unwrap(), &image);
}
}
#[test]
#[ignore = "manual 24 MP release benchmark; run on main and the backend branch"]
fn benchmark_deflate_formats() {
for pattern in ["ramp", "noise"] {
let mut doc = document(SampleType::U8, Size::new(6000, 4000));
if pattern == "noise" {
let mut seed = 0x9e3779b9u32;
let raw: Vec<u8> = (0..96_000_000usize)
.map(|i| {
seed ^= seed << 13;
seed ^= seed >> 17;
seed ^= seed << 5;
if i % 4 == 3 { 255 } else { (seed >> 24) as u8 }
})
.collect();
doc.layers[0].content = LayerContent::Raster(photocraft_raster::Surface::from_interleaved(doc.pixel_format(), doc.bounds(), &raw));
}
let pixels = doc.layers[0].surface().unwrap().read_region(doc.bounds());
let image = Image::from_normalized(6000, 4000, ChannelLayout::Rgba, CodecSample::U8, &pixels).unwrap();
drop(pixels);
let psd_opts = PsdExportOptions { merged_compression: Compression::Zip, merged_matte: false, ..Default::default() };
let save_opts = SaveOptions { thumbnail: Some(Rgba8Image::new(256, 170)), ..Default::default() };
let png = photocraft_codecs::encode(&image, Format::Png, &Default::default()).unwrap();
let psd = document_to_psd_with(&doc, &psd_opts).0.to_bytes().unwrap();
let pcraft = save_to_bytes(&doc, &save_opts).unwrap();
for trial in 0..6 {
for op in ["png-encode", "png-decode", "psd-encode", "psd-decode", "pcraft-save", "pcraft-load"] {
let start = Instant::now();
let size = match op {
"png-encode" => black_box(photocraft_codecs::encode(black_box(&image), Format::Png, &Default::default()).unwrap()).len(),
"png-decode" => black_box(photocraft_codecs::decode(black_box(&png)).unwrap()).data().len(),
"psd-encode" => black_box(document_to_psd_with(black_box(&doc), &psd_opts).0.to_bytes().unwrap()).len(),
"psd-decode" => {
black_box(import("bench.psd", black_box(&psd)).unwrap());
psd.len()
}
"pcraft-save" => black_box(save_to_bytes(black_box(&doc), &save_opts).unwrap()).len(),
_ => {
black_box(load_from_bytes(black_box(&pcraft)).unwrap());
pcraft.len()
}
};
println!("deflate,{pattern}-{op},{trial},{:.3},{size}", start.elapsed().as_secs_f64() * 1000.0);
}
}
}
}
+25 -12
View File
@@ -2,9 +2,19 @@
Replace the workspace flate2 Rust backend with zlib-rs and runtime CPU detection. Tiles in .pcraft remain Zstandard; deflate changes its PNG previews.
Retained with explicitly accepted tradeoffs. Some PSD ZIP files grow by up to 0.71%; the 16-bit city PSD import is about 6% slower in confirmation; the small web-layout PNG grows by 598 bytes (+1.40%); one .pcraft preview adds 25 bytes. Real photo PNGs become smaller and encode faster. Compression-effort probes recovered size only by introducing other speed regressions, so no effort change is applied. There is no native-format tile speedup from deflate.
Size, import-speed and dependency tradeoffs remain maintainer decisions.
Some PSD ZIP files grow by up to 0.71%. The macOS 16-bit city PSD import is about 6% slower in confirmation.
The small web-layout PNG grows by 598 bytes (+1.40%). One .pcraft preview adds 25 bytes.
Real photo PNGs become smaller and encode faster. Compression levels are unchanged.
There is no native-format tile speedup from deflate.
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.
Measured on 2026-10-19 against main `d95fdf31`, independently of the other performance changes.
Host: Apple M4 Max, 16 CPUs, 128 GiB RAM, macOS arm64, rustc 1.99.1.
Release settings: optimization level 3, no LTO, 16 codegen units and no debug information.
Builds finish before measurement. Measurements use default priority without concurrent builds or profiling.
Five alternating pairs follow a warmup. Confirmation uses nine pairs.
Time changes compare medians, and intervals resample paired ratios. Negative changes mean less elapsed time.
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.
@@ -59,15 +69,18 @@ The shared harness times in-memory operations. Input loading, image conversion,
Every PNG encode decodes to identical source pixels, each PSD operation produces the same decoded layer/mask samples as main, and complete .pcraft documents compare equal at every depth. Compressed bytes may differ. The committed compatibility test independently inflates PNG IDAT and PSD ZIP/ZIP-prediction data using old miniz_oxide and checks .pcraft tiles plus thumbnails.
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.
## Linux and Windows x86-64 validation
## Earlier split validation
[GitHub Actions run](https://github.com/reneleonhardt/photocraft/actions/runs/39444647224) passed the deflate compatibility test,
the full IO/codecs/format package tests and `cargo xtask wasm` on Ubuntu 24.04 and Windows 2022 x86-64.
The temporary `ci/deflate-zlib-rs-x86` branch at `46232997` contains the workflow and benchmark setup.
The run's `deflate-x86-results` artifact contains all raw samples, the full table, input and binary hashes, backend features and host details.
These checks passed when the split was prepared; they were not all rerun for this benchmark review. Current affected checks are listed above where production code changed.
- `cargo test -p photocraft-codecs -p photocraft-psd -p photocraft-format -p photocraft-io --locked`: passed (1956 passed, 9 ignored).
- `cargo clippy -p photocraft-codecs -p photocraft-psd -p photocraft-format -p photocraft-io --locked --all-targets -- -D warnings`: passed.
- `cargo xtask layers`: passed.
- `cargo xtask wasm`: passed.
- `cargo xtask test-corpus -p psd -p codecs -p io`: passed (1853 passed, 9 ignored).
- The compatibility test also passed with the main backend before measurements. To reproduce the main comparison, copy only this test and its miniz_oxide dev-dependency into a clean main worktree.
The timing host used an Intel Xeon 6973P-C with four vCPUs and rustc 1.99.1.
Main at `d95fdf31` and the backend change at `a37198d4` used matching release settings.
One warmup preceded nine alternating pairs on the same 12/24 MP photos at U8/U16.
Both importers received the same baseline-encoded PSD files. Loading, conversion, hashes and checks stayed outside the elapsed-time measurements.
PNG encode time fell 23.8–56.8%, PSD export time 18.1–58.2%, and PSD import time 7.8–21.6%.
The 16-bit city import fell from 440.733 to 402.057 ms (-8.78%); the macOS slowdown did not recur on this runner.
Photo PNGs became 1.2–2.1% smaller. Exported PSDs grew by 0.014–0.709%.
All decoded pixels, dimensions, depths and profiles matched. These results cover this Linux host; Windows and F32 were not timed.