Skip to content

Commit 99d6692

Browse files
committed
Auto merge of rust-lang#87431 - the8472:array-iter-fold, r=kennytm
implement fold() on array::IntoIter to improve flatten().collect() perf With rust-lang#87168 flattening `array::IntoIter`s is now `TrustedLen`, the `FromIterator` implementation for `Vec` has a specialization for `TrustedLen` iterators which uses internal iteration. This implements one of the main internal iteration methods on `array::Into` to optimize the combination of those two features. This should address the main issue in rust-lang#87411 ``` # 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) ```
2 parents 998cfe5 + 2276c5e commit 99d6692

File tree

2 files changed

+27
-0
lines changed

2 files changed

+27
-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

+21
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,27 @@ 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+
// FIXME: This uses try_fold(&mut iter) instead of fold(iter) because the latter
133+
// would go through the blanket `impl Iterator for &mut I` implementation
134+
// which lacks inline annotations on its methods and adding those would be a larger
135+
// perturbation than using try_fold here.
136+
// Whether it would be beneficial to add those annotations should be investigated separately.
137+
(&mut self.alive)
138+
.try_fold::<_, _, Result<_, !>>(init, |acc, idx| {
139+
// SAFETY: idx is obtained by folding over the `alive` range, which implies the
140+
// value is currently considered alive but as the range is being consumed each value
141+
// we read here will only be read once and then considered dead.
142+
Ok(fold(acc, unsafe { data.get_unchecked(idx).assume_init_read() }))
143+
})
144+
.unwrap()
145+
}
146+
126147
fn count(self) -> usize {
127148
self.len()
128149
}

0 commit comments

Comments
 (0)