Do not use unnecessary endian conversion.

This commit is contained in:
Orson Peters 2024-08-11 14:55:29 +02:00
parent a04a1e464f
commit fce1decc3c

View File

@ -79,9 +79,8 @@ impl Hash for Ipv4Addr {
fn hash<H: Hasher>(&self, state: &mut H) { fn hash<H: Hasher>(&self, state: &mut H) {
// Hashers are often more efficient at hashing a fixed-width integer // Hashers are often more efficient at hashing a fixed-width integer
// than a bytestring, so convert before hashing. We don't use to_bits() // than a bytestring, so convert before hashing. We don't use to_bits()
// here as that involves a byteswap on little-endian machines, which are // here as that may involve a byteswap which is unnecessary.
// more common than big-endian machines. u32::from_ne_bytes(self.octets).hash(state);
u32::from_le_bytes(self.octets).hash(state);
} }
} }
@ -172,9 +171,8 @@ impl Hash for Ipv6Addr {
fn hash<H: Hasher>(&self, state: &mut H) { fn hash<H: Hasher>(&self, state: &mut H) {
// Hashers are often more efficient at hashing a fixed-width integer // Hashers are often more efficient at hashing a fixed-width integer
// than a bytestring, so convert before hashing. We don't use to_bits() // than a bytestring, so convert before hashing. We don't use to_bits()
// here as that involves byteswaps on little-endian machines, which are // here as that may involve unnecessary byteswaps.
// more common than big-endian machines. u128::from_ne_bytes(self.octets).hash(state);
u128::from_le_bytes(self.octets).hash(state);
} }
} }