@@ -133,18 +133,18 @@ impl Hasher for StableHasher {
133
133
134
134
#[ inline]
135
135
fn write_isize ( & mut self , i : isize ) {
136
- // Always treat isize as i64 so we get the same results on 32 and 64 bit
136
+ // Always treat isize as a 64-bit number so we get the same results on 32 and 64 bit
137
137
// platforms. This is important for symbol hashes when cross compiling,
138
138
// for example. Sign extending here is preferable as it means that the
139
139
// same negative number hashes the same on both 32 and 64 bit platforms.
140
- let value = ( i as i64 ) . to_le ( ) as u64 ;
140
+ let value = i as u64 ;
141
141
142
142
// Cold path
143
143
#[ cold]
144
144
#[ inline( never) ]
145
145
fn hash_value ( state : & mut SipHasher128 , value : u64 ) {
146
146
state. write_u8 ( 0xFF ) ;
147
- state. write_u64 ( value) ;
147
+ state. write_u64 ( value. to_le ( ) ) ;
148
148
}
149
149
150
150
// `isize` values often seem to have a small (positive) numeric value in practice.
@@ -161,6 +161,10 @@ impl Hasher for StableHasher {
161
161
// 8 bytes. Since this prefix cannot occur when we hash a single byte, when we hash two
162
162
// `isize`s that fit within a different amount of bytes, they should always produce a different
163
163
// byte stream for the hasher.
164
+ //
165
+ // To ensure that this optimization hashes the exact same bytes on both little-endian and
166
+ // big-endian architectures, we compare the value with 0xFF before we convert the number
167
+ // into a unified representation (little-endian).
164
168
if value < 0xFF {
165
169
self . state . write_u8 ( value as u8 ) ;
166
170
} else {
0 commit comments