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

Make [u8]::cmp implementation branchless #93962

Merged
merged 1 commit into from
Feb 16, 2022

Conversation

joboet
Copy link
Contributor

@joboet joboet commented Feb 13, 2022

The current implementation generates rather ugly assembly code, branching when the common parts are equal. By performing the comparison of the lengths upfront using a subtraction, the assembly gets much prettier: https://godbolt.org/z/4e5fnEKGd.

This will probably not impact speed too much, as the expensive part is in most cases the memcmp, but it sure looks better (I'm porting a sorting algorithm currently, and that branch just bothered me).

@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 Feb 13, 2022
@rust-log-analyzer

This comment has been minimized.

@the8472
Copy link
Member

the8472 commented Feb 13, 2022

Also shaves off a few instructions on short arrays. https://godbolt.org/z/hGzb5aKPq

@joboet
Copy link
Contributor Author

joboet commented Feb 14, 2022

@rustbot label T-libs

@rustbot rustbot added the T-libs Relevant to the library team, which will review and decide on the PR/issue. label Feb 14, 2022
@Mark-Simulacrum
Copy link
Member

@bors try @rust-timer queue

@rust-timer
Copy link
Collaborator

Awaiting bors try build completion.

@rustbot label: +S-waiting-on-perf

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

bors commented Feb 14, 2022

⌛ Trying commit 71e00a43760178698ab4431670de13ea0cfbfcee with merge 582fba747804a832de267609ab4837f67d268f3a...

@bors
Copy link
Contributor

bors commented Feb 14, 2022

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

@rust-timer
Copy link
Collaborator

Queued 582fba747804a832de267609ab4837f67d268f3a with parent 52dd59e, future comparison URL.

@rust-timer
Copy link
Collaborator

Finished benchmarking commit (582fba747804a832de267609ab4837f67d268f3a): comparison url.

Summary: This benchmark run did not return any relevant results.

If you disagree with this performance assessment, please file an issue in rust-lang/rustc-perf.

Benchmarking this pull request likely means that it is perf-sensitive, so we're automatically marking it as not fit for rolling up. While you can manually mark this PR as fit for rollup, we strongly recommend not doing so since this PR led to changes in compiler perf.

@bors rollup=never
@rustbot label: +S-waiting-on-review -S-waiting-on-perf -perf-regression

@rustbot rustbot removed the S-waiting-on-perf Status: Waiting on a perf run to be completed. label Feb 14, 2022
@Mark-Simulacrum
Copy link
Member

r=me with commits squashed

@Mark-Simulacrum
Copy link
Member

@bors r+ rollup

@bors
Copy link
Contributor

bors commented Feb 16, 2022

📌 Commit 3960ce6 has been approved by Mark-Simulacrum

@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 16, 2022
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this pull request Feb 16, 2022
…-Simulacrum

Make [u8]::cmp implementation branchless

The current implementation generates rather ugly assembly code, branching when the common parts are equal. By performing the comparison of the lengths upfront using a subtraction, the assembly gets much prettier: https://godbolt.org/z/4e5fnEKGd.

This will probably not impact speed too much, as the expensive part is in most cases the `memcmp`, but it sure looks better (I'm porting a sorting algorithm currently, and that branch just bothered me).
bors added a commit to rust-lang-ci/rust that referenced this pull request Feb 16, 2022
…askrgr

Rollup of 10 pull requests

Successful merges:

 - rust-lang#92366 (Resolve concern of `derive_default_enum`)
 - rust-lang#93382 (Add a bit more padding in search box)
 - rust-lang#93962 (Make [u8]::cmp implementation branchless)
 - rust-lang#94015 (rustdoc --check option documentation)
 - rust-lang#94017 (Clarify confusing UB statement in MIR)
 - rust-lang#94020 (Support pretty printing of invalid constants)
 - rust-lang#94027 (Update browser UI test version)
 - rust-lang#94037 (Fix inconsistent symbol mangling with -Zverbose)
 - rust-lang#94045 (Update books)
 - rust-lang#94054 (:arrow_up: rust-analyzer)

Failed merges:

r? `@ghost`
`@rustbot` modify labels: rollup
@bors bors merged commit 9a42121 into rust-lang:master Feb 16, 2022
@rustbot rustbot added this to the 1.60.0 milestone Feb 16, 2022
@joboet joboet deleted the branchless_slice_ord branch June 25, 2022 06:58
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.

None yet

8 participants