diff options
| author | The8472 <git@infinite-source.de> | 2021-07-24 17:42:20 +0200 |
|---|---|---|
| committer | The8472 <git@infinite-source.de> | 2021-07-24 19:24:11 +0200 |
| commit | e015e9da717bf501ab4752b2ea045d899f804a42 (patch) | |
| tree | 50978138163fc4dad124b8f7de1ff97680b6f2d0 /library/core/src/array | |
| parent | 18840b0719aa766a1bc49ea2eb5dc2e4cde7da3f (diff) | |
| download | rust-e015e9da717bf501ab4752b2ea045d899f804a42.tar.gz rust-e015e9da717bf501ab4752b2ea045d899f804a42.zip | |
implement fold() on array::IntoIter to improve flatten().collect() perf
``` # old test vec::bench_flat_map_collect ... bench: 2,244,024 ns/iter (+/- 18,903) # new test vec::bench_flat_map_collect ... bench: 172,863 ns/iter (+/- 2,141) ```
Diffstat (limited to 'library/core/src/array')
| -rw-r--r-- | library/core/src/array/iter.rs | 16 |
1 files changed, 16 insertions, 0 deletions
diff --git a/library/core/src/array/iter.rs b/library/core/src/array/iter.rs index 61ab1b1faff..004d1736b0f 100644 --- a/library/core/src/array/iter.rs +++ b/library/core/src/array/iter.rs @@ -123,6 +123,22 @@ impl<T, const N: usize> Iterator for IntoIter<T, N> { (len, Some(len)) } + #[inline] + fn fold<Acc, Fold>(mut self, init: Acc, mut fold: Fold) -> Acc + where + Fold: FnMut(Acc, Self::Item) -> Acc, + { + let data = &mut self.data; + (&mut self.alive) + .try_fold::<_, _, Result<_, !>>(init, |acc, idx| { + // SAFETY: idx is obtained by folding over the `alive` range, which implies the + // value is currently considered alive but as the range is being consumed each value + // we read here will only be read once and then considered dead. + Ok(fold(acc, unsafe { data.get_unchecked(idx).assume_init_read() })) + }) + .unwrap() + } + fn count(self) -> usize { self.len() } |
