-
Notifications
You must be signed in to change notification settings - Fork 13k
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
Conversation
r? @TimNN (rust_highfive has picked a reviewer for you, use r? to override) |
r? @oli-obk |
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 cc @ecstatic-morse do you have a timeline on const branching? I'd really prefer not to do this PR. cc @RalfJung |
See this comment, 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 |
Please take your time and absolutely do not rush anything even a little bit. @bors try @rust-timer queue |
Awaiting bors try build completion |
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 Does |
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. |
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? 😆 |
@oli-obk I'll benchmark, but I'll take a while. First I need some sleep. 💤 |
☀️ Try build successful - checks-azure |
Queued 9c45d16 with parent 2e72448, future comparison URL. |
This is exactly what I expected when #63786 happened. ;) Ultimately it is up to T-libs if they want to maintain such code.
Why not, with the "use internal unstable" mechanism that we have these days? |
Finished benchmarking try commit 9c45d16, comparison URL. |
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. |
Also, I do not see the usefulness of this: these functions return 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. |
Oh, I didn't even realize this.
Once |
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?
Btw, with #68809 these functons are now |
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.