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::fold #106343

Merged
merged 4 commits into from
Jun 15, 2023
Merged

optimize slice::Iter::fold #106343

merged 4 commits into from
Jun 15, 2023

Conversation

the8472
Copy link
Member

@the8472 the8472 commented Jan 1, 2023

Fixes 2 of 4 cases from #106288

OLD: test slice::fold_to_last                                           ... bench:         248 ns/iter (+/- 3)
NEW: test slice::fold_to_last                                           ... bench:           0 ns/iter (+/- 0)

@rustbot
Copy link
Collaborator

rustbot commented Jan 1, 2023

r? @joshtriplett

(rustbot has picked a reviewer for you, use r? to override)

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-libs Relevant to the library team, which will review and decide on the PR/issue. labels Jan 1, 2023
@rustbot
Copy link
Collaborator

rustbot commented Jan 1, 2023

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

@the8472
Copy link
Member Author

the8472 commented Jan 1, 2023

@bors try @rust-timer queue

@rust-timer

This comment has been minimized.

@rustbot rustbot added the S-waiting-on-perf Status: Waiting on a perf run to be completed. label Jan 1, 2023
@bors
Copy link
Contributor

bors commented Jan 1, 2023

⌛ Trying commit 6a6a41907748388572966d5f214133a8b4efefd5 with merge 3fad16f536f1c5c74f8d8c80d98c3ea988f1e33b...

@bors
Copy link
Contributor

bors commented Jan 1, 2023

💔 Test failed - checks-actions

@bors bors 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 Jan 1, 2023
@the8472
Copy link
Member Author

the8472 commented Jan 1, 2023

@bors try

@bors
Copy link
Contributor

bors commented Jan 1, 2023

⌛ Trying commit e42a42b95ba5c8c3a30fc24badcae06bb0c53a3f with merge d8fe6702c1e876077d9db9bdfe7b8756d8d7de6e...

@rust-log-analyzer

This comment has been minimized.

@bors
Copy link
Contributor

bors commented Jan 1, 2023

☀️ Try build successful - checks-actions
Build commit: d8fe6702c1e876077d9db9bdfe7b8756d8d7de6e (d8fe6702c1e876077d9db9bdfe7b8756d8d7de6e)

@rust-timer

This comment has been minimized.

@rust-timer

This comment was marked as outdated.

@rustbot rustbot added perf-regression Performance regression. and removed S-waiting-on-perf Status: Waiting on a perf run to be completed. labels Jan 1, 2023
@the8472 the8472 closed this Jan 1, 2023
@the8472 the8472 reopened this Jan 2, 2023
@the8472 the8472 marked this pull request as draft January 2, 2023 04:19
@the8472
Copy link
Member Author

the8472 commented Jan 2, 2023

@bors try @rust-timer queue

@rust-timer

This comment has been minimized.

@jyn514
Copy link
Member

jyn514 commented Jun 13, 2023

i don't think i'm a good reviewer for libs PRs 😅 the old code was simpler iirc

r? @scottmcm

@rustbot rustbot assigned scottmcm and unassigned jyn514 Jun 13, 2023
loop {
// SAFETY: the loop iterates `i in 0..len`, which always is in bounds of
// the slice allocation
acc = f(acc, unsafe { & $( $mut_ )? *self.ptr.add(i).as_ptr() });
Copy link
Member

Choose a reason for hiding this comment

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

How much of the duplication here is essential?

Avoiding the Option loop seems entirely reasonable to me, but how does this approach compare to something much shorter? For example, something like

for _ in 0..len!(self) {
    acc = f(acc, next_unchecked!(self));
}

Or the same with a while (n --> 0) loop or something, if the Range is not ok.

And if that's not sufficient, it would be nice to have some comments here about why it's written this way.

(Also, if overloading fold is worth it, I expect rfold should be overridden too.)

@rustbot author

Copy link
Member Author

Choose a reason for hiding this comment

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

If you look at the PR history you'll see that I tried several approaches. A for-in-range loop was one of them. I even used IndexRange instead of Range.

And if that's not sufficient, it would be nice to have some comments here about why it's written this way.

Ok, will do.

(Also, if overloading fold is worth it, I expect rfold should be overridden too.)

Maybe, but that'd be optimizing two things at once which makes assessing perf more complicated.

Copy link
Member Author

Choose a reason for hiding this comment

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

Added a comment.

@rustbot ready

@rustbot rustbot 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 Jun 14, 2023
this seems to produce less IR
@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Jun 14, 2023
{
// this implementation consists of the following optimizations compared to the
// default implementation:
// - do-while loop, as is llvm's preferred loop shape,
Copy link
Member

Choose a reason for hiding this comment

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

It's very weird to me that doing this manually is needed, since LLVM has a loop rotation pass to do this. But I guess it's fine.

@scottmcm
Copy link
Member

@bors r+ rollup=never

@bors
Copy link
Contributor

bors commented Jun 15, 2023

📌 Commit d90508f has been approved by scottmcm

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-review Status: Awaiting review from the assignee but also interested parties. labels Jun 15, 2023
Comment on lines -40 to -47
// CHECK-NOT: panic

// Call to panic_cannot_unwind in case of double-panic is expected,
// on LLVM 16 and older, but other panics are not.
// old: filter
// old-NEXT: ; call core::panicking::panic_cannot_unwind
// old-NEXT: panic_cannot_unwind

Copy link
Contributor

@klensy klensy Jun 15, 2023

Choose a reason for hiding this comment

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

Now there panic? Or why this was killed.
Ohh, i see it lower, my bad.

@bors
Copy link
Contributor

bors commented Jun 15, 2023

⌛ Testing commit d90508f with merge 4996b56...

@bors
Copy link
Contributor

bors commented Jun 15, 2023

☀️ Test successful - checks-actions
Approved by: scottmcm
Pushing 4996b56 to master...

1 similar comment
@bors
Copy link
Contributor

bors commented Jun 15, 2023

☀️ Test successful - checks-actions
Approved by: scottmcm
Pushing 4996b56 to master...

@bors bors added merged-by-bors This PR was explicitly merged by bors. labels Jun 15, 2023
@bors bors merged commit 4996b56 into rust-lang:master Jun 15, 2023
@rustbot rustbot added this to the 1.72.0 milestone Jun 15, 2023
@rust-timer
Copy link
Collaborator

Finished benchmarking commit (4996b56): comparison URL.

Overall result: ✅ improvements - no action needed

@rustbot label: -perf-regression

Instruction count

This is a highly reliable metric that was used to determine the overall result at the top of this comment.

mean range count
Regressions ❌
(primary)
0.7% [0.4%, 1.1%] 5
Regressions ❌
(secondary)
- - 0
Improvements ✅
(primary)
-0.2% [-0.4%, -0.2%] 87
Improvements ✅
(secondary)
-0.3% [-1.2%, -0.1%] 24
All ❌✅ (primary) -0.2% [-0.4%, 1.1%] 92

Max RSS (memory usage)

Results

This is a less reliable metric that may be of interest but was not used to determine the overall result at the top of this comment.

mean range count
Regressions ❌
(primary)
4.3% [2.1%, 6.0%] 4
Regressions ❌
(secondary)
1.3% [1.3%, 1.3%] 1
Improvements ✅
(primary)
-4.1% [-7.1%, -1.8%] 3
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) 0.7% [-7.1%, 6.0%] 7

Cycles

Results

This is a less reliable metric that may be of interest but was not used to determine the overall result at the top of this comment.

mean range count
Regressions ❌
(primary)
1.2% [1.2%, 1.2%] 1
Regressions ❌
(secondary)
- - 0
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) 1.2% [1.2%, 1.2%] 1

Binary size

Results

This is a less reliable metric that may be of interest but was not used to determine the overall result at the top of this comment.

mean range count
Regressions ❌
(primary)
0.2% [0.0%, 0.6%] 45
Regressions ❌
(secondary)
- - 0
Improvements ✅
(primary)
-0.4% [-0.4%, -0.4%] 7
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) 0.1% [-0.4%, 0.6%] 52

Bootstrap: 646.601s -> 647.802s (0.19%)

bors added a commit to rust-lang-ci/rust that referenced this pull request Dec 22, 2023
…<try>

Update `slice::Iter::rfold` to match `slice::Iter::fold`

Adds a new codegen test for `rfold`, like the one from rust-lang#106343, and makes a similar fix, updating `rfold` to work via indices too.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
merged-by-bors This PR was explicitly merged by bors. 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.

9 participants