From f90972a41040ee4ceb9662ae9b37cb93573bd742 Mon Sep 17 00:00:00 2001 From: The 8472 Date: Mon, 24 Jun 2024 19:56:22 +0200 Subject: [PATCH] add comments explaining optimizations for Filter::next_chunk --- core/src/iter/adapters/filter.rs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/core/src/iter/adapters/filter.rs b/core/src/iter/adapters/filter.rs index 1c99f3938e270..ba49070329c22 100644 --- a/core/src/iter/adapters/filter.rs +++ b/core/src/iter/adapters/filter.rs @@ -41,8 +41,9 @@ where let result = self.iter.try_for_each(|element| { let idx = initialized; + // branchless index update combined with unconditionally copying the value even when + // it is filtered reduces branching and dependencies in the loop. initialized = idx + (self.predicate)(&element) as usize; - // SAFETY: Loop conditions ensure the index is in bounds. unsafe { array.get_unchecked_mut(idx) }.write(element); @@ -99,6 +100,7 @@ where fn next_chunk( &mut self, ) -> Result<[Self::Item; N], array::IntoIter> { + // avoid codegen for the dead branch let fun = const { if crate::mem::needs_drop::() { array::iter_next_chunk::