Skip to content

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

perf(stack): one loop per statistic, 1.3–2.2× faster on one thread

Nicolas Falesy9ff27ec204b6
diff --git a/crates/algo/src/stack.rs b/crates/algo/src/stack.rs
index 512e169c5..dbb5b22e6 100644
--- a/crates/algo/src/stack.rs
+++ b/crates/algo/src/stack.rs
@@ -141,6 +141,27 @@ const CHUNK: usize = 4096;
/// Combines `layers` (each `len` pixels) with statistic `s`. Colour results retain their float
/// range; quantisation/clipping belongs to the destination surface, not the statistic.
pub fn combine(layers: &[Vec<[f32; 4]>], s: Stat) -> Vec<[f32; 4]> {
+ // Pick the statistic once, outside the pixel loop: each arm passes `stat` a constant mode, so
+ // the compiler builds one loop per statistic with the `match` folded away. Matching on `s`
+ // three times per pixel inside one shared loop made one thread 5–15% slower than the old
+ // serial loop (Mean, Variance, StdDev, Skewness, Kurtosis); this alone cuts Mean's
+ // instructions by a fifth.
+ match s {
+ Stat::Entropy => combine_with(layers, |v, sc| stat(v, Stat::Entropy, sc)),
+ Stat::Kurtosis => combine_with(layers, |v, sc| stat(v, Stat::Kurtosis, sc)),
+ Stat::Maximum => combine_with(layers, |v, sc| stat(v, Stat::Maximum, sc)),
+ Stat::Mean => combine_with(layers, |v, sc| stat(v, Stat::Mean, sc)),
+ Stat::Median => combine_with(layers, |v, sc| stat(v, Stat::Median, sc)),
+ Stat::Minimum => combine_with(layers, |v, sc| stat(v, Stat::Minimum, sc)),
+ Stat::Range => combine_with(layers, |v, sc| stat(v, Stat::Range, sc)),
+ Stat::Skewness => combine_with(layers, |v, sc| stat(v, Stat::Skewness, sc)),
+ Stat::StandardDeviation => combine_with(layers, |v, sc| stat(v, Stat::StandardDeviation, sc)),
+ Stat::Summation => combine_with(layers, |v, sc| stat(v, Stat::Summation, sc)),
+ Stat::Variance => combine_with(layers, |v, sc| stat(v, Stat::Variance, sc)),
+ }
+}
+
+fn combine_with(layers: &[Vec<[f32; 4]>], f: impl Fn(&mut [f32], &mut EntropyScratch) -> f32 + Sync + Send) -> Vec<[f32; 4]> {
let len = layers.iter().map(Vec::len).max().unwrap_or(0);
let mut out = vec![[0.0f32; 4]; len];
// Every output pixel depends only on the input pixels at the same index, so chunks of the
@@ -150,22 +171,24 @@ pub fn combine(layers: &[Vec<[f32; 4]>], s: Stat) -> Vec<[f32; 4]> {
crate::photo_util::par_rows(&mut out, CHUNK, 1, |chunk, outs| {
// Scratch is per chunk, not per pixel: the old loop's three `Vec`s were already reused
// across pixels, so keep that, and give each task its own set.
- let mut samples: [Vec<f32>; 3] = [0, 1, 2].map(|_| Vec::with_capacity(layers.len()));
+ // Sized to the layer count up front and filled by index (`k` samples per pixel): a
+ // `push` per sample re-checked the capacity and stored the length every time; indexing
+ // cuts the loop's instructions by another third.
+ let mut samples: [Vec<f32>; 3] = [0, 1, 2].map(|_| vec![0.0; layers.len()]);
let mut scratch = EntropyScratch::new();
let base = chunk * CHUNK;
for (j, o) in outs.iter_mut().enumerate() {
let i = base + j;
- for v in samples.iter_mut() {
- v.clear();
- }
+ let mut k = 0;
let mut alpha = 0.0f32;
for l in layers {
if let Some(p) = l.get(i)
&& p[3] > 0.0
{
for c in 0..3 {
- samples[c].push(p[c]);
+ samples[c][k] = p[c];
}
+ k += 1;
alpha = alpha.max(p[3]);
}
}
@@ -173,7 +196,7 @@ pub fn combine(layers: &[Vec<[f32; 4]>], s: Stat) -> Vec<[f32; 4]> {
continue;
}
for c in 0..3 {
- o[c] = stat(&mut samples[c], s, &mut scratch);
+ o[c] = f(&mut samples[c][..k], &mut scratch);
}
o[3] = alpha;
}