about summary refs log tree commit diff
diff options
context:
space:
mode:
authormartha <marthadev@pm.me>2024-01-05 03:34:21 +0100
committermarthadev <marthadev@pm.me>2024-01-05 16:35:44 +0100
commit0e9c3d95336efb6dd85f6b97235f58a7cf040bfc (patch)
tree4648e4d27b7f10d782852cf07f2c706ecf7ef859
parentf688dd684faca5b31b156fac2c6e0ae81fc9bc90 (diff)
downloadrust-0e9c3d95336efb6dd85f6b97235f58a7cf040bfc.tar.gz
rust-0e9c3d95336efb6dd85f6b97235f58a7cf040bfc.zip
Fix #119551: Rewrite Iterator::position default impl, storing the accumulating value outside of the fold in an attempt to improve code generation
Squashed with: Add inheriting overflow checks back
-rw-r--r--library/core/src/iter/traits/iterator.rs19
1 files changed, 13 insertions, 6 deletions
diff --git a/library/core/src/iter/traits/iterator.rs b/library/core/src/iter/traits/iterator.rs
index 8e2c887a161..d94a508b5b2 100644
--- a/library/core/src/iter/traits/iterator.rs
+++ b/library/core/src/iter/traits/iterator.rs
@@ -3094,16 +3094,23 @@ pub trait Iterator {
         P: FnMut(Self::Item) -> bool,
     {
         #[inline]
-        fn check<T>(
-            mut predicate: impl FnMut(T) -> bool,
-        ) -> impl FnMut(usize, T) -> ControlFlow<usize, usize> {
+        fn check<'a, T>(
+            mut predicate: impl FnMut(T) -> bool + 'a,
+            acc: &'a mut usize,
+        ) -> impl FnMut((), T) -> ControlFlow<usize, ()> + 'a {
             #[rustc_inherit_overflow_checks]
-            move |i, x| {
-                if predicate(x) { ControlFlow::Break(i) } else { ControlFlow::Continue(i + 1) }
+            move |_, x| {
+                if predicate(x) {
+                    ControlFlow::Break(*acc)
+                } else {
+                    *acc += 1;
+                    ControlFlow::Continue(())
+                }
             }
         }
 
-        self.try_fold(0, check(predicate)).break_value()
+        let mut acc = 0;
+        self.try_fold((), check(predicate, &mut acc)).break_value()
     }
 
     /// Searches for an element in an iterator from the right, returning its