Skip to content

Commit 4bb9a8b

Browse files
committed
Add an overflow check in the Iter::next() impl for Range<_>
This helps with vectorization in some cases, such as (0..u16::MAX).collect::<Vec<u16>>(), as LLVM is able to change the loop condition to use equality instead of less than
1 parent c240751 commit 4bb9a8b

File tree

1 file changed

+10
-3
lines changed

1 file changed

+10
-3
lines changed

src/libcore/iter/range.rs

+10-3
Original file line numberDiff line numberDiff line change
@@ -214,9 +214,16 @@ impl<A: Step> Iterator for ops::Range<A> {
214214
#[inline]
215215
fn next(&mut self) -> Option<A> {
216216
if self.start < self.end {
217-
let mut n = self.start.add_one();
218-
mem::swap(&mut n, &mut self.start);
219-
Some(n)
217+
// We check for overflow here, even though it can't actually
218+
// happen. Adding this check does however help llvm vectorize loops
219+
// for some ranges that don't get vectorized otherwise,
220+
// and this won't actually result in an extra check in an optimized build.
221+
if let Some(mut n) = self.start.add_usize(1) {
222+
mem::swap(&mut n, &mut self.start);
223+
Some(n)
224+
} else {
225+
None
226+
}
220227
} else {
221228
None
222229
}

0 commit comments

Comments
 (0)