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

Use internal iteration in FromIterator and Extend implementations. #43255

Closed
Stebalien opened this issue Jul 15, 2017 · 4 comments
Closed

Use internal iteration in FromIterator and Extend implementations. #43255

Stebalien opened this issue Jul 15, 2017 · 4 comments
Labels
C-enhancement Category: An issue proposing an enhancement or a PR with one. I-slow Issue: Problems and improvements with respect to performance of generated code. T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.

Comments

@Stebalien
Copy link
Contributor

Internal iterators (e.g., Iterator::fold(...)) often optimize better (e.g., in Zip, FlatMap, etc...) as they don't need to repeatedly check the entire state of the iterator every iteration. Given that FromIterator and Extend implementations consume the entire iterator, they could use internal iterators and take advantage of this fact.

@bluss?

(I have no benchmarks or code and don't really have time to do anything but suggest that someone else look into this ATM, sorry.)

@bluss
Copy link
Member

bluss commented Jul 16, 2017

I benchmarked this at some point (in connection to #37306), and sure enough, stuff like .chain() can benefit greatly if .fold() is used in FromIterator (because since chain is a TrustedLen if its parts are).

The nature of the optimization is that it can make a really big difference in its best case (think byte at a time, while checking different chain iterator flags per byte versus memcpy).

I kind of consider it too weird if we have a better loop than the for loop, but the pragmatic thing would be to use whatever is faster internally and work towards a Rust where the for loop is never a worse alternative.

@Stebalien
Copy link
Contributor Author

I wonder if we could make for loops use internal iteration when possible or if that would be way too magical... (I feel dirty even suggesting this). Alternatively, in the distant future, we may be able to use the iterator's internal iterator implementation as a hint to some amazing equivalence proving optimizer.

@Mark-Simulacrum Mark-Simulacrum added I-slow Issue: Problems and improvements with respect to performance of generated code. T-libs-api Relevant to the library API team, which will review and decide on the PR/issue. labels Jul 19, 2017
@Mark-Simulacrum Mark-Simulacrum added the C-enhancement Category: An issue proposing an enhancement or a PR with one. label Jul 26, 2017
@steveklabnik
Copy link
Member

Triage: I'm not aware of any movement on this issue. @bluss's comment seems to indicate that this is an improvement if we could make it happen.

@Stebalien
Copy link
Contributor Author

This looks like it has been implemented. At least in Vec, HashMap, BTreeMap, and String.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-enhancement Category: An issue proposing an enhancement or a PR with one. I-slow Issue: Problems and improvements with respect to performance of generated code. T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

4 participants