From fb8cc7bd99b10b6e219d5736da2bfaae68f964c9 Mon Sep 17 00:00:00 2001 From: kadmin Date: Tue, 5 Jan 2021 01:59:39 +0000 Subject: [PATCH 1/2] Clean up branching in skip --- library/core/src/iter/adapters/skip.rs | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/library/core/src/iter/adapters/skip.rs b/library/core/src/iter/adapters/skip.rs index dd5325660c3..e55c7a6bf5d 100644 --- a/library/core/src/iter/adapters/skip.rs +++ b/library/core/src/iter/adapters/skip.rs @@ -1,3 +1,4 @@ +use crate::intrinsics::unlikely; use crate::iter::{adapters::SourceIter, FusedIterator, InPlaceIterable}; use crate::ops::{ControlFlow, Try}; @@ -31,13 +32,10 @@ where #[inline] fn next(&mut self) -> Option { - if self.n == 0 { - self.iter.next() - } else { - let old_n = self.n; - self.n = 0; - self.iter.nth(old_n) + if unlikely(self.n > 0) { + self.iter.nth(crate::mem::take(&mut self.n) - 1); } + self.iter.next() } #[inline] From e5094a28514f25654f9096601bd1df75657f16ba Mon Sep 17 00:00:00 2001 From: kadmin Date: Fri, 8 Jan 2021 09:50:35 +0000 Subject: [PATCH 2/2] Add more benchmarks --- library/core/benches/iter.rs | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/library/core/benches/iter.rs b/library/core/benches/iter.rs index fb6b4b78379..f2169914ac9 100644 --- a/library/core/benches/iter.rs +++ b/library/core/benches/iter.rs @@ -276,7 +276,29 @@ bench_sums! { bench_sums! { bench_cycle_take_sum, bench_cycle_take_ref_sum, - (0i64..10000).cycle().take(1000000) + (0..10000).cycle().take(1000000) +} + +bench_sums! { + bench_cycle_skip_take_sum, + bench_cycle_skip_take_ref_sum, + (0..100000).cycle().skip(1000000).take(1000000) +} + +bench_sums! { + bench_cycle_take_skip_sum, + bench_cycle_take_skip_ref_sum, + (0..100000).cycle().take(1000000).skip(100000) +} + +bench_sums! { + bench_skip_cycle_skip_zip_add_sum, + bench_skip_cycle_skip_zip_add_ref_sum, + (0..100000).skip(100).cycle().skip(100) + .zip((0..100000).cycle().skip(10)) + .map(|(a,b)| a+b) + .skip(100000) + .take(1000000) } // Checks whether Skip> is as fast as Zip, Skip>, from