Skip extra evaluation when identical

This commit is contained in:
Andrew 2024-02-20 18:54:49 +13:00 committed by andrews05
parent e503a5c8f9
commit 3e3c027cd0

View file

@ -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 {