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

improve performance of signed saturating_mul #65312

Merged
merged 1 commit into from
Oct 13, 2019

Conversation

tspiteri
Copy link
Contributor

Reciprocal throughput is improved from 2.3 to 1.7. https://godbolt.org/z/ROMiX6

Fixes #65309.

Reciprocal throughput is improved from 2.3 to 1.7.
https://godbolt.org/z/ROMiX6
@rust-highfive
Copy link
Collaborator

r? @dtolnay

(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 11, 2019
@nagisa
Copy link
Member

nagisa commented Oct 11, 2019

@bors r+

@bors
Copy link
Contributor

bors commented Oct 11, 2019

📌 Commit 57aae75 has been approved by nagisa

@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 Oct 11, 2019
@tspiteri
Copy link
Contributor Author

The code I showed in the issue is equivalent to also changing unwrap_or_else into unwrap_or, but I don't see any clear benefits there. While it does remove the branch, the reciprocal throughput remains unchanged, and I'm not sure if it can have any adverse effects with for example inlining; so I'm not changing that. https://godbolt.org/z/OqYAJ6

Copy link
Member

@dtolnay dtolnay left a comment

Choose a reason for hiding this comment

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

Thanks!

Reasoning through the implementation: the new code is equivalent to:

(self < 0 && rhs < 0) || (self >= 0 && rhs >= 0)

but we know self != 0 and rhs != 0 or else the checked_mul would not have overflowed.

@dtolnay
Copy link
Member

dtolnay commented Oct 11, 2019

@bors r+

@bors
Copy link
Contributor

bors commented Oct 11, 2019

💡 This pull request was already approved, no need to approve it again.

@bors
Copy link
Contributor

bors commented Oct 11, 2019

📌 Commit 57aae75 has been approved by dtolnay

@dtolnay
Copy link
Member

dtolnay commented Oct 11, 2019

r? @nagisa

@rust-highfive rust-highfive assigned nagisa and unassigned dtolnay Oct 11, 2019
@@ -1058,7 +1058,7 @@ $EndFeature, "
#[inline]
pub fn saturating_mul(self, rhs: Self) -> Self {
self.checked_mul(rhs).unwrap_or_else(|| {
if (self < 0 && rhs < 0) || (self > 0 && rhs > 0) {
if (self < 0) == (rhs < 0) {
Copy link
Member

Choose a reason for hiding this comment

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

This is actually also more readable than before IMO, nice :)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

And in this case I think constness just has to wait :)

Centril added a commit to Centril/rust that referenced this pull request Oct 13, 2019
improve performance of signed saturating_mul

Reciprocal throughput is improved from 2.3 to 1.7. https://godbolt.org/z/ROMiX6

Fixes rust-lang#65309.
Centril added a commit to Centril/rust that referenced this pull request Oct 13, 2019
improve performance of signed saturating_mul

Reciprocal throughput is improved from 2.3 to 1.7. https://godbolt.org/z/ROMiX6

Fixes rust-lang#65309.
bors added a commit that referenced this pull request Oct 13, 2019
Rollup of 10 pull requests

Successful merges:

 - #65214 (Split non-CAS atomic support off into target_has_atomic_load_store)
 - #65246 (vxWorks: implement get_path() and get_mode() for File fmt::Debug)
 - #65312 (improve performance of signed saturating_mul)
 - #65336 (Fix typo in task::Waker)
 - #65346 (nounwind tests and cleanup)
 - #65347 (Fix #[unwind(abort)] with Rust ABI)
 - #65366 (Implement Error::source on IntoStringError + Remove superfluous cause impls)
 - #65369 (Don't discard value names when using address or memory sanitizer)
 - #65370 (Add `dyn` to `Any` documentation)
 - #65373 (Fix typo in docs for `Rc`)

Failed merges:

r? @ghost
@bors bors merged commit 57aae75 into rust-lang:master Oct 13, 2019
@tspiteri tspiteri deleted the signed-sat-mul branch October 17, 2019 10:05
@dtolnay dtolnay self-assigned this Mar 24, 2024
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.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

performance of saturating_mul can be improved by removing branches
6 participants