Skip to content

Commit 95979dc

Browse files
committed
Auto merge of #51601 - Emerentius:step_by_range_diet, r=sfackler
Specialize StepBy<Range(Inclusive)> Part of #51557, related to #43064, #31155 As discussed in the above issues, `step_by` optimizes very badly on ranges which is related to 1. the special casing of the first `StepBy::next()` call 2. the need to do 2 additions of `n - 1` and `1` inside the range's `next()` This PR eliminates both by overriding `next()` to always produce the current element and also step ahead by `n` elements in one go. The generated code is much better, even identical in the case of a `Range` with constant `start` and `end` where `start+step` can't overflow. Without constant bounds it's a bit longer than the manual loop. `RangeInclusive` doesn't optimize as nicely but is still much better than the original asm. Unsigned integers optimize better than signed ones for some reason. See the following two links for a comparison. [godbolt: specialization for ..](https://godbolt.org/g/haHLJr) [godbolt: specialization for ..=](https://godbolt.org/g/ewyMu6) `RangeFrom`, the only other range with an `Iterator` implementation can't be specialized like this without changing behaviour due to overflow. There is no way to save "finished-ness". The approach can not be used in general, because it would produce side effects of the underlying iterator too early. May obsolete #51435, haven't checked.
2 parents fff1aba + 000aff6 commit 95979dc

File tree

2 files changed

+81
-7
lines changed

2 files changed

+81
-7
lines changed

src/libcore/iter/mod.rs

+73-7
Original file line numberDiff line numberDiff line change
@@ -319,9 +319,10 @@
319319
use cmp;
320320
use fmt;
321321
use iter_private::TrustedRandomAccess;
322-
use ops::Try;
322+
use ops::{self, Try};
323323
use usize;
324324
use intrinsics;
325+
use mem;
325326

326327
#[stable(feature = "rust1", since = "1.0.0")]
327328
pub use self::iterator::Iterator;
@@ -672,12 +673,7 @@ impl<I> Iterator for StepBy<I> where I: Iterator {
672673

673674
#[inline]
674675
fn next(&mut self) -> Option<Self::Item> {
675-
if self.first_take {
676-
self.first_take = false;
677-
self.iter.next()
678-
} else {
679-
self.iter.nth(self.step)
680-
}
676+
<Self as StepBySpecIterator>::spec_next(self)
681677
}
682678

683679
#[inline]
@@ -737,6 +733,76 @@ impl<I> Iterator for StepBy<I> where I: Iterator {
737733
}
738734
}
739735

736+
// hidden trait for specializing iterator methods
737+
// could be generalized but is currently only used for StepBy
738+
trait StepBySpecIterator {
739+
type Item;
740+
fn spec_next(&mut self) -> Option<Self::Item>;
741+
}
742+
743+
impl<I> StepBySpecIterator for StepBy<I>
744+
where
745+
I: Iterator,
746+
{
747+
type Item = I::Item;
748+
749+
#[inline]
750+
default fn spec_next(&mut self) -> Option<I::Item> {
751+
if self.first_take {
752+
self.first_take = false;
753+
self.iter.next()
754+
} else {
755+
self.iter.nth(self.step)
756+
}
757+
}
758+
}
759+
760+
impl<T> StepBySpecIterator for StepBy<ops::Range<T>>
761+
where
762+
T: Step,
763+
{
764+
#[inline]
765+
fn spec_next(&mut self) -> Option<Self::Item> {
766+
self.first_take = false;
767+
if !(self.iter.start < self.iter.end) {
768+
return None;
769+
}
770+
// add 1 to self.step to get original step size back
771+
// it was decremented for the general case on construction
772+
if let Some(n) = self.iter.start.add_usize(self.step+1) {
773+
let next = mem::replace(&mut self.iter.start, n);
774+
Some(next)
775+
} else {
776+
let last = self.iter.start.clone();
777+
self.iter.start = self.iter.end.clone();
778+
Some(last)
779+
}
780+
}
781+
}
782+
783+
impl<T> StepBySpecIterator for StepBy<ops::RangeInclusive<T>>
784+
where
785+
T: Step,
786+
{
787+
#[inline]
788+
fn spec_next(&mut self) -> Option<Self::Item> {
789+
self.first_take = false;
790+
if !(self.iter.start <= self.iter.end) {
791+
return None;
792+
}
793+
// add 1 to self.step to get original step size back
794+
// it was decremented for the general case on construction
795+
if let Some(n) = self.iter.start.add_usize(self.step+1) {
796+
let next = mem::replace(&mut self.iter.start, n);
797+
Some(next)
798+
} else {
799+
let last = self.iter.start.replace_one();
800+
self.iter.end.replace_zero();
801+
Some(last)
802+
}
803+
}
804+
}
805+
740806
// StepBy can only make the iterator shorter, so the len will still fit.
741807
#[stable(feature = "iterator_step_by", since = "1.28.0")]
742808
impl<I> ExactSizeIterator for StepBy<I> where I: ExactSizeIterator {}

src/libcore/tests/iter.rs

+8
Original file line numberDiff line numberDiff line change
@@ -1618,6 +1618,14 @@ fn test_range_step() {
16181618
assert_eq!((isize::MIN..isize::MAX).step_by(1).size_hint(), (usize::MAX, Some(usize::MAX)));
16191619
}
16201620

1621+
#[test]
1622+
fn test_range_inclusive_step() {
1623+
assert_eq!((0..=50).step_by(10).collect::<Vec<_>>(), [0, 10, 20, 30, 40, 50]);
1624+
assert_eq!((0..=5).step_by(1).collect::<Vec<_>>(), [0, 1, 2, 3, 4, 5]);
1625+
assert_eq!((200..=255u8).step_by(10).collect::<Vec<_>>(), [200, 210, 220, 230, 240, 250]);
1626+
assert_eq!((250..=255u8).step_by(1).collect::<Vec<_>>(), [250, 251, 252, 253, 254, 255]);
1627+
}
1628+
16211629
#[test]
16221630
fn test_range_last_max() {
16231631
assert_eq!((0..20).last(), Some(19));

0 commit comments

Comments
 (0)