Skip to content

Explicitly deref in float partial_eq #97943

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

Merged
merged 2 commits into from
Jun 11, 2022
Merged

Explicitly deref in float partial_eq #97943

merged 2 commits into from
Jun 11, 2022

Conversation

Warrenren
Copy link
Contributor

The current code will not results bug, but it difficult to understand. These code result to call &f32::partial_cmp(), and the performance will be lower than the changed code. I'm not sure why the current code don't use (*self) (*other), if you have some idea, please let me know.

The current code will not results bug, but it difficult to understand. These code result to call &f32::partial_cmp(), and the performance will be lower than the changed code. I'm not sure why the current code don't use (*self) (*other), if you have some idea, please let me know.
@rust-highfive
Copy link
Contributor

Hey! It looks like you've submitted a new PR for the library teams!

If this PR contains changes to any rust-lang/rust public library APIs then please comment with @rustbot label +T-libs-api -T-libs to tag it appropriately. If this PR contains changes to any unstable APIs please edit the PR description to add a link to the relevant API Change Proposal or create one if you haven't already. If you're unsure where your change falls no worries, just leave it as is and the reviewer will take a look and make a decision to forward on if necessary.

Examples of T-libs-api changes:

  • Stabilizing library features
  • Introducing insta-stable changes such as new implementations of existing stable traits on existing stable types
  • Introducing new or changing existing unstable library APIs (excluding permanently unstable features / features without a tracking issue)
  • Changing public documentation in ways that create new stability guarantees
  • Changing observable runtime behavior of library APIs

@rustbot rustbot added the T-libs Relevant to the library team, which will review and decide on the PR/issue. label Jun 10, 2022
@rust-highfive
Copy link
Contributor

Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @kennytm (or someone else) soon.

Please see the contribution instructions for more information.

@rust-highfive rust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Jun 10, 2022
@eggyal
Copy link
Contributor

eggyal commented Jun 10, 2022

There isn't an implementation of PartialOrd for &f32, so the values are implicitly dereferenced.

@Warrenren
Copy link
Contributor Author

When I debug, the code go to the following implementation. And this for all &T which implement PartialOrd. So f32::partial_cmp call &f32::le() and &f32::ge(). To avoid going here, should change "self" to "*self" and "other" to "*other"

impl<A: ?Sized, B: ?Sized> PartialOrd<&B> for &A
where
A: PartialOrd,
{
#[inline]
fn partial_cmp(&self, other: &&B) -> Option {
PartialOrd::partial_cmp(*self, *other)
}
#[inline]
fn lt(&self, other: &&B) -> bool {
PartialOrd::lt(*self, *other)
}
#[inline]
fn le(&self, other: &&B) -> bool {
PartialOrd::le(*self, *other)
}
#[inline]
fn gt(&self, other: &&B) -> bool {
PartialOrd::gt(*self, *other)
}
#[inline]
fn ge(&self, other: &&B) -> bool {
PartialOrd::ge(*self, *other)
}
}

@kadiwa4
Copy link
Contributor

kadiwa4 commented Jun 10, 2022

There is such an implementation (docs), but it shouldn't have an impact on performance. Do you have an example of a performance improvement?

@eggyal
Copy link
Contributor

eggyal commented Jun 10, 2022

Ah yes, I'd forgotten there was a blanket impl for references. But it's all inlined and optimised away (at least in release mode) so I'm not sure it's really a big problem? That said, I can't see any issue with making this change.

@Warrenren
Copy link
Contributor Author

There is such an implementation (docs), but it shouldn't have an impact on performance. Do you have an example of a performance improvement?

I have made a comment, I have some problem to describe the issue and the comment correct the description. If using "self", there will call fn which could be avoid.

@Noratrieb
Copy link
Member

In my opinion, this does not make the code clearer but only adds more noise. This should not have any perf impact, as the compiler usually optimizes these calls away. Godbolt confirms this: https://godbolt.org/z/5TazWeees

@Warrenren
Copy link
Contributor Author

Ah yes, I'd forgotten there was a blanket impl for references. But it's all inlined and optimised away (at least in release mode) so I'm not sure it's really a big problem? That said, I can't see any issue with making this change.

Actually, using "*self" will make it easy to understand. I read it for a lot time and at last debug to know what happened. Though there is not issue, I still suggest to change it to make it easy to understand

@Warrenren
Copy link
Contributor Author

For other fn, the "*self " is used, why this different

In my opinion, this does not make the code clearer but only adds more noise. This should not have any perf impact, as the compiler usually optimizes these calls away. Godbolt confirms this: https://godbolt.org/z/5TazWeees

For other fn, the "*self " is used, why this different.

@kadiwa4
Copy link
Contributor

kadiwa4 commented Jun 10, 2022

With -Copt-level=0, Godbolt registers an improvement. But I suggest removing the parantheses, because this also works:
*self <= *other

@Warrenren
Copy link
Contributor Author

With -Copt-level=0, Godbolt registers an improvement. But I suggest removing the parantheses, because this also works: *self <= *other

With -Copt-level=0, Godbolt registers an improvement. But I suggest removing the parantheses, because this also works: *self <= *other

I write it because other fn use parantheses, so make them same .

line 1352, delete parentheses for reviewers asking for it.
@Dylan-DPC Dylan-DPC changed the title line 1352, change self to (*self), other to (*other) line 1352, change self to *self, other to *other Jun 11, 2022
@Dylan-DPC
Copy link
Member

@bors r+ rollup

@bors
Copy link
Collaborator

bors commented Jun 11, 2022

📌 Commit 9e1e476 has been approved by Dylan-DPC

@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 Jun 11, 2022
@klensy
Copy link
Contributor

klensy commented Jun 11, 2022

Current PR title is actually bad :-(

@Dylan-DPC
Copy link
Member

suggestions are welcome :P

@Noratrieb
Copy link
Member

"Explicitly deref in float partial_eq"

@klensy
Copy link
Contributor

klensy commented Jun 11, 2022

suggestions are welcome :P

I'm not fully understand reasons for this PR from description and later discussion, maybe T-libs member will comment?

@Dylan-DPC Dylan-DPC changed the title line 1352, change self to *self, other to *other Explicitly deref in float partial_eq Jun 11, 2022
bors added a commit to rust-lang-ci/rust that referenced this pull request Jun 11, 2022
…askrgr

Rollup of 5 pull requests

Successful merges:

 - rust-lang#97904 (Small grammar fix in the compile_error documentation)
 - rust-lang#97943 (line 1352, change `self` to `*self`, other to `*other`)
 - rust-lang#97969 (Make -Cpasses= only apply to pre-link optimization)
 - rust-lang#97990 (Add more eslint checks)
 - rust-lang#97994 (feat(fix): update some links in `hir.rs`)

Failed merges:

r? `@ghost`
`@rustbot` modify labels: rollup
@bors bors merged commit 39de4e4 into rust-lang:master Jun 11, 2022
@rustbot rustbot added this to the 1.63.0 milestone Jun 11, 2022
@compiler-errors
Copy link
Member

@Dylan-DPC why did this get r+'ed? It's arguable if it's an improvement in readability, and nobody from @rust-lang/libs had the chance to review it. I know it was a small PR, but what's the rush to merge it?

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.