From 9722d16909ae5afc257d8543f93ca78569ec70ca Mon Sep 17 00:00:00 2001 From: Khawar Zamman Wani Date: Tue, 27 Oct 2026 13:19:08 +0530 Subject: [PATCH] perf(selection): retain direct Replace path and tighten validation --- crates/algo/src/selection.rs | 12 +++++++++--- crates/algo/src/selection/combine_mask.rs | 11 +++++++++-- crates/algo/src/selection/combine_mask/tests.rs | 13 +++++++++++-- 3 files changed, 29 insertions(+), 7 deletions(-) diff --git a/crates/algo/src/selection.rs b/crates/algo/src/selection.rs index ea1f6dce4e4..7c59167b3c0 100644 --- a/crates/algo/src/selection.rs +++ b/crates/algo/src/selection.rs @@ -142,8 +142,14 @@ pub fn combine(old: Option<&Surface>, new: &[f32], area: Rect, mode: SelectionMo if area.is_empty() { return None; } - if old.is_none_or(|s| s.format() == PixelFormat::GRAY8) { - return combine_mask::gray8(old, new, area, mode, false); + // Replace already writes straight from the input mask to tiles; it needs no old mask. + if mode == SelectionMode::Replace || (old.is_none() && mode == SelectionMode::Add) { + let s = mask_to_surface(new, area); + return (s.tile_count() > 0).then_some(s); + } + let old = old?; + if old.format() == PixelFormat::GRAY8 { + return combine_mask::gray8(Some(old), new, area, mode, false); } // Quantize to the 8-bit grid selections are stored on, so combining a mask with itself is // exact. Below half an 8-bit step is nothing. @@ -151,7 +157,7 @@ pub fn combine(old: Option<&Surface>, new: &[f32], area: Rect, mode: SelectionMo let s = match mode { SelectionMode::Replace => surface_from_masks(area, new, None, |_, v| v), _ => { - let prev = mask_from_surface(old, area); + let prev = mask_from_surface(Some(old), area); match mode { SelectionMode::Add => surface_from_masks(area, new, Some(&prev), |p, v| p.max(q(v))), SelectionMode::Subtract => surface_from_masks(area, new, Some(&prev), |p, v| (p - q(v)).max(0.0)), diff --git a/crates/algo/src/selection/combine_mask.rs b/crates/algo/src/selection/combine_mask.rs index 9e3a9f3a209..bbe4c657384 100644 --- a/crates/algo/src/selection/combine_mask.rs +++ b/crates/algo/src/selection/combine_mask.rs @@ -13,7 +13,7 @@ use std::sync::Arc; /// and Intersect return only the selection inside the band. Missing mask samples read zero. pub fn combine_band(old: Option<&Surface>, mask: &[f32], band: Rect, mode: SelectionMode) -> Option { let keep = matches!(mode, SelectionMode::Add | SelectionMode::Subtract); - if !keep { + if !keep || old.is_none() { return combine(old, mask, band, mode); } if band.is_empty() { @@ -27,7 +27,14 @@ pub fn combine_band(old: Option<&Surface>, mask: &[f32], band: Rect, mode: Selec let inside = combine(old, mask, band, mode); let mut out = old.cloned()?; let values = inside.map_or_else(|| vec![0.0; band.width() as usize * band.height() as usize], |s| s.read_region(band)); - out.write_region(band, &values); + if out.channels() == 1 { + out.write_region(band, &values); + } else { + // A selection uses its first channel as coverage. Broadcast it when writing back to + // an unusual multichannel surface, rather than passing a short buffer to write_region. + let pixels: Vec = values.iter().flat_map(|v| std::iter::repeat_n(*v, out.channels())).collect(); + out.write_region(band, &pixels); + } out.prune(); (out.tile_count() > 0).then_some(out) } diff --git a/crates/algo/src/selection/combine_mask/tests.rs b/crates/algo/src/selection/combine_mask/tests.rs index 65e06f3e004..5c55d9d90ab 100644 --- a/crates/algo/src/selection/combine_mask/tests.rs +++ b/crates/algo/src/selection/combine_mask/tests.rs @@ -32,7 +32,7 @@ fn reference_band(old: Option<&Surface>, mask: &[f32], area: Rect, mode: Selecti if area.is_empty() { return Some(old.clone()); } - let values = mask_from_surface(inside.as_ref(), area); + let values = inside.map_or_else(|| vec![0.0; area.width() as usize * area.height() as usize], |s| s.read_region(area)); let mut out = old.clone(); out.write_region(area, &values); out.prune(); @@ -88,6 +88,15 @@ fn unusual_selection_formats_keep_their_float_behavior() { assert_eq!(combine(Some(&old), &mask, area, mode), reference(Some(&old), &mask, area, mode)); if color == ColorMode::Grayscale { assert_eq!(combine_band(Some(&old), &mask, area, mode), reference_band(Some(&old), &mask, area, mode)); + } else { + let out = combine_band(Some(&old), &mask, area, mode).unwrap(); + let inside = reference(Some(&old), &mask, area, mode).unwrap(); + let expected = inside.pixel(0, 0)[0]; + let encoded = Surface::with_default(out.format(), &vec![expected; out.channels()]); + assert_eq!(out.pixel(0, 0), encoded.pixel(0, 0)); + if matches!(mode, SelectionMode::Add | SelectionMode::Subtract) { + assert_eq!(out.pixel(20, 20), old.pixel(20, 20)); + } } } } @@ -143,7 +152,7 @@ fn gray8_combining_release_comparison() { for (case, size) in [("sparse", 50), ("dense", 4000), ("full", 6000)] { let mut mask = vec![0.0; 6000 * 6000]; let start = (6000 - size) / 2; - for row in mask.chunks_exact_mut(6000).skip(start).take(size) { + for row in mask.as_chunks_mut::<6000>().0.iter_mut().skip(start).take(size) { row[start..start + size].fill(0.5); } for mode in MODES {