Improve `OrderedFloat` hash performance (#7512)

Co-authored-by: Lucas Meurer <hi@lucasmerlin.me>
This commit is contained in:
valadaptive 2025-09-08 11:39:52 -04:00 committed by GitHub
parent 9cc7f2ec16
commit a8e36e3313
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 14 additions and 12 deletions

View File

@ -124,13 +124,15 @@ mod private {
#[inline] #[inline]
fn hash<H: Hasher>(&self, state: &mut H) { fn hash<H: Hasher>(&self, state: &mut H) {
if *self == 0.0 { let bits = if self.is_nan() {
state.write_u8(0); // "Canonical" NaN.
} else if self.is_nan() { 0x7fc00000
state.write_u8(1);
} else { } else {
self.to_bits().hash(state); // A trick taken from the `ordered-float` crate: -0.0 + 0.0 == +0.0.
} // https://github.com/reem/rust-ordered-float/blob/1841f0541ea0e56779cbac03de2705149e020675/src/lib.rs#L2178-L2181
(self + 0.0).to_bits()
};
bits.hash(state);
} }
} }
@ -142,13 +144,13 @@ mod private {
#[inline] #[inline]
fn hash<H: Hasher>(&self, state: &mut H) { fn hash<H: Hasher>(&self, state: &mut H) {
if *self == 0.0 { let bits = if self.is_nan() {
state.write_u8(0); // "Canonical" NaN.
} else if self.is_nan() { 0x7ff8000000000000
state.write_u8(1);
} else { } else {
self.to_bits().hash(state); (self + 0.0).to_bits()
} };
bits.hash(state);
} }
} }
} }