Skip to content

Commit e015e9d

Browse files
committedJul 24, 2021
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) ```
1 parent 18840b0 commit e015e9d

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-0
lines changed
 

‎library/alloc/benches/vec.rs

+6
Original file line numberDiff line numberDiff line change
@@ -726,3 +726,9 @@ fn bench_dedup_old_100000(b: &mut Bencher) {
726726
fn bench_dedup_new_100000(b: &mut Bencher) {
727727
bench_vec_dedup_new(b, 100000);
728728
}
729+
730+
#[bench]
731+
fn bench_flat_map_collect(b: &mut Bencher) {
732+
let v = vec![777u32; 500000];
733+
b.iter(|| v.iter().flat_map(|color| color.rotate_left(8).to_be_bytes()).collect::<Vec<_>>());
734+
}

‎library/core/src/array/iter.rs

+16
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,22 @@ impl<T, const N: usize> Iterator for IntoIter<T, N> {
123123
(len, Some(len))
124124
}
125125

126+
#[inline]
127+
fn fold<Acc, Fold>(mut self, init: Acc, mut fold: Fold) -> Acc
128+
where
129+
Fold: FnMut(Acc, Self::Item) -> Acc,
130+
{
131+
let data = &mut self.data;
132+
(&mut self.alive)
133+
.try_fold::<_, _, Result<_, !>>(init, |acc, idx| {
134+
// SAFETY: idx is obtained by folding over the `alive` range, which implies the
135+
// value is currently considered alive but as the range is being consumed each value
136+
// we read here will only be read once and then considered dead.
137+
Ok(fold(acc, unsafe { data.get_unchecked(idx).assume_init_read() }))
138+
})
139+
.unwrap()
140+
}
141+
126142
fn count(self) -> usize {
127143
self.len()
128144
}

0 commit comments

Comments
 (0)
Please sign in to comment.