Skip to content

Override Iterator methods when possible in std #24214

Closed

Description

@aturon

Now that Iterator has re-emerged as a single trait, it's possible to override default methods like count, last and nth for iterators on slices, vectors, and so on. We should audit the applicable iterators and take advantage of overrides when possible.

libcore:

Activity

  1. added
    E-easyCall for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue.
    I-slowIssue: Problems and improvements with respect to performance of generated code.
    E-mentorCall for participation: This issue has a mentor. Use #t-compiler/help on Zulip for discussion.
    on Apr 8, 2015
  2. aturon commented on Apr 8, 2015

    @aturon
    ContributorAuthor

    I'd be happy to mentor work on this bug.

  3. djmally commented on Apr 9, 2015

    @djmally
    Contributor

    I would be interested in working on this.

  4. GuillaumeGomez commented on Apr 9, 2015

    @GuillaumeGomez
    Member

    Me too.

  5. tamird commented on Apr 17, 2015

    @tamird
    Contributor

    Also interested in working on this. Anything left to do?

  6. djmally commented on Apr 20, 2015

    @djmally
    Contributor

    @aturon Any word on this?

  7. pnkfelix commented on Apr 20, 2015

    @pnkfelix
    Contributor

    Also, I'm curious if methods like skip(n) are required to actually call next n tines, or if they are allowed to "jump ahead" on random-access iterators. (Maybe that sort of trick will have to wait for specialization in the language, to avoid composing that with e.g. a map or other potentially side-effectful iterator

  8. Stebalien commented on Apr 21, 2015

    @Stebalien
    Contributor

    @pnkfelix skip can call nth on the underlying iterator and the underlying iterator can optimize nth if possible.

  9. pnkfelix commented on Apr 21, 2015

    @pnkfelix
    Contributor

    @Stebalien I think that is my point: You say "the underlying iterator can optimize nth if possible", but I claim to allow that would require changing the specification of nth, which currently is specified as "going through n iterations", which I interpret as "calls the next() method n times" -- a side-effectful iterator chain (e.g. one using inspect) can observe the difference between that or jumping ahead n steps when given a random-access iterator.

  10. aturon commented on Apr 21, 2015

    @aturon
    ContributorAuthor

    Oh dear, I seem to've lost track of this thread, sorry to everyone who was waiting on me to organize things!

    I think probably the first place to start is just to get a list of the public iterators in std, which we can turn into a checklist and track assignees for. Part of the work will just be figuring out which cases are actually opportunities for doing better.

    Anyone interested in putting such a list together? I'd be glad to help coordinate.

  11. aturon commented on Apr 21, 2015

    @aturon
    ContributorAuthor

    (And if no one is in a position to make a list, I can put one together tomorrow.)

  12. Stebalien commented on Apr 21, 2015

    @Stebalien
    Contributor

    @pnkfelix It doesn't because:

    1. Skip can assume that the underlying iterator correctly implements nth.
    2. Some iterators (e.g. slice::Iter) have side-effect-free next methods. These iterators can implement optimized nth functions.

    So vec![1, 2, 3, 4, 5].iter().skip(2) can have a constant time skip given an optimized slice::Iter::nth method. Unfortunately, as implemented, ranges can't optimize nth this way because they are generic.

  13. 37 remaining items

  14. added a commit that references this issue on Jan 20, 2016
  15. removed
    E-help-wantedCall for participation: Help is requested to fix this issue.
    on Feb 29, 2016
  16. added a commit that references this issue on Apr 20, 2016
  17. added a commit that references this issue on May 19, 2016
  18. added a commit that references this issue on May 28, 2016
  19. brson commented on Jun 27, 2016

    @brson
    Contributor

    From comments it appears this is done. Thanks all.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    E-easyCall for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue.E-mentorCall for participation: This issue has a mentor. Use #t-compiler/help on Zulip for discussion.I-slowIssue: Problems and improvements with respect to performance of generated code.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions