Skip to content
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

RangeInclusive internal iteration performance improvement. #58122

Merged
merged 2 commits into from
Feb 23, 2019
Merged
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
61 changes: 58 additions & 3 deletions src/libcore/iter/range.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use convert::TryFrom;
use mem;
use ops::{self, Add, Sub};
use ops::{self, Add, Sub, Try};
use usize;

use super::{FusedIterator, TrustedLen};
Expand Down Expand Up @@ -368,11 +368,11 @@ impl<A: Step> Iterator for ops::RangeInclusive<A> {
Some(Less) => {
self.is_empty = Some(false);
self.start = plus_n.add_one();
return Some(plus_n)
return Some(plus_n);
}
Some(Equal) => {
self.is_empty = Some(true);
return Some(plus_n)
return Some(plus_n);
}
_ => {}
}
Expand All @@ -382,6 +382,34 @@ impl<A: Step> Iterator for ops::RangeInclusive<A> {
None
}

#[inline]
fn try_fold<B, F, R>(&mut self, init: B, mut f: F) -> R
where
Self: Sized, F: FnMut(B, Self::Item) -> R, R: Try<Ok=B>
{
self.compute_is_empty();

if self.is_empty() {
return Try::from_ok(init);
}

let mut accum = init;

while self.start < self.end {
let n = self.start.add_one();
let n = mem::replace(&mut self.start, n);
accum = f(accum, n)?;
}

self.is_empty = Some(true);

if self.start == self.end {
accum = f(accum, self.start.clone())?;
matthieu-m marked this conversation as resolved.
Show resolved Hide resolved
}

Try::from_ok(accum)
}

#[inline]
fn last(mut self) -> Option<A> {
self.next_back()
Expand Down Expand Up @@ -415,6 +443,33 @@ impl<A: Step> DoubleEndedIterator for ops::RangeInclusive<A> {
self.end.clone()
})
}

#[inline]
fn try_rfold<B, F, R>(&mut self, init: B, mut f: F) -> R where
Self: Sized, F: FnMut(B, Self::Item) -> R, R: Try<Ok=B>
{
self.compute_is_empty();

if self.is_empty() {
return Try::from_ok(init);
}

let mut accum = init;

while self.start < self.end {
let n = self.end.sub_one();
let n = mem::replace(&mut self.end, n);
accum = f(accum, n)?;
}

self.is_empty = Some(true);

if self.start == self.end {
accum = f(accum, self.start.clone())?;
}

Try::from_ok(accum)
}
}

#[stable(feature = "fused", since = "1.26.0")]
Expand Down
2 changes: 2 additions & 0 deletions src/libcore/ops/range.rs
Original file line number Diff line number Diff line change
Expand Up @@ -334,12 +334,14 @@ pub struct RangeInclusive<Idx> {
trait RangeInclusiveEquality: Sized {
fn canonicalized_is_empty(range: &RangeInclusive<Self>) -> bool;
}

impl<T> RangeInclusiveEquality for T {
#[inline]
default fn canonicalized_is_empty(range: &RangeInclusive<Self>) -> bool {
range.is_empty.unwrap_or_default()
}
}

impl<T: PartialOrd> RangeInclusiveEquality for T {
#[inline]
fn canonicalized_is_empty(range: &RangeInclusive<Self>) -> bool {
Expand Down
24 changes: 21 additions & 3 deletions src/libcore/tests/iter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1738,19 +1738,37 @@ fn test_range_inclusive_folds() {
assert_eq!((1..=10).sum::<i32>(), 55);
assert_eq!((1..=10).rev().sum::<i32>(), 55);

let mut it = 40..=50;
let mut it = 44..=50;
assert_eq!(it.try_fold(0, i8::checked_add), None);
assert_eq!(it, 44..=50);
assert_eq!(it, 47..=50);
assert_eq!(it.try_fold(0, i8::checked_add), None);
assert_eq!(it, 50..=50);
assert_eq!(it.try_fold(0, i8::checked_add), Some(50));
assert!(it.is_empty());
assert_eq!(it.try_fold(0, i8::checked_add), Some(0));
assert!(it.is_empty());

let mut it = 40..=47;
assert_eq!(it.try_rfold(0, i8::checked_add), None);
assert_eq!(it, 40..=44);
assert_eq!(it.try_rfold(0, i8::checked_add), None);
assert_eq!(it, 44..=47);
assert_eq!(it, 40..=41);
assert_eq!(it.try_rfold(0, i8::checked_add), Some(81));
assert!(it.is_empty());
assert_eq!(it.try_rfold(0, i8::checked_add), Some(0));
assert!(it.is_empty());

let mut it = 10..=20;
assert_eq!(it.try_fold(0, |a,b| Some(a+b)), Some(165));
assert!(it.is_empty());
assert_eq!(it.try_fold(0, |a,b| Some(a+b)), Some(0));
assert!(it.is_empty());

let mut it = 10..=20;
assert_eq!(it.try_rfold(0, |a,b| Some(a+b)), Some(165));
assert!(it.is_empty());
assert_eq!(it.try_rfold(0, |a,b| Some(a+b)), Some(0));
assert!(it.is_empty());
}

#[test]
Expand Down