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

Optimize slice_iter.copied().next_chunk() #103166

Merged
merged 2 commits into from
Oct 19, 2022

Conversation

the8472
Copy link
Member

@the8472 the8472 commented Oct 17, 2022

OLD: 
test iter::bench_copied_array_chunks                               ... bench:         371 ns/iter (+/- 7)
NEW:
test iter::bench_copied_array_chunks                               ... bench:          31 ns/iter (+/- 0)

The default next_chunk implementation suffers from having to assemble the array byte by byte via next(), checking the Option<&T> and then dereferencing &T. The specialization copies the chunk directly from the slice.

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

rustbot commented Oct 17, 2022

Hey! It looks like you've submitted a new PR for the library teams!

If this PR contains changes to any rust-lang/rust public library APIs then please comment with @rustbot label +T-libs-api -T-libs to tag it appropriately. If this PR contains changes to any unstable APIs please edit the PR description to add a link to the relevant API Change Proposal or create one if you haven't already. If you're unsure where your change falls no worries, just leave it as is and the reviewer will take a look and make a decision to forward on if necessary.

Examples of T-libs-api changes:

  • Stabilizing library features
  • Introducing insta-stable changes such as new implementations of existing stable traits on existing stable types
  • Introducing new or changing existing unstable library APIs (excluding permanently unstable features / features without a tracking issue)
  • Changing public documentation in ways that create new stability guarantees
  • Changing observable runtime behavior of library APIs

@rust-highfive
Copy link
Collaborator

r? @m-ou-se

(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 17, 2022
@vacuus
Copy link
Contributor

vacuus commented Oct 17, 2022

Just my two cents, use raw_array/raw_arr instead of raw_ary for the variable name. I don't think that'll go over a formatting line length threshold, anyway. Also, the safety comment should have a ; instead of ,.

@rust-log-analyzer

This comment has been minimized.

@the8472 the8472 changed the title Optimize slice_iter.copied.next_chunk Optimize slice_iter.copied().next_chunk() Oct 17, 2022
Copy link
Member

@m-ou-se m-ou-se left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

r=me :)

@m-ou-se m-ou-se 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 Oct 18, 2022
@the8472
Copy link
Member Author

the8472 commented Oct 18, 2022

@bors r=m-ou-se rollup

@bors
Copy link
Contributor

bors commented Oct 18, 2022

📌 Commit 873a18e has been approved by m-ou-se

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 Oct 18, 2022
bors added a commit to rust-lang-ci/rust that referenced this pull request Oct 19, 2022
…iaskrgr

Rollup of 4 pull requests

Successful merges:

 - rust-lang#103166 (Optimize `slice_iter.copied().next_chunk()`)
 - rust-lang#103176 (Fix `TyKind::is_simple_path`)
 - rust-lang#103178 (Partially fix `src/test/run-make/coverage-reports` when cross-compiling)
 - rust-lang#103198 (Update cargo)

Failed merges:

r? `@ghost`
`@rustbot` modify labels: rollup
@bors bors merged commit d6eb7bc into rust-lang:master Oct 19, 2022
@rustbot rustbot added this to the 1.66.0 milestone Oct 19, 2022
Dylan-DPC added a commit to Dylan-DPC/rust that referenced this pull request Nov 8, 2022
…imulacrum

Specialize `iter::ArrayChunks::fold` for TrustedRandomAccess iterators

```
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 rust-lang#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/
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