diff options
| author | bors <bors@rust-lang.org> | 2024-01-06 22:34:53 +0000 |
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2024-01-06 22:34:53 +0000 |
| commit | fde0e98247dd4dd13af6cfe21f9eab2e54d50f12 (patch) | |
| tree | de2f32fd1f863d6d089415a36e571d6fd62a25b5 | |
| parent | b6a8c762eed0ae0383658c38d65cb91bbd9800a1 (diff) | |
| parent | 0e9c3d95336efb6dd85f6b97235f58a7cf040bfc (diff) | |
| download | rust-fde0e98247dd4dd13af6cfe21f9eab2e54d50f12.tar.gz rust-fde0e98247dd4dd13af6cfe21f9eab2e54d50f12.zip | |
Auto merge of #119599 - marthadev:position, r=the8472
Rewrite Iterator::position default impl Storing the accumulating value outside the fold in an attempt to improve code generation has shown speedups on various handwritten benchmarks, see discussion at #119551.
| -rw-r--r-- | library/core/src/iter/traits/iterator.rs | 19 |
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 |
