Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit f90972a

Browse files
committedJun 25, 2024
add comments explaining optimizations for Filter::next_chunk
1 parent 4039a7f commit f90972a

File tree

1 file changed

+3
-1
lines changed

1 file changed

+3
-1
lines changed
 

‎core/src/iter/adapters/filter.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,9 @@ where
4141

4242
let result = self.iter.try_for_each(|element| {
4343
let idx = initialized;
44+
// branchless index update combined with unconditionally copying the value even when
45+
// it is filtered reduces branching and dependencies in the loop.
4446
initialized = idx + (self.predicate)(&element) as usize;
45-
4647
// SAFETY: Loop conditions ensure the index is in bounds.
4748
unsafe { array.get_unchecked_mut(idx) }.write(element);
4849

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

0 commit comments

Comments
 (0)
Failed to load comments.