add comments explaining optimizations for Filter::next_chunk · model-checking/verify-rust-std@f90972a (original) (raw)

Original file line number Diff line number Diff line change
@@ -41,8 +41,9 @@ where
41 41
42 42 let result = self.iter.try_for_each(|element
43 43 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.
44 46 initialized = idx + (self.predicate)(&element) as usize;
45 -
46 47 // SAFETY: Loop conditions ensure the index is in bounds.
47 48 unsafe { array.get_unchecked_mut(idx) }.write(element);
48 49
@@ -99,6 +100,7 @@ where
99 100 fn next_chunk<const N: usize>(
100 101 &mut self,
101 102 ) -> Result<[Self::Item; N], array::IntoIter<Self::Item, N>> {
103 +// avoid codegen for the dead branch
102 104 let fun = const {
103 105 if crate::mem::needs_drop::<I::Item>() {
104 106 array::iter_next_chunk::<I::Item, N>