From 6c940893e58ad141ec8e3eaf2c65741d15d9eb93 Mon Sep 17 00:00:00 2001 From: Andre Bogus Date: Mon, 16 Jan 2017 22:28:58 +0100 Subject: [PATCH 1/4] branchless .filter(_).count() --- src/libcore/iter/mod.rs | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/libcore/iter/mod.rs b/src/libcore/iter/mod.rs index 3999db0d63c..5dce60b79c9 100644 --- a/src/libcore/iter/mod.rs +++ b/src/libcore/iter/mod.rs @@ -1099,6 +1099,16 @@ impl Iterator for Filter where P: FnMut(&I::Item) -> bool let (_, upper) = self.iter.size_hint(); (0, upper) // can't know a lower bound, due to the predicate } + + #[inline] + fn count(self) -> usize { + let (mut c, mut predicate, mut iter) = (0, self.predicate, self.iter); + for x in iter.by_ref() { + // branchless count + c += (&mut predicate)(&x) as usize; + } + c + } } #[stable(feature = "rust1", since = "1.0.0")] From 27f76157aafbac691e625b5d1b8b37330ec04552 Mon Sep 17 00:00:00 2001 From: Andre Bogus Date: Tue, 17 Jan 2017 13:18:16 +0100 Subject: [PATCH 2/4] fix style nits --- src/libcore/iter/mod.rs | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/src/libcore/iter/mod.rs b/src/libcore/iter/mod.rs index 5dce60b79c9..ea98265ef8d 100644 --- a/src/libcore/iter/mod.rs +++ b/src/libcore/iter/mod.rs @@ -1086,7 +1086,7 @@ impl Iterator for Filter where P: FnMut(&I::Item) -> bool #[inline] fn next(&mut self) -> Option { - for x in self.iter.by_ref() { + for x in &mut self.iter { if (self.predicate)(&x) { return Some(x); } @@ -1101,13 +1101,12 @@ impl Iterator for Filter where P: FnMut(&I::Item) -> bool } #[inline] - fn count(self) -> usize { - let (mut c, mut predicate, mut iter) = (0, self.predicate, self.iter); - for x in iter.by_ref() { - // branchless count - c += (&mut predicate)(&x) as usize; + fn count(mut self) -> usize { + let mut count = 0; + for x in &mut self.iter { + count += (self.predicate)(&x) as usize; } - c + count } } From b40432c94924f1fd7fef7df403348742ee17af4d Mon Sep 17 00:00:00 2001 From: Andre Bogus Date: Wed, 18 Jan 2017 13:55:47 +0100 Subject: [PATCH 3/4] add test case for filter+count --- src/libcoretest/iter.rs | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/libcoretest/iter.rs b/src/libcoretest/iter.rs index 99d31293053..05a674e05d5 100644 --- a/src/libcoretest/iter.rs +++ b/src/libcoretest/iter.rs @@ -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]; From bfabe817de438c63777aa8c01d6998b5158f7fdb Mon Sep 17 00:00:00 2001 From: Andre Bogus Date: Tue, 24 Jan 2017 09:46:01 +0100 Subject: [PATCH 4/4] add explanation, fix test --- src/libcore/iter/mod.rs | 11 +++++++++++ src/libcoretest/iter.rs | 2 +- 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/src/libcore/iter/mod.rs b/src/libcore/iter/mod.rs index ea98265ef8d..d9b8c5ea589 100644 --- a/src/libcore/iter/mod.rs +++ b/src/libcore/iter/mod.rs @@ -1100,6 +1100,17 @@ impl Iterator for Filter where P: FnMut(&I::Item) -> bool (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; diff --git a/src/libcoretest/iter.rs b/src/libcoretest/iter.rs index 05a674e05d5..e6d2494f5fd 100644 --- a/src/libcoretest/iter.rs +++ b/src/libcoretest/iter.rs @@ -194,7 +194,7 @@ fn test_iterator_enumerate_count() { #[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); + assert_eq!(xs.iter().filter(|&&x| x % 2 == 0).count(), 5); } #[test]