about summary refs log tree commit diff
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2021-10-12 23:59:48 +0000
committerbors <bors@rust-lang.org>2021-10-12 23:59:48 +0000
commitef4b3069baafa75bc047a80b58cc130e80576a4e (patch)
treef6555341c1bd9a3e10e5b3da15b4694332d48197
parentd7c97a02d1215e4ef26c31cb72dbaf16fd548b2c (diff)
parentf1c588f1ef391b33663f44c52b17c9cc12928a75 (diff)
downloadrust-ef4b3069baafa75bc047a80b58cc130e80576a4e.tar.gz
rust-ef4b3069baafa75bc047a80b58cc130e80576a4e.zip
Auto merge of #89774 - the8472:inline-mut-iter-next, r=m-ou-se
inline next() on &mut Iterator impl

In [#87431](https://github.com/rust-lang/rust/pull/87431/files#diff-79a6b417b85ecf4f1a4ef2235135fedf540199caf6e9e1d154ac6a413b40a757R132-R136)   I found that `(&mut range).fold` doesn't optimize well because the default impl for for `fold` on `&mut Iterator` doesn't inline `next`. In that particular case it was worked around by using `try_fold` which takes a `&mut self` instead of `self`.

Let's see if this can be fixed more broadly.
-rw-r--r--library/core/src/array/iter.rs19
-rw-r--r--library/core/src/iter/traits/iterator.rs1
2 files changed, 7 insertions, 13 deletions
diff --git a/library/core/src/array/iter.rs b/library/core/src/array/iter.rs
index 822747dd0e8..5d63cf03fcb 100644
--- a/library/core/src/array/iter.rs
+++ b/library/core/src/array/iter.rs
@@ -135,19 +135,12 @@ impl<T, const N: usize> Iterator for IntoIter<T, N> {
         Fold: FnMut(Acc, Self::Item) -> Acc,
     {
         let data = &mut self.data;
-        // FIXME: This uses try_fold(&mut iter) instead of fold(iter) because the latter
-        //  would go through the blanket `impl Iterator for &mut I` implementation
-        //  which lacks inline annotations on its methods and adding those would be a larger
-        //  perturbation than using try_fold here.
-        //  Whether it would be beneficial to add those annotations should be investigated separately.
-        (&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()
+        self.alive.by_ref().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.
+            fold(acc, unsafe { data.get_unchecked(idx).assume_init_read() })
+        })
     }
 
     fn count(self) -> usize {
diff --git a/library/core/src/iter/traits/iterator.rs b/library/core/src/iter/traits/iterator.rs
index 8f9b32e7750..f53d6cac7ed 100644
--- a/library/core/src/iter/traits/iterator.rs
+++ b/library/core/src/iter/traits/iterator.rs
@@ -3460,6 +3460,7 @@ pub trait Iterator {
 #[stable(feature = "rust1", since = "1.0.0")]
 impl<I: Iterator + ?Sized> Iterator for &mut I {
     type Item = I::Item;
+    #[inline]
     fn next(&mut self) -> Option<I::Item> {
         (**self).next()
     }