Skip to content

Commit bfabe81

Browse files
committed
add explanation, fix test
1 parent b40432c commit bfabe81

File tree

2 files changed

+12
-1
lines changed

2 files changed

+12
-1
lines changed

src/libcore/iter/mod.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1100,6 +1100,17 @@ impl<I: Iterator, P> Iterator for Filter<I, P> where P: FnMut(&I::Item) -> bool
11001100
(0, upper) // can't know a lower bound, due to the predicate
11011101
}
11021102

1103+
// this special case allows the compiler to make `.filter(_).count()`
1104+
// branchless. Barring perfect branch prediction (which is unattainable in
1105+
// the general case), this will be much faster in >90% of cases (containing
1106+
// virtually all real workloads) and only a tiny bit slower in the rest.
1107+
//
1108+
// Having this specialization thus allows us to write `.filter(p).count()`
1109+
// where we would otherwise write `.map(|x| p(x) as usize).sum()`, which is
1110+
// less readable and also less backwards-compatible to Rust before 1.10.
1111+
//
1112+
// Using the branchless version will also simplify the LLVM byte code, thus
1113+
// leaving more budget for LLVM optimizations.
11031114
#[inline]
11041115
fn count(mut self) -> usize {
11051116
let mut count = 0;

src/libcoretest/iter.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ fn test_iterator_enumerate_count() {
194194
#[test]
195195
fn test_iterator_filter_count() {
196196
let xs = [0, 1, 2, 3, 4, 5, 6, 7, 8];
197-
assert_eq!(xs.iter().filter(|x| x % 2 == 0).count(), 5);
197+
assert_eq!(xs.iter().filter(|&&x| x % 2 == 0).count(), 5);
198198
}
199199

200200
#[test]

0 commit comments

Comments
 (0)