From a0983e610588e58edef132148de07f3710cee657 Mon Sep 17 00:00:00 2001 From: Brian Torres Date: Mon, 19 Oct 2026 11:44:20 -0700 Subject: [PATCH] Free Transform: the warp writes each tile as it finishes (#7807) Every finished tile kept its f32 pixels until the last one was done, 16 bytes a pixel of an RGBA result on top of the result itself. Writing each tile as soon as it is warped holds only the tiles in flight. 100 MP 8-bit layer (release, M4 Max, 16 cores), extra memory per result pixel and time: scale 110 % 16.4 -> 4.8 B, 0.8 -> 0.6 s; rotate 15 deg 12.5 -> 3.4 B, 0.8 -> 0.5 s; 90 % + 15 deg bilinear 12.7 -> 3.4 B, 0.5 -> 0.3 s; 90 % + 15 deg bicubic 19.9 -> 12.4 B (its pre-reduced copy of the source), 1.0 -> 0.8 s. Co-Authored-By: Claude Opus 5.5 --- crates/algo/src/transform.rs | 36 ++++++++++++++++-------------------- 1 file changed, 16 insertions(+), 20 deletions(-) diff --git a/crates/algo/src/transform.rs b/crates/algo/src/transform.rs index 91260bd285e..2a25a02d84c 100644 --- a/crates/algo/src/transform.rs +++ b/crates/algo/src/transform.rs @@ -294,7 +294,7 @@ fn warp( converted = src.try_convert(fmt)?; &converted }; - let mut out = Surface::new(fmt); + let out = Surface::new(fmt); if src_rect.is_empty() || clip.is_some_and(|c| c.is_empty()) { return Ok(out); } @@ -366,7 +366,7 @@ fn warp( return Ok(None); } let k = finished.fetch_add(1, std::sync::atomic::Ordering::Relaxed) + 1; - // The last few percent are the writes. + // Short of 100 %: the result is still pruned and handed back after the last tile. ctl.progress(0.98 * k as f32 / total as f32); // Source footprint of this tile (inverse-mapped corners of its sampled part), padded for the filter. let tc = [(t.x0, t.y0), (t.x1, t.y0), (t.x1, t.y1), (t.x0, t.y1)].map(|(x, y)| (x as f64, y as f64)); @@ -503,33 +503,29 @@ fn warp( } Ok(any.then_some((*t, outp))) }; - let result_bytes = tiles.len().checked_mul(std::mem::size_of::<(Rect, Vec)>()).ok_or(photocraft_raster::AllocationError::SizeOverflow)?; - let mut done = Vec::new(); - done.try_reserve_exact(tiles.len()).map_err(|_| photocraft_raster::AllocationError::NotEnoughMemory { bytes: result_bytes })?; - let done = std::sync::Mutex::new(done); + // Each tile is written as soon as it is warped, so only the tiles in flight hold their `f32` + // pixels: kept until the last one was done, they cost 16 bytes a pixel of an RGBA result on + // top of the result's own (#7807). + let out = std::sync::Mutex::new(out); + let warp_and_write = |tile: &Rect| -> Result<(), photocraft_raster::AllocationError> { + if let Some((r, v)) = work(tile)? { + out.lock().unwrap_or_else(std::sync::PoisonError::into_inner).try_write_region(r, &v)?; + } + Ok(()) + }; #[cfg(not(target_arch = "wasm32"))] { use rayon::prelude::*; - tiles.into_par_iter().try_for_each(|tile| -> Result<(), photocraft_raster::AllocationError> { - if let Some(result) = work(&tile)? { - done.lock().unwrap_or_else(std::sync::PoisonError::into_inner).push(result); - } - Ok(()) - })?; + tiles.into_par_iter().try_for_each(|tile| warp_and_write(&tile))?; } #[cfg(target_arch = "wasm32")] for tile in tiles { - if let Some(result) = work(&tile)? { - done.lock().unwrap_or_else(std::sync::PoisonError::into_inner).push(result); - } + warp_and_write(&tile)?; } - let done = done.into_inner().unwrap_or_else(std::sync::PoisonError::into_inner); if ctl.cancelled() { - return Ok(out); - } - for (r, v) in done { - out.try_write_region(r, &v)?; + return Ok(Surface::new(fmt)); } + let mut out = out.into_inner().unwrap_or_else(std::sync::PoisonError::into_inner); out.prune(); Ok(out) }