From 07a1d5f0273fdcce5ec7ec3cba9a6db232d9124b Mon Sep 17 00:00:00 2001 From: The 8472 Date: Mon, 28 Aug 2023 14:35:51 +0200 Subject: [PATCH] reduce indirection in for_each specialization --- library/core/src/iter/adapters/take.rs | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/library/core/src/iter/adapters/take.rs b/library/core/src/iter/adapters/take.rs index 70252e075b9..c1d8cc4ff57 100644 --- a/library/core/src/iter/adapters/take.rs +++ b/library/core/src/iter/adapters/take.rs @@ -303,13 +303,12 @@ fn spec_fold(mut self, init: B, mut f: F) -> B } #[inline] - fn spec_for_each(self, f: F) { - // Based on the the Iterator trait default impl. - #[inline] - fn call(mut f: impl FnMut(T)) -> impl FnMut((), T) { - move |(), item| f(item) + fn spec_for_each(mut self, mut f: F) { + let end = self.n.min(self.iter.size()); + for i in 0..end { + // SAFETY: i < end <= self.iter.size() and we discard the iterator at the end + let val = unsafe { self.iter.__iterator_get_unchecked(i) }; + f(val); } - - self.spec_fold((), call(f)); } }