Optimize StepBy::nth overflow handling

This commit is contained in:
oberien 2018-01-19 22:34:22 +01:00
parent f08dec114f
commit f72b7f7c86

View file

@ -307,6 +307,7 @@ use fmt;
use iter_private::TrustedRandomAccess;
use ops::Try;
use usize;
use intrinsics;
#[stable(feature = "rust1", since = "1.0.0")]
pub use self::iterator::Iterator;
@ -718,7 +719,11 @@ impl<I> Iterator for StepBy<I> where I: Iterator {
}
// overflow handling
while n.checked_mul(step).is_none() {
loop {
let mul = n.checked_mul(step);
if unsafe { intrinsics::likely(mul.is_some())} {
return self.iter.nth(mul.unwrap() - 1);
}
let div_n = usize::MAX / n;
let div_step = usize::MAX / step;
let nth_n = div_n * n;
@ -732,7 +737,6 @@ impl<I> Iterator for StepBy<I> where I: Iterator {
};
self.iter.nth(nth - 1);
}
self.iter.nth(n * step - 1)
}
}