reduce repetition with macros and document overrides of the default impl of PartialEq::ne#143377
reduce repetition with macros and document overrides of the default impl of PartialEq::ne#143377hkBst wants to merge 3 commits intorust-lang:mainfrom
Conversation
Note that implementations for reference types are not strictly the same as the default because they are forwarding of the underlying implementation. In such cases, if the compiler cannot optimize the inversion of the result from the underlying implementation (e.g., inline assembly or FFI is used), then |
|
@taiki-e that is a good point, thanks! |
fe36c54 to
eac6335
Compare
This comment has been minimized.
This comment has been minimized.
eac6335 to
4489647
Compare
4489647 to
88f2556
Compare
|
I don't think we should add macros to appease the thing that is linting in an effort to make the code more readable. Adding macros is rather the opposite of making the code more readable. |
|
@workingjubilee macros like these are used all over the library for repeating impls just like here... Do you feel the same way about those, or can you explain why you think these are any different? |
|
If you look you will find that I have pushed people to reduce the usage of macros in other PRs, yes. |
|
Very well, I was just trying to clarify your point. Macros come with the disadvantage of more complicated code, but one advantage of using macros like this is that it is easier to see which code is identically duplicated and which parts contain differences. Opinions will vary on in which cases the upside outweighs the downside. My latest change introduced 3 macros:
|
|
I don't have any strong preferences here r? workingjubilee |
|
|
59bece7 to
3c1c6e9
Compare
|
☔ The latest upstream changes (presumably #143867) made this pull request unmergeable. Please resolve the merge conflicts. |
3c1c6e9 to
9c60f35
Compare
| #[inline] | ||
| fn ne(&self, other: &Self) -> bool { *self != *other } |
There was a problem hiding this comment.
Primitive ne isn't equivalent to !(*self == *other). This difference goes straight to MIR and codegen:
rust/compiler/rustc_codegen_ssa/src/mir/rvalue.rs
Lines 822 to 833 in 0fb279b
Yes it can be trivially optimized away but why emit additional IR when you can emit less?
There was a problem hiding this comment.
Interesting! I suppose that treatment may be necessary in case these are implemented by inline assembly or via FFI.
Yes it can be trivially optimized away but why emit additional IR when you can emit less?
The documentation in this very file says that:
/// The default implementation of `ne` provides this consistency and is almost
/// always sufficient. It should not be overridden without very good reason.
Is the performance impact of emitting slightly more IR really a very good reason? I would expect it is on the edge of immeasurable.
There was a problem hiding this comment.
Is the performance impact of emitting slightly more IR really a very good reason? I would expect it is on the edge of immeasurable.
That is a very interesting question since we do in fact have some measurements! Let's at least find out if our existing ones would say anything.
There was a problem hiding this comment.
My reservation wasn't mainly about the performance impact. I'm thinking of alternative codegen backends that read Rust MIR and generate code with instruction sets that have ne as well as eq for primitives.
Rather than telling people to rely on MIR opts or LLVM opts (when the backend may not be LLVM) I'd prefer us to do something better on the outset.
The other thing is that it just feels weird? I just don't think clippy is very applicable to this specific case.
There was a problem hiding this comment.
And like, what's the benefit here? Of course we don't have to be biased towards the status quo but what is the difference between the change and the status quo?
These are just two lines in a macro. Removing them isn't too much different from keeping them.
There was a problem hiding this comment.
The difference would be that the code would then do what the comment about implementing ne seems to recommend. Removing ne is just one of three ways of achieving that, and merely the one that seemed most likely to work at the time. But I'm also happy to document the "very good reason" why we want to impl ne here, or change the comment or both.
There was a problem hiding this comment.
I've now worked a bit on the documentation, mentioning primitive impls of == and !=. The only removed ne impl now is the one for ()...
|
☔ The latest upstream changes (presumably #144997) made this pull request unmergeable. Please resolve the merge conflicts. |
9c60f35 to
187c020
Compare
This comment has been minimized.
This comment has been minimized.
|
The question of measurable performance impact if any was raised so we might as well test this much, even if it's not the be-all end-all of performance testing since our runtime benches remain lacking: @bors try @rust-timer queue |
This comment has been minimized.
This comment has been minimized.
clippy fix: remove manual PartialEq::ne
document overrides of the default impl of PartialEq::ne <del>Removes manual impls of PartialEq::ne which are equivalent to the default.</del> - Cleans up documentation of `PartialEq::ne` and documents overrides of the default impl. - Uses macros to remove duplication.
|
Failed in rollup: #149828 (comment) @bors r- This failure was hard to track down, because it was caused by a code change in a PR that was labelled as a docs update. |
@Zalathar Apologies for leading you astray. In my defense, this failure seems to have been caused by ui tests that didn't exist when this change set was last tested, which included the name of an internal macro that got renamed in this change set. |
|
You're right, @Zalathar, the PR title and description should be accurate instead of misleading. |
6912f15 to
cc8c43d
Compare
This comment has been minimized.
This comment has been minimized.
|
☔ The latest upstream changes (presumably #149891) made this pull request unmergeable. Please resolve the merge conflicts. |
cc8c43d to
6be8deb
Compare
|
This PR was rebased onto a different main commit. Here's a range-diff highlighting what actually changed. Rebasing is a normal part of keeping PRs up to date, so no action is needed—this note is just to help reviewers. |
|
@rustbot ready |
|
I've improved the headline, so it can't be mistaken for a docs only commit so easily... |
|
@workingjubilee Anything else you need to push this over the line (again)? |
|
It seems I have been finding half-reasons to put off looking at this despite doing other reviews. Partially because I find the conclusion regrettable as I allowed too much work to occur before getting to it. Partially because I was hoping my conclusion was just some passing mood. And, er, it seems it hasn't passed. Over the course of this I had been trying to persuade myself that I should be reasonable, and that a significantly cut-down form of the original PR could have some merit here. But in this case that changed when this finally bounced on CI another time, I felt like I had to insist on an update to the PR description, and then saw the expansion of the subject to include two things that feel unrelated. All together, that managed to eat the last of my belief that this is a good PR to land, even in a reduced form. What of this is improvement versus change for change's sake, I can no longer tell, nor do I think we will benefit by sending it in and letting future revisions iterate on top of it. I must close this. My apologies for letting this drag out. |
|
@workingjubilee Thanks for letting me know. I suppose I shall try to separate this change into parts and submit those separately in order to salvage the value that I think is in here. I hope you'll not mind my doing so. |
|
The code changes introduced here do not seem to have sufficient merit, to me, to warrant their inclusion, even when separated. Mere avoidance of repetition is not a virtue if it also obfuscates things. |
|
@hkBst In short: I do mind. If I can only convince you to submit smaller and smaller, more atomized PRs, and you then reviewer-shop until you find someone who agrees, then I do not have any actual discretion here. My rejection meaning nothing means my approval means nothing. And apparently I should not ever again explain my thought process around declining a PR lest it be interpreted as mere suggestion. |
Removes manual impls of PartialEq::ne which are equivalent to the default.PartialEq::neand documents overrides of the default impl.