Skip to content

Work-around optimiser deficiencies in Range iterator. #24705

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions src/libcore/iter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2737,9 +2737,17 @@ impl<A: Step + One> Iterator for ops::Range<A> where

#[inline]
fn next(&mut self) -> Option<A> {
if self.start < self.end {
let mut n = &self.start + &A::one();
mem::swap(&mut n, &mut self.start);
// FIXME #24660: this may start returning Some after returning
// None if the + overflows. This is OK per Iterator's
// definition, but it would be really nice for a core iterator
// like `x..y` to be as well behaved as
// possible. Unfortunately, for types like `i32`, LLVM
// mishandles the version that places the mutation inside the
// `if`: it seems to optimise the `Option<i32>` in a way that
// confuses it.
let mut n = &self.start + &A::one();
mem::swap(&mut n, &mut self.start);
if n < self.end {
Some(n)
} else {
None
Expand Down
7 changes: 7 additions & 0 deletions src/libcoretest/iter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -932,3 +932,10 @@ fn bench_max(b: &mut Bencher) {
it.map(scatter).max()
})
}

#[bench]
fn bench_range_constant_fold(b: &mut Bencher) {
// this should be constant-folded to just '1000', and so this
// benchmark should run quickly...
b.iter(|| (0..1000).count())
}