Skip to content

Commit

Permalink
add comments explaining optimizations for Filter::next_chunk
Browse files Browse the repository at this point in the history
  • Loading branch information
the8472 committed Jun 25, 2024
1 parent 4039a7f commit f90972a
Showing 1 changed file with 3 additions and 1 deletion.
4 changes: 3 additions & 1 deletion core/src/iter/adapters/filter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -99,6 +100,7 @@ where
fn next_chunk<const N: usize>(
&mut self,
) -> Result<[Self::Item; N], array::IntoIter<Self::Item, N>> {
// avoid codegen for the dead branch
let fun = const {
if crate::mem::needs_drop::<I::Item>() {
array::iter_next_chunk::<I::Item, N>
Expand Down

0 comments on commit f90972a

Please sign in to comment.