Skip to content

Commit

Permalink
Rollup merge of rust-lang#58730 - timvermeulen:internal_iteration, r=…
Browse files Browse the repository at this point in the history
…scottmcm

Have all methods of Filter and FilterMap use internal iteration

This PR changes `Filter::{next, next_back, count}` and `FilterMap::{next, next_back}` to all use internal iteration. The `next` and `next_back` methods are changed to directly forward to `try_for_each` and `try_rfold` respectively, using `Result` as the `Try` type. I think that's okay? Alternatively, I could change their implementations to use `LoopState` instead if there's any benefit in doing so.

r? @scottmcm
  • Loading branch information
GuillaumeGomez committed Feb 28, 2019
2 parents cbe9b50 + 88bd624 commit 847f58e
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 32 deletions.
32 changes: 30 additions & 2 deletions src/libcore/benches/iter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -185,13 +185,13 @@ bench_sums! {
bench_sums! {
bench_filter_sum,
bench_filter_ref_sum,
(0i64..1000000).filter(|x| x % 2 == 0)
(0i64..1000000).filter(|x| x % 3 == 0)
}

bench_sums! {
bench_filter_chain_sum,
bench_filter_chain_ref_sum,
(0i64..1000000).chain(0..1000000).filter(|x| x % 2 == 0)
(0i64..1000000).chain(0..1000000).filter(|x| x % 3 == 0)
}

bench_sums! {
Expand Down Expand Up @@ -306,3 +306,31 @@ fn bench_skip_then_zip(b: &mut Bencher) {
assert_eq!(s, 2009900);
});
}

#[bench]
fn bench_filter_count(b: &mut Bencher) {
b.iter(|| {
(0i64..1000000).map(black_box).filter(|x| x % 3 == 0).count()
})
}

#[bench]
fn bench_filter_ref_count(b: &mut Bencher) {
b.iter(|| {
(0i64..1000000).map(black_box).by_ref().filter(|x| x % 3 == 0).count()
})
}

#[bench]
fn bench_filter_chain_count(b: &mut Bencher) {
b.iter(|| {
(0i64..1000000).chain(0..1000000).map(black_box).filter(|x| x % 3 == 0).count()
})
}

#[bench]
fn bench_filter_chain_ref_count(b: &mut Bencher) {
b.iter(|| {
(0i64..1000000).chain(0..1000000).map(black_box).by_ref().filter(|x| x % 3 == 0).count()
})
}
37 changes: 7 additions & 30 deletions src/libcore/iter/adapters/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -681,12 +681,7 @@ impl<I: Iterator, P> Iterator for Filter<I, P> where P: FnMut(&I::Item) -> bool

#[inline]
fn next(&mut self) -> Option<I::Item> {
for x in &mut self.iter {
if (self.predicate)(&x) {
return Some(x);
}
}
None
self.try_for_each(Err).err()
}

#[inline]
Expand All @@ -707,12 +702,9 @@ impl<I: Iterator, P> Iterator for Filter<I, P> where P: FnMut(&I::Item) -> bool
// Using the branchless version will also simplify the LLVM byte code, thus
// leaving more budget for LLVM optimizations.
#[inline]
fn count(mut self) -> usize {
let mut count = 0;
for x in &mut self.iter {
count += (self.predicate)(&x) as usize;
}
count
fn count(self) -> usize {
let mut predicate = self.predicate;
self.iter.map(|x| predicate(&x) as usize).sum()
}

#[inline]
Expand Down Expand Up @@ -746,12 +738,7 @@ impl<I: DoubleEndedIterator, P> DoubleEndedIterator for Filter<I, P>
{
#[inline]
fn next_back(&mut self) -> Option<I::Item> {
for x in self.iter.by_ref().rev() {
if (self.predicate)(&x) {
return Some(x);
}
}
None
self.try_rfold((), |_, x| Err(x)).err()
}

#[inline]
Expand Down Expand Up @@ -820,12 +807,7 @@ impl<B, I: Iterator, F> Iterator for FilterMap<I, F>

#[inline]
fn next(&mut self) -> Option<B> {
for x in self.iter.by_ref() {
if let Some(y) = (self.f)(x) {
return Some(y);
}
}
None
self.try_for_each(Err).err()
}

#[inline]
Expand Down Expand Up @@ -863,12 +845,7 @@ impl<B, I: DoubleEndedIterator, F> DoubleEndedIterator for FilterMap<I, F>
{
#[inline]
fn next_back(&mut self) -> Option<B> {
for x in self.iter.by_ref().rev() {
if let Some(y) = (self.f)(x) {
return Some(y);
}
}
None
self.try_rfold((), |_, x| Err(x)).err()
}

#[inline]
Expand Down

0 comments on commit 847f58e

Please sign in to comment.