Improve `OrderedFloat` hash performance (#7512)
Co-authored-by: Lucas Meurer <hi@lucasmerlin.me>
This commit is contained in:
parent
9cc7f2ec16
commit
a8e36e3313
|
|
@ -124,13 +124,15 @@ mod private {
|
|||
|
||||
#[inline]
|
||||
fn hash<H: Hasher>(&self, state: &mut H) {
|
||||
if *self == 0.0 {
|
||||
state.write_u8(0);
|
||||
} else if self.is_nan() {
|
||||
state.write_u8(1);
|
||||
let bits = if self.is_nan() {
|
||||
// "Canonical" NaN.
|
||||
0x7fc00000
|
||||
} 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]
|
||||
fn hash<H: Hasher>(&self, state: &mut H) {
|
||||
if *self == 0.0 {
|
||||
state.write_u8(0);
|
||||
} else if self.is_nan() {
|
||||
state.write_u8(1);
|
||||
let bits = if self.is_nan() {
|
||||
// "Canonical" NaN.
|
||||
0x7ff8000000000000
|
||||
} else {
|
||||
self.to_bits().hash(state);
|
||||
}
|
||||
(self + 0.0).to_bits()
|
||||
};
|
||||
bits.hash(state);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue