diff options
| author | The 8472 <git@infinite-source.de> | 2024-06-24 19:56:22 +0200 |
|---|---|---|
| committer | The 8472 <git@infinite-source.de> | 2024-06-25 23:20:00 +0200 |
| commit | 2be2d77c501730caedae8db2f0a0f02db57ae505 (patch) | |
| tree | 0b9a1247214a0661e5ec2b7bafd6f4e14d91990d | |
| parent | 133e7b10a45bddd4ef5ba265d848ce0a1976e8ae (diff) | |
| download | rust-2be2d77c501730caedae8db2f0a0f02db57ae505.tar.gz rust-2be2d77c501730caedae8db2f0a0f02db57ae505.zip | |
add comments explaining optimizations for Filter::next_chunk
| -rw-r--r-- | library/core/src/iter/adapters/filter.rs | 4 |
1 files changed, 3 insertions, 1 deletions
diff --git a/library/core/src/iter/adapters/filter.rs b/library/core/src/iter/adapters/filter.rs index 1c99f3938e2..ba49070329c 100644 --- a/library/core/src/iter/adapters/filter.rs +++ b/library/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<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> |
