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

constify some checked arithmetics #65107

Closed
wants to merge 1 commit into from

Conversation

llogiq
Copy link
Contributor

@llogiq llogiq commented Oct 4, 2019

This makes the checked_{add, sub, mul, "neg", "shl", "shr"} operations const fns. Division is a bit more involved. I guess I could solve it with some trickery, but I'm not sure if it would negatively affect performance, so I stopped there.

@rust-highfive
Copy link
Collaborator

r? @TimNN

(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 4, 2019
@llogiq llogiq changed the title constify some const arithmetics constify some checked arithmetics Oct 4, 2019
@tesuji
Copy link
Contributor

tesuji commented Oct 4, 2019

r? @oli-obk

@oli-obk
Copy link
Contributor

oli-obk commented Oct 4, 2019

Oh no 🤣 this PR was bound to happen. I love it and hate it at the same time. I think llvm will clean this up nicely so it's not a perf problem (maybe in debug mode though) but it's still weird code that we'll surely revert once we get const if

cc @ecstatic-morse do you have a timeline on const branching?

I'd really prefer not to do this PR. cc @RalfJung

@ecstatic-morse
Copy link
Contributor

ecstatic-morse commented Oct 4, 2019

See this comment, although perhaps the optimizer has gotten better?

Right now, I consider branching in consts blocked on #63812, but it could be implemented today if there's an urgent need. I can coordinate with @eddyb to drive #63812 forward.

edit:

It has not gotten better, although this tests abs, not checked arithmetic

@Centril
Copy link
Contributor

Centril commented Oct 4, 2019

Right now, I consider branching in consts blocked on #63812, but it could be implemented today if there's an urgent need. I can coordinate with @eddyb to drive #63812 forward.

Please take your time and absolutely do not rush anything even a little bit.

@bors try @rust-timer queue

@rust-timer
Copy link
Collaborator

Awaiting bors try build completion

@bors
Copy link
Contributor

bors commented Oct 4, 2019

⌛ Trying commit 257fb1e with merge 9c45d16...

bors added a commit that referenced this pull request Oct 4, 2019
constify some checked arithmetics

This makes the `checked_`{`add`, `sub`, `mul`, "neg", "shl", "shr"} operations const fns. Division is a bit more involved. I guess I could solve it with some trickery, but I'm not sure if it would negatively affect performance, so I stopped there.
@Centril Centril added const-hack relnotes Marks issues that should be documented in the release notes of the next release. labels Oct 4, 2019
@Centril Centril added this to the 1.40 milestone Oct 4, 2019
@Centril Centril added T-libs-api Relevant to the library API team, which will review and decide on the PR/issue. needs-fcp This change is insta-stable, so needs a completed FCP to proceed. labels Oct 4, 2019
@ecstatic-morse
Copy link
Contributor

@Centril Does rustc-perf test the runtime performance of generated code? Or just the time it takes to compile that code? If it's the latter, I suspect that data won't be very useful here, since I doubt that rustc uses checked arithmetic in a hot loop anywhere. We would want to use something like (the artist formerly known as) lolbench on some numerical libraries written in pure rust.

@Centril
Copy link
Contributor

Centril commented Oct 4, 2019

compile time is being tested; I have no idea if checked arithmetic is being used often but it can't hurt to check I think.

@oli-obk
Copy link
Contributor

oli-obk commented Oct 4, 2019

Please take your time and absolutely do not rush anything even a little bit.

This very much. My question was just that, without any subtext. I also forgot that having the feature unstably won't help these functions on their way to stability.

I propose to close this since it seems to not get optimized as well as the code with branching

@ecstatic-morse
Copy link
Contributor

Please take your time and absolutely do not rush anything even a little bit.

This very much. My question was just that, without any subtext. I also forgot that having the feature unstably won't help these functions on their way to stability.

I propose to close this since it seems to not get optimized as well as the code with branching

Just to clarify, should I or should I not rush? 😆

@llogiq
Copy link
Contributor Author

llogiq commented Oct 4, 2019

@oli-obk I'll benchmark, but I'll take a while. First I need some sleep. 💤

@bors
Copy link
Contributor

bors commented Oct 4, 2019

☀️ Try build successful - checks-azure
Build commit: 9c45d16 (9c45d164d2b4d914b5c53b0f3ddd09438fcd89b8)

@rust-timer
Copy link
Collaborator

Queued 9c45d16 with parent 2e72448, future comparison URL.

@RalfJung
Copy link
Member

RalfJung commented Oct 5, 2019

This is exactly what I expected when #63786 happened. ;)

Ultimately it is up to T-libs if they want to maintain such code.

I also forgot that having the feature unstably won't help these functions on their way to stability.

Why not, with the "use internal unstable" mechanism that we have these days?

@rust-timer
Copy link
Collaborator

Finished benchmarking try commit 9c45d16, comparison URL.

@tspiteri
Copy link
Contributor

The performance does seem to suffer quite a bit, the moves from the indexing do not seem to be optimized out, and llvm-mca reports a regression in reciprocal throughput by a factor of 2.5, from 1.2 to 3.0: https://godbolt.org/z/lYIVmr, so to me this PR does not look like a clear win even ignoring the const-hacky loss in readability.

@tspiteri
Copy link
Contributor

tspiteri commented Oct 11, 2019

Also, I do not see the usefulness of this: these functions return Option, and a constant Option does not have an obvious use case, as the Option needs to be used in a match anyway so it defies the constness point until match can be used in constants, when the hack becomes unnecessary. For constants it is already possible to use the available overflowing_* const methods.

I don't mean that the methods should never be const, just that there is no adequate compensation for the present loss in performance with this implementation.

@RalfJung
Copy link
Member

Also, I do not see the usefulness of this: these functions return Option

Oh, I didn't even realize this.
@llogiq give that Option AFAIK has no const fn methods and match cannot be used in const fn either, what is the point in making these functions const through hacks?

I don't mean that the methods should never be const

Once if works in const fn, we'll certainly make them const as there'll be no cost to that at that point. :)

@llogiq llogiq closed this Oct 11, 2019
@llogiq llogiq deleted the const-checked branch October 11, 2019 14:06
@Centril Centril removed this from the 1.40 milestone Nov 7, 2019
JohnTitor added a commit to JohnTitor/rust that referenced this pull request Dec 30, 2019
Clean up const-hack PRs now that const if / match exist.

Closes rust-lang#67627.

Cleans up these merged PRs tagged with `const-hack`:

- rust-lang#63810
- rust-lang#63786
- rust-lang#61635
- rust-lang#58044

reverting their contents to have the match or if expressions they originally contained.

r? @oli-obk

There's one more PR in those tagged with `const-hack` that originally wasn't merged (rust-lang#65107). Reading the thread, it looks like it was originally closed because the `const-hack` for the checked arithmetic non-negligibly hurt performance, and because there was no way to manipulate the returned Option at compile time anyway (with neither const if nor const match). Would you like me to add these changes to the changes from this PR here too, now that we have the necessary features?
@RalfJung
Copy link
Member

RalfJung commented Aug 7, 2020

Btw, with #68809 these functons are now const.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
needs-fcp This change is insta-stable, so needs a completed FCP to proceed. relnotes Marks issues that should be documented in the release notes of the next release. S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.