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

Migrated to f32::total_cmp() function in FloatOrd::cmp() #5208

Closed
wants to merge 3 commits into from

Conversation

nhunds
Copy link

@nhunds nhunds commented Jul 5, 2022

Objective

Fixes #5152

Solution

Use total_cmp() function from f32 in cmp() function on FloatOrd

@nhunds nhunds changed the title Migrated to f32::total_cmp() function. Migrated to f32::total_cmp() function in FloratOrd::cmp() Jul 5, 2022
@nhunds nhunds changed the title Migrated to f32::total_cmp() function in FloratOrd::cmp() Migrated to f32::total_cmp() function in FloatOrd::cmp() Jul 5, 2022
@alice-i-cecile
Copy link
Member

Code change looks good to me. @DJMcNab, advice on the hash concern you raised?

@mockersf
Copy link
Member

mockersf commented Jul 5, 2022

This should be benchmarked too 👍

@DJMcNab
Copy link
Member

DJMcNab commented Jul 5, 2022

I don't think the hash concern is relevant here, since the only thing that matters is hash in relation to PartialEq

However, I think the clippy lint we ignore is making a good point - we should also implement PartialOrd in terms of this Ord impl.

@DJMcNab
Copy link
Member

DJMcNab commented Jul 5, 2022

Saying that, I'm not convinced PartialEq doesn't have requirements related to the PartialOrd implementation

Basically, we need to check the documentation for all four traits, which I can't right at this moment.

@alice-i-cecile alice-i-cecile added A-Rendering Drawing game state to the screen C-Code-Quality A section of code that is hard to understand or change A-Math Fundamental domain-agnostic mathematical operations S-Needs-Benchmarking This set of changes needs performance benchmarking to double-check that they help labels Jul 5, 2022
@nhunds
Copy link
Author

nhunds commented Jul 5, 2022

I think we don't need the PartialEq implementation, but in my opinion it seems like f32 doesn't have a implementation of the Hash trait.

Copy link
Member

@james7132 james7132 left a comment

Choose a reason for hiding this comment

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

Going to second the PartialOrd implementation. The Ord documentation explicitly states the two must be in sync.

Copy link
Member

@DJMcNab DJMcNab left a comment

Choose a reason for hiding this comment

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

We still need to think about the Hash implementation

At the moment, we either:

  • need a test which loops through all f32 values, and checks that the implementations agree. I expect that they won't, unfortunately.
  • convert the only usage of this impl (in FontAtlasSet) to use BTreeMap. I suspect for this workload, that would even be faster. This is the path I'd recommend taking

We also need to use a manual PartialOrd impl.

crates/bevy_utils/src/float_ord.rs Outdated Show resolved Hide resolved
Co-authored-by: Daniel McNab <36049421+DJMcNab@users.noreply.github.com>
@superdump
Copy link
Contributor

* convert the only usage of this impl (in `FontAtlasSet`) to use `BTreeMap`. I suspect for this workload, that would even be faster. This is the path I'd recommend taking

Why would BTreeMap be faster? It would still be a binary search versus a hash and direct lookup, right?

@DJMcNab
Copy link
Member

DJMcNab commented Jul 13, 2022

Based on the using with_capacity(1), I assume that the majority of the cases have very few items.

Additionally, the keys are small, so a binary search should be very fast.

@atlv24
Copy link
Contributor

atlv24 commented Jan 23, 2024

The same approach taken in #10558 should be used here

@NthTensor
Copy link
Contributor

I'm going to close this out. Looks like this is wanted, but work has stalled out and the approach needs to be changed. Feel free to re-open.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-Math Fundamental domain-agnostic mathematical operations A-Rendering Drawing game state to the screen C-Code-Quality A section of code that is hard to understand or change S-Needs-Benchmarking This set of changes needs performance benchmarking to double-check that they help
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

Use the freshly stabilized f32::total_cmp methods
8 participants