Skip to content

Commit

Permalink
Do not use unnecessary endian conversion.
Browse files Browse the repository at this point in the history
  • Loading branch information
orlp committed Aug 11, 2024
1 parent a04a1e4 commit fce1dec
Showing 1 changed file with 4 additions and 6 deletions.
10 changes: 4 additions & 6 deletions library/core/src/net/ip_addr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,8 @@ impl Hash for Ipv4Addr {
fn hash<H: Hasher>(&self, state: &mut H) {
// Hashers are often more efficient at hashing a fixed-width integer
// 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
// more common than big-endian machines.
u32::from_le_bytes(self.octets).hash(state);
// here as that may involve a byteswap which is unnecessary.
u32::from_ne_bytes(self.octets).hash(state);
}
}

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

Expand Down

0 comments on commit fce1dec

Please sign in to comment.