From bf2b0129d1d3cc1ffa733059f3088adb6d745fe6 Mon Sep 17 00:00:00 2001 From: Philippe-Cholet Date: Tue, 7 Nov 2023 10:59:08 +0100 Subject: [PATCH] Better `Product::size_hint` `b` size hint was computed even when not needed. --- src/adaptors/mod.rs | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/src/adaptors/mod.rs b/src/adaptors/mod.rs index 623272eae..e3b6d0f28 100644 --- a/src/adaptors/mod.rs +++ b/src/adaptors/mod.rs @@ -365,15 +365,13 @@ where } fn size_hint(&self) -> (usize, Option) { - let has_cur = matches!(self.a_cur, Some(Some(_))) as usize; // Not ExactSizeIterator because size may be larger than usize - let (b_min, b_max) = self.b.size_hint(); - // Compute a * b_orig + b for both lower and upper bound - size_hint::add( - size_hint::mul(self.a.size_hint(), self.b_orig.size_hint()), - (b_min * has_cur, b_max.map(move |x| x * has_cur)), - ) + let mut sh = size_hint::mul(self.a.size_hint(), self.b_orig.size_hint()); + if matches!(self.a_cur, Some(Some(_))) { + sh = size_hint::add(sh, self.b.size_hint()); + } + sh } fn fold(self, mut accum: Acc, mut f: G) -> Acc