about summary refs log tree commit diff
path: root/library/core/src/array
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2022-05-14 03:12:53 +0000
committerbors <bors@rust-lang.org>2022-05-14 03:12:53 +0000
commit9fbbe75fd73ed3c202cec3b0ba51eadf506f03fe (patch)
tree370bb66caa2dcab24871a888d08ecfe70fdfe8d4 /library/core/src/array
parentf1f721e64014863f41c1a386b04af04c2de25321 (diff)
parente8fc7ba6a756628b3226e002e3f3e54ddf14fa04 (diff)
downloadrust-9fbbe75fd73ed3c202cec3b0ba51eadf506f03fe.tar.gz
rust-9fbbe75fd73ed3c202cec3b0ba51eadf506f03fe.zip
Auto merge of #95602 - scottmcm:faster-array-intoiter-fold, r=the8472
Fix `array::IntoIter::fold` to use the optimized `Range::fold`

It was using `Iterator::by_ref` in the implementation, which ended up pessimizing it enough that, for example, it didn't vectorize when we tried it in the <https://rust-lang.zulipchat.com/#narrow/stream/257879-project-portable-simd/topic/Reducing.20sum.20into.20wider.20types> conversation.

Demonstration that the codegen test doesn't pass on the current nightly: <https://rust.godbolt.org/z/Taxev5eMn>
Diffstat (limited to 'library/core/src/array')
-rw-r--r--library/core/src/array/iter.rs16
1 files changed, 15 insertions, 1 deletions
diff --git a/library/core/src/array/iter.rs b/library/core/src/array/iter.rs
index 5cecc4086d8..f4885ed9ffb 100644
--- a/library/core/src/array/iter.rs
+++ b/library/core/src/array/iter.rs
@@ -266,7 +266,7 @@ impl<T, const N: usize> Iterator for IntoIter<T, N> {
         Fold: FnMut(Acc, Self::Item) -> Acc,
     {
         let data = &mut self.data;
-        self.alive.by_ref().fold(init, |acc, idx| {
+        iter::ByRefSized(&mut self.alive).fold(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.
@@ -323,6 +323,20 @@ impl<T, const N: usize> DoubleEndedIterator for IntoIter<T, N> {
         })
     }
 
+    #[inline]
+    fn rfold<Acc, Fold>(mut self, init: Acc, mut rfold: Fold) -> Acc
+    where
+        Fold: FnMut(Acc, Self::Item) -> Acc,
+    {
+        let data = &mut self.data;
+        iter::ByRefSized(&mut self.alive).rfold(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.
+            rfold(acc, unsafe { data.get_unchecked(idx).assume_init_read() })
+        })
+    }
+
     fn advance_back_by(&mut self, n: usize) -> Result<(), usize> {
         let len = self.len();