Skip to content

Commit

Permalink
Skip extra evaluation when identical
Browse files Browse the repository at this point in the history
  • Loading branch information
andrews05 committed Mar 11, 2024
1 parent e503a5c commit 3e3c027
Showing 1 changed file with 12 additions and 11 deletions.
23 changes: 12 additions & 11 deletions src/reduction/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 3e3c027

Please sign in to comment.