Skip to content

Speedup heapsort by 1.8x by making it branchless #107894

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

Merged
merged 2 commits into from
Feb 12, 2023

Conversation

Voultapher
Copy link
Contributor

@Voultapher Voultapher commented Feb 10, 2023

slice::sort_unstable will fall back to heapsort if it repeatedly fails to find a good pivot. By making the core child update code branchless it is much faster. On Zen3 sorting 10k u64 and forcing the sort to pick heapsort, results in:

455us -> 278us

455us -> 249us

`slice::sort_unstable` will fall back to heapsort if it repeatedly fails to find
a good pivot. By making the core child update code branchless it is much faster.
On Zen3 sorting 10k `u64` and forcing the sort to pick heapsort, results in:

455us -> 278us
@rustbot
Copy link
Collaborator

rustbot commented Feb 10, 2023

r? @scottmcm

(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 Feb 10, 2023
@rustbot
Copy link
Collaborator

rustbot commented Feb 10, 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

@Voultapher
Copy link
Contributor Author

r? thomcc

@rustbot rustbot assigned thomcc and unassigned scottmcm Feb 10, 2023
This allows even better code-gen, cmp + adc. While also more clearly
communicating the intent.
@Voultapher
Copy link
Contributor Author

r? @scottmcm

@rustbot rustbot assigned scottmcm and unassigned thomcc Feb 11, 2023
@scottmcm
Copy link
Member

Thanks for benching it!

@bors r+

(Ok to rollup because this is in the fallback sorting/selecting path, so is unlikely to matter for compiler perf.)

@bors
Copy link
Collaborator

bors commented Feb 11, 2023

📌 Commit ee0376c 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 Feb 11, 2023
@thomcc
Copy link
Member

thomcc commented Feb 11, 2023

@bors rollup=never

@thomcc
Copy link
Member

thomcc commented Feb 11, 2023

@scottmcm Not sure I agree — that's true but compiler is huge and it's hard to say what matters. Better safe than sorry IMO.

@scottmcm
Copy link
Member

@thomcc Sure, no objections, especially since the queue isn't too long right now.

@bors
Copy link
Collaborator

bors commented Feb 12, 2023

⌛ Testing commit ee0376c with merge b7089e0...

@bors
Copy link
Collaborator

bors commented Feb 12, 2023

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

1 similar comment
@bors
Copy link
Collaborator

bors commented Feb 12, 2023

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

@bors bors added the merged-by-bors This PR was explicitly merged by bors. label Feb 12, 2023
@bors bors merged commit b7089e0 into rust-lang:master Feb 12, 2023
@rustbot rustbot added this to the 1.69.0 milestone Feb 12, 2023
@rust-timer
Copy link
Collaborator

Finished benchmarking commit (b7089e0): comparison URL.

Overall result: no relevant changes - no action needed

@rustbot label: -perf-regression

Instruction count

This benchmark run did not return any relevant results for this metric.

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)
- - 0
Regressions ❌
(secondary)
1.6% [1.6%, 1.6%] 1
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
-3.3% [-3.3%, -3.3%] 1
All ❌✅ (primary) - - 0

Cycles

This benchmark run did not return any relevant results for this metric.

@Voultapher Voultapher changed the title Speedup heapsort by 1.5x by making it branchless Speedup heapsort by 1.8x by making it branchless May 14, 2023
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.

6 participants