Stable hashing: add comments and tests concerning platform-independence
SipHasher128 implements short_write in an endian-independent way, yet its write_xxx Hasher trait methods undo this endian-independence by byte swapping the integer inputs on big-endian hardware. StableHasher then adds endian-independence back by also byte-swapping on big-endian hardware prior to invoking SipHasher128. This double swap may have the appearance of being a no-op, but is in fact by design. In particular, we really do want SipHasher128 to be platform-dependent, in order to be consistent with the libstd SipHasher. Try to clarify this intent. Also, add and update a couple of unit tests.
This commit is contained in:
@@ -5,6 +5,9 @@ use smallvec::SmallVec;
|
||||
use std::hash::{BuildHasher, Hash, Hasher};
|
||||
use std::mem;
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests;
|
||||
|
||||
/// When hashing something that ends up affecting properties like symbol names,
|
||||
/// we want these symbol names to be calculated independently of other factors
|
||||
/// like what architecture you're compiling *from*.
|
||||
@@ -129,7 +132,8 @@ impl Hasher for StableHasher {
|
||||
fn write_isize(&mut self, i: isize) {
|
||||
// Always treat isize as i64 so we get the same results on 32 and 64 bit
|
||||
// platforms. This is important for symbol hashes when cross compiling,
|
||||
// for example.
|
||||
// for example. Sign extending here is preferable as it means that the
|
||||
// same negative number hashes the same on both 32 and 64 bit platforms.
|
||||
self.state.write_i64((i as i64).to_le());
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user