From 3e3c027cd047ab7d351ccf833d193cf5fece2441 Mon Sep 17 00:00:00 2001 From: Andrew Date: Tue, 20 Feb 2024 18:54:49 +1300 Subject: [PATCH] Skip extra evaluation when identical --- src/reduction/mod.rs | 23 ++++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/src/reduction/mod.rs b/src/reduction/mod.rs index 1c5ee6c1..e3f59bf2 100644 --- a/src/reduction/mod.rs +++ b/src/reduction/mod.rs @@ -143,22 +143,23 @@ pub(crate) fn perform_reductions( // Attempt to reduce to a lower bit depth if opts.bit_depth_reduction && !deadline.passed() { - // First try reducing the `png` var (could be grayscale or indexed) + // First try the `png` var let reduced = reduced_bit_depth_8_or_less(&png); - // It's possible that an indexed version will be smaller (it might reach a lower depth), - // but grayscale is usually better so we'll skip it if we're doing cheap evaluations and - // already have a reduced image. - let try_indexed = !cheap || reduced.is_none(); + // Then try the `indexed` var, unless we're doing cheap evaluations and already have a reduction + if (!cheap || reduced.is_none()) && !deadline.passed() { + if let Some(indexed) = indexed.and_then(|png| reduced_bit_depth_8_or_less(&png)) { + // Only evaluate this if it's different from the first result (which must be grayscale if it exists) + if reduced.as_ref().map_or(true, |r| r.data != indexed.data) { + eval.try_image(Arc::new(indexed)); + evaluation_added = true; + } + } + } + // Enter the first result into the evaluator if let Some(reduced) = reduced { eval.try_image(Arc::new(reduced)); evaluation_added = true; } - if try_indexed && !deadline.passed() { - if let Some(reduced) = indexed.and_then(|png| reduced_bit_depth_8_or_less(&png)) { - eval.try_image(Arc::new(reduced)); - evaluation_added = true; - } - } } if evaluation_added {