Skip to content

Commit

Permalink
Rollup merge of rust-lang#39107 - llogiq:branchless_filter_count, r=a…
Browse files Browse the repository at this point in the history
…lexcrichton

branchless .filter(_).count()

I found that the branchless version is only slower if we have little to no branch misses, which usually isn't the case. I notice speedups between -5% (perfect prediction) and 60% (real world data).
  • Loading branch information
frewsxcv authored Feb 5, 2017
2 parents 79027e9 + bfabe81 commit eb92641
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
22 changes: 21 additions & 1 deletion src/libcore/iter/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1086,7 +1086,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 self.iter.by_ref() {
for x in &mut self.iter {
if (self.predicate)(&x) {
return Some(x);
}
Expand All @@ -1099,6 +1099,26 @@ impl<I: Iterator, P> Iterator for Filter<I, P> where P: FnMut(&I::Item) -> bool
let (_, upper) = self.iter.size_hint();
(0, upper) // can't know a lower bound, due to the predicate
}

// this special case allows the compiler to make `.filter(_).count()`
// branchless. Barring perfect branch prediction (which is unattainable in
// the general case), this will be much faster in >90% of cases (containing
// virtually all real workloads) and only a tiny bit slower in the rest.
//
// Having this specialization thus allows us to write `.filter(p).count()`
// where we would otherwise write `.map(|x| p(x) as usize).sum()`, which is
// less readable and also less backwards-compatible to Rust before 1.10.
//
// 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
}
}

#[stable(feature = "rust1", since = "1.0.0")]
Expand Down
6 changes: 6 additions & 0 deletions src/libcoretest/iter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,12 @@ fn test_iterator_enumerate_count() {
assert_eq!(xs.iter().count(), 6);
}

#[test]
fn test_iterator_filter_count() {
let xs = [0, 1, 2, 3, 4, 5, 6, 7, 8];
assert_eq!(xs.iter().filter(|&&x| x % 2 == 0).count(), 5);
}

#[test]
fn test_iterator_peekable() {
let xs = vec![0, 1, 2, 3, 4, 5];
Expand Down

0 comments on commit eb92641

Please sign in to comment.