You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Auto merge of #64572 - nnethercote:simplify-Iterator-methods, r=<try>
Simplify some `Iterator` methods.
PR #64545 got a big speed-up by replacing a hot call to `all()` with
explicit iteration. This is because the implementation of `all()` is
excessively complex: it wraps the given predicate in a closure that
returns a `LoopState`, passes that closure to `try_for_each()`, which
wraps the first closure in a second closure, passes that second closure
to `try_fold()`, which does the actual iteration using the second
closure.
A sufficient smart compiler could optimize all this away; rustc is
currently not sufficiently smart.
This commit does the following.
- Changes the implementations of `all()`, `any()`, `find()` and
`find_map()` to use the simplest possible code, rather than using
`try_for_each()`. (I am reminded of "The Evolution of a Haskell
Programmer".) These are both shorter and faster than the current
implementations, and will permit the undoing of the `all()` removal in
#64545.
- Changes `ResultShunt::next()` so it doesn't call `self.find()`,
because that was causing infinite recursion with the new
implementation of `find()`, which itself calls `self.next()`. (I
honestly don't know how the old implementation of
`ResultShunt::next()` didn't cause an infinite loop, given that it
also called `self.next()`, albeit via `try_for_each()` and
`try_fold()`.)
- Changes `nth()` to use `self.next()` in a while loop rather than `for
x in self`, because using self-iteration within an iterator method
seems dubious, and `self.next()` is used in all the other iterator
methods.
0 commit comments