Skip to content

Commit 3e3c027

Browse files
committed
Skip extra evaluation when identical
1 parent e503a5c commit 3e3c027

File tree

1 file changed

+12
-11
lines changed

1 file changed

+12
-11
lines changed

src/reduction/mod.rs

+12-11
Original file line numberDiff line numberDiff line change
@@ -143,22 +143,23 @@ pub(crate) fn perform_reductions(
143143

144144
// Attempt to reduce to a lower bit depth
145145
if opts.bit_depth_reduction && !deadline.passed() {
146-
// First try reducing the `png` var (could be grayscale or indexed)
146+
// First try the `png` var
147147
let reduced = reduced_bit_depth_8_or_less(&png);
148-
// It's possible that an indexed version will be smaller (it might reach a lower depth),
149-
// but grayscale is usually better so we'll skip it if we're doing cheap evaluations and
150-
// already have a reduced image.
151-
let try_indexed = !cheap || reduced.is_none();
148+
// Then try the `indexed` var, unless we're doing cheap evaluations and already have a reduction
149+
if (!cheap || reduced.is_none()) && !deadline.passed() {
150+
if let Some(indexed) = indexed.and_then(|png| reduced_bit_depth_8_or_less(&png)) {
151+
// Only evaluate this if it's different from the first result (which must be grayscale if it exists)
152+
if reduced.as_ref().map_or(true, |r| r.data != indexed.data) {
153+
eval.try_image(Arc::new(indexed));
154+
evaluation_added = true;
155+
}
156+
}
157+
}
158+
// Enter the first result into the evaluator
152159
if let Some(reduced) = reduced {
153160
eval.try_image(Arc::new(reduced));
154161
evaluation_added = true;
155162
}
156-
if try_indexed && !deadline.passed() {
157-
if let Some(reduced) = indexed.and_then(|png| reduced_bit_depth_8_or_less(&png)) {
158-
eval.try_image(Arc::new(reduced));
159-
evaluation_added = true;
160-
}
161-
}
162163
}
163164

164165
if evaluation_added {

0 commit comments

Comments
 (0)