Skip to content

Commit 2248d5b

Browse files
committed
Auto merge of #47180 - varkor:range-iterator-overrides, r=alexcrichton
Add iterator method specialisations to Range* Add specialised implementations of `max` for `Range`, and `last`, `min` and `max` for `RangeInclusive`, all of which lead to significant advantages in the generated assembly on x86. Note that adding specialisations of `min` and `last` for `Range` led to no benefit, and adding `sum` for `Range` and `RangeInclusive` led to type inference issues (though this is possibly still worthwhile considering the performance gain). This addresses some of the concerns in #39975.
2 parents b5392f5 + 2d83343 commit 2248d5b

File tree

2 files changed

+54
-0
lines changed

2 files changed

+54
-0
lines changed

src/libcore/iter/range.rs

+20
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,11 @@ impl<A: Step> Iterator for ops::Range<A> {
251251
self.start = self.end.clone();
252252
None
253253
}
254+
255+
#[inline]
256+
fn max(mut self) -> Option<A> {
257+
self.next_back()
258+
}
254259
}
255260

256261
// These macros generate `ExactSizeIterator` impls for various range types.
@@ -367,6 +372,21 @@ impl<A: Step> Iterator for ops::RangeInclusive<A> {
367372
self.end.replace_zero();
368373
None
369374
}
375+
376+
#[inline]
377+
fn last(mut self) -> Option<A> {
378+
self.next_back()
379+
}
380+
381+
#[inline]
382+
fn min(mut self) -> Option<A> {
383+
self.next()
384+
}
385+
386+
#[inline]
387+
fn max(mut self) -> Option<A> {
388+
self.next_back()
389+
}
370390
}
371391

372392
#[unstable(feature = "inclusive_range", reason = "recently added, follows RFC", issue = "28237")]

src/libcore/tests/iter.rs

+34
Original file line numberDiff line numberDiff line change
@@ -1352,6 +1352,40 @@ fn test_range_step() {
13521352
assert_eq!((isize::MIN..isize::MAX).step_by(1).size_hint(), (usize::MAX, Some(usize::MAX)));
13531353
}
13541354

1355+
#[test]
1356+
fn test_range_max() {
1357+
assert_eq!((0..100).max(), Some(99));
1358+
assert_eq!((-20..-10).max(), Some(-11));
1359+
assert_eq!((1..1).max(), None);
1360+
}
1361+
1362+
#[test]
1363+
fn test_range_inclusive_last_max() {
1364+
assert_eq!((0..=20).last(), Some(20));
1365+
assert_eq!((-20..=0).last(), Some(0));
1366+
assert_eq!((5..=5).last(), Some(5));
1367+
let mut r = 10..=10;
1368+
r.next();
1369+
assert_eq!(r.last(), None);
1370+
1371+
assert_eq!((0..=20).max(), Some(20));
1372+
assert_eq!((-20..=0).max(), Some(0));
1373+
assert_eq!((5..=5).max(), Some(5));
1374+
let mut r = 10..=10;
1375+
r.next();
1376+
assert_eq!(r.max(), None);
1377+
}
1378+
1379+
#[test]
1380+
fn test_range_inclusive_min() {
1381+
assert_eq!((0..=20).min(), Some(0));
1382+
assert_eq!((-20..=0).min(), Some(-20));
1383+
assert_eq!((5..=5).min(), Some(5));
1384+
let mut r = 10..=10;
1385+
r.next();
1386+
assert_eq!(r.min(), None);
1387+
}
1388+
13551389
#[test]
13561390
fn test_repeat() {
13571391
let mut it = repeat(42);

0 commit comments

Comments
 (0)