Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplify some Iterator methods. #64572

Closed

Commits on Sep 18, 2019

  1. Simplify some Iterator methods.

    PR rust-lang#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
      rust-lang#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.
    nnethercote committed Sep 18, 2019
    Configuration menu
    Copy the full SHA
    7e98fb9 View commit details
    Browse the repository at this point in the history