1
Fork 0

Add a dedicated length-prefixing method to Hasher

This accomplishes two main goals:
- Make it clear who is responsible for prefix-freedom, including how they should do it
- Make it feasible for a `Hasher` that *doesn't* care about Hash-DoS resistance to get better performance by not hashing lengths

This does not change rustc-hash, since that's in an external crate, but that could potentially use it in future.
This commit is contained in:
Scott McMurray 2022-03-04 00:17:26 -08:00
parent 086bf7a8ff
commit 98054377ee
14 changed files with 166 additions and 6 deletions

View file

@ -73,6 +73,17 @@ impl Hasher for StableHasher {
self.state.write(bytes);
}
#[inline]
fn write_str(&mut self, s: &str) {
self.state.write_str(s);
}
#[inline]
fn write_length_prefix(&mut self, len: usize) {
// Our impl for `usize` will extend it if needed.
self.write_usize(len);
}
#[inline]
fn write_u8(&mut self, i: u8) {
self.state.write_u8(i);