Skip to content

Work-around optimiser deficiencies in Range iterator. #26390

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 2 commits 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
28 changes: 17 additions & 11 deletions src/libcore/iter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2797,14 +2797,12 @@ impl<A> Iterator for RangeInclusive<A> where

#[inline]
fn next(&mut self) -> Option<A> {
self.range.next().or_else(|| {
if !self.done && self.range.start == self.range.end {
self.done = true;
Some(self.range.end.clone())
} else {
None
}
})
if !self.done && self.range.start == self.range.end {
self.done = true;
Some(self.range.end.clone())
} else {
self.range.next()
}
}

#[inline]
Expand Down Expand Up @@ -2896,9 +2894,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();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unfortunately I think doing this addition outside the guard causes the panic semantics of this iterator to change, which I believe will be a regression. For example this code does not panic today, but I suspect it will panic with this implementation:

fn main() {
    for i in 255u8..255 {
        println!("{}", i);
    }
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yea, didn't notice that corner case...
Anyway, closing is perfectly alright with me, I just wanted to move the PR forward :)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm ok, I think for now we may want to not merge this due to that, but perhaps we can think of a new idea in the future!

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 @@ -1167,3 +1167,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())
}