Skip to content

Commit 76fb91b

Browse files
authored
Rollup merge of rust-lang#64885 - andjo403:iter, r=scottmcm
use try_fold instead of try_for_each to reduce compile time as it was stated in rust-lang#64572 that the biggest gain was due to less code was generated I tried to reduce the number of functions to inline by using try_fold direct instead of calling try_for_each that calls try_fold. as there is some gains with using the try_fold function this is maybe a way forward. when I tried to compile the clap-rs benchmark I get times gains only some % from rust-lang#64572 there is more function that use eg. fold that calls try_fold that also can be changed but the question is how mush "duplication" that is tolerated in std to give faster compile times can someone start a perf run? cc @nnethercote @scottmcm @bluss r? @ghost
2 parents 8f5f92a + 8737061 commit 76fb91b

File tree

1 file changed

+14
-13
lines changed

1 file changed

+14
-13
lines changed

src/libcore/iter/traits/iterator.rs

+14-13
Original file line numberDiff line numberDiff line change
@@ -1859,14 +1859,13 @@ pub trait Iterator {
18591859
Self: Sized, F: FnMut(Self::Item) -> bool
18601860
{
18611861
#[inline]
1862-
fn check<T>(mut f: impl FnMut(T) -> bool) -> impl FnMut(T) -> LoopState<(), ()> {
1863-
move |x| {
1862+
fn check<T>(mut f: impl FnMut(T) -> bool) -> impl FnMut((), T) -> LoopState<(), ()> {
1863+
move |(), x| {
18641864
if f(x) { LoopState::Continue(()) }
18651865
else { LoopState::Break(()) }
18661866
}
18671867
}
1868-
1869-
self.try_for_each(check(f)) == LoopState::Continue(())
1868+
self.try_fold((), check(f)) == LoopState::Continue(())
18701869
}
18711870

18721871
/// Tests if any element of the iterator matches a predicate.
@@ -1913,14 +1912,14 @@ pub trait Iterator {
19131912
F: FnMut(Self::Item) -> bool
19141913
{
19151914
#[inline]
1916-
fn check<T>(mut f: impl FnMut(T) -> bool) -> impl FnMut(T) -> LoopState<(), ()> {
1917-
move |x| {
1915+
fn check<T>(mut f: impl FnMut(T) -> bool) -> impl FnMut((), T) -> LoopState<(), ()> {
1916+
move |(), x| {
19181917
if f(x) { LoopState::Break(()) }
19191918
else { LoopState::Continue(()) }
19201919
}
19211920
}
19221921

1923-
self.try_for_each(check(f)) == LoopState::Break(())
1922+
self.try_fold((), check(f)) == LoopState::Break(())
19241923
}
19251924

19261925
/// Searches for an element of an iterator that satisfies a predicate.
@@ -1972,14 +1971,16 @@ pub trait Iterator {
19721971
P: FnMut(&Self::Item) -> bool,
19731972
{
19741973
#[inline]
1975-
fn check<T>(mut predicate: impl FnMut(&T) -> bool) -> impl FnMut(T) -> LoopState<(), T> {
1976-
move |x| {
1974+
fn check<T>(
1975+
mut predicate: impl FnMut(&T) -> bool
1976+
) -> impl FnMut((), T) -> LoopState<(), T> {
1977+
move |(), x| {
19771978
if predicate(&x) { LoopState::Break(x) }
19781979
else { LoopState::Continue(()) }
19791980
}
19801981
}
19811982

1982-
self.try_for_each(check(predicate)).break_value()
1983+
self.try_fold((), check(predicate)).break_value()
19831984
}
19841985

19851986
/// Applies function to the elements of iterator and returns
@@ -2004,14 +2005,14 @@ pub trait Iterator {
20042005
F: FnMut(Self::Item) -> Option<B>,
20052006
{
20062007
#[inline]
2007-
fn check<T, B>(mut f: impl FnMut(T) -> Option<B>) -> impl FnMut(T) -> LoopState<(), B> {
2008-
move |x| match f(x) {
2008+
fn check<T, B>(mut f: impl FnMut(T) -> Option<B>) -> impl FnMut((), T) -> LoopState<(), B> {
2009+
move |(), x| match f(x) {
20092010
Some(x) => LoopState::Break(x),
20102011
None => LoopState::Continue(()),
20112012
}
20122013
}
20132014

2014-
self.try_for_each(check(f)).break_value()
2015+
self.try_fold((), check(f)).break_value()
20152016
}
20162017

20172018
/// Searches for an element in an iterator, returning its index.

0 commit comments

Comments
 (0)