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 <[email protected]>
This commit is contained in:
Brian TorresandClaude Opus 5.5 committed 2026-10-19 12:10:15 -07:00
1 parent 818de14222
commit a0983e6105
1 file changed
+16 -20
+16 -20
View File
@@ -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<f32>)>()).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)
}