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

Specialize iter::ArrayChunks::fold for TrustedRandomAccess iterators #103446

Merged
merged 5 commits into from
Nov 8, 2022

Conversation

the8472
Copy link
Member

@the8472 the8472 commented Oct 23, 2022

OLD:
test iter::bench_trusted_random_access_chunks                      ... bench:         368 ns/iter (+/- 4)
NEW:
test iter::bench_trusted_random_access_chunks                      ... bench:          30 ns/iter (+/- 0)

The resulting assembly is similar to #103166 but the specialization kicks in under different (partially overlapping) conditions compared to that PR. They're complementary.

In principle a TRA-based specialization could be applied to all ArrayChunks methods, including next() as we do for Zip but that would have all the same hazards as the Zip specialization. Only doing it for fold is far less hazardous. The downside is that it only helps with internal, exhaustive iteration. I.e. for _ in or try_fold will not benefit.

Note that the regular, try_fold-based and the specialized fold() impl have observably slightly different behavior. Namely the specialized variant does not fetch the remainder elements from the underlying iterator. We do have a few other places in the standard library where beyond-the-end-of-iteration side-effects are being elided under some circumstances but not others.

Inspired by https://old.reddit.com/r/rust/comments/yaft60/zerocost_iterator_abstractionsnot_so_zerocost/

@rustbot rustbot added the T-libs Relevant to the library team, which will review and decide on the PR/issue. label Oct 23, 2022
@rustbot

This comment was marked as resolved.

@rust-highfive
Copy link
Collaborator

r? @Mark-Simulacrum

(rust-highfive has picked a reviewer for you, use r? to override)

@rust-highfive rust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Oct 23, 2022
@rust-log-analyzer

This comment has been minimized.

@the8472 the8472 force-pushed the tra-array-chunks branch 2 times, most recently from f7a4b02 to be14127 Compare October 23, 2022 19:56
@Mark-Simulacrum
Copy link
Member

Seems broadly okay but I would like to hear your thoughts on especially the second comment around the safety condition.

@Mark-Simulacrum Mark-Simulacrum added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Nov 6, 2022
This also updates the existing iter::Copied::next_chunk benchmark so
that the thing it benches doesn't get masked by the ArrayChunks specialization
This is fairly safe use of TRA since it consumes the iterator so
no struct in an unsafe state will be left exposed to user code
The type is unsafe and now exposed to the whole crate.
Document it properly and add an unsafe method so the
caller can make it visible that something unsafe is happening.
@the8472
Copy link
Member Author

the8472 commented Nov 7, 2022

@bors r=Mark-Simulacrum rollup

@bors
Copy link
Contributor

bors commented Nov 7, 2022

📌 Commit 3925fc0 has been approved by Mark-Simulacrum

It is now in the queue for this repository.

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Nov 7, 2022
@klensy
Copy link
Contributor

klensy commented Nov 7, 2022

Maybe don't rollup optimizations?

@the8472
Copy link
Member Author

the8472 commented Nov 7, 2022

This is unstable API that's not used in the compiler yet, it should be fine.

bors added a commit to rust-lang-ci/rust that referenced this pull request Nov 8, 2022
Rollup of 7 pull requests

Successful merges:

 - rust-lang#103446 (Specialize `iter::ArrayChunks::fold` for TrustedRandomAccess iterators)
 - rust-lang#103651 (Fix `rustc_parse_format` spans following escaped utf-8 multibyte chars)
 - rust-lang#103865 (Move `fallback_has_occurred` state tracking to `FnCtxt`)
 - rust-lang#103955 (Update linker-plugin-lto.md to contain up to Rust 1.65)
 - rust-lang#103987 (Remove `in_tail_expr` from FnCtxt)
 - rust-lang#104067 (fix debuginfo for windows_gnullvm_base.rs)
 - rust-lang#104094 (fully move `on_unimplemented` to `error_reporting`)

Failed merges:

r? `@ghost`
`@rustbot` modify labels: rollup
@bors bors merged commit b695ed3 into rust-lang:master Nov 8, 2022
@rustbot rustbot added this to the 1.67.0 milestone Nov 8, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-libs Relevant to the library team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants