Skip to content

Commit bd65462

Browse files
committed
Fix rust-lang#119551: Rewrite Iterator::position default impl, storing the accumulating value outside of the fold in an attempt to improve code generation
1 parent f688dd6 commit bd65462

File tree

1 file changed

+14
-8
lines changed

1 file changed

+14
-8
lines changed

library/core/src/iter/traits/iterator.rs

+14-8
Original file line numberDiff line numberDiff line change
@@ -3094,16 +3094,22 @@ pub trait Iterator {
30943094
P: FnMut(Self::Item) -> bool,
30953095
{
30963096
#[inline]
3097-
fn check<T>(
3098-
mut predicate: impl FnMut(T) -> bool,
3099-
) -> impl FnMut(usize, T) -> ControlFlow<usize, usize> {
3100-
#[rustc_inherit_overflow_checks]
3101-
move |i, x| {
3102-
if predicate(x) { ControlFlow::Break(i) } else { ControlFlow::Continue(i + 1) }
3097+
fn check<'a, T>(
3098+
mut predicate: impl FnMut(T) -> bool + 'a,
3099+
acc: &'a mut usize,
3100+
) -> impl FnMut((), T) -> ControlFlow<usize, ()> + 'a {
3101+
move |_, x| {
3102+
if predicate(x) {
3103+
ControlFlow::Break(*acc)
3104+
} else {
3105+
*acc += 1;
3106+
ControlFlow::Continue(())
3107+
}
31033108
}
31043109
}
3105-
3106-
self.try_fold(0, check(predicate)).break_value()
3110+
3111+
let mut acc = 0;
3112+
self.try_fold((), check(predicate, &mut acc)).break_value()
31073113
}
31083114

31093115
/// Searches for an element in an iterator from the right, returning its

0 commit comments

Comments
 (0)