diff options
| author | Scott McMurray <scottmcm@users.noreply.github.com> | 2022-03-04 00:17:26 -0800 |
|---|---|---|
| committer | Scott McMurray <scottmcm@users.noreply.github.com> | 2022-05-06 00:03:38 -0700 |
| commit | 98054377ee13956e6134b13aa31be01d9cd5a6af (patch) | |
| tree | c08f6fb1a70c834ba46216f550f0ebb69a57b46b /compiler/rustc_data_structures/src | |
| parent | 086bf7a8ff3550ff1f4c8e78d8b3a7804fdbbb36 (diff) | |
| download | rust-98054377ee13956e6134b13aa31be01d9cd5a6af.tar.gz rust-98054377ee13956e6134b13aa31be01d9cd5a6af.zip | |
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.
Diffstat (limited to 'compiler/rustc_data_structures/src')
| -rw-r--r-- | compiler/rustc_data_structures/src/lib.rs | 1 | ||||
| -rw-r--r-- | compiler/rustc_data_structures/src/sip128.rs | 8 | ||||
| -rw-r--r-- | compiler/rustc_data_structures/src/stable_hasher.rs | 11 |
3 files changed, 20 insertions, 0 deletions
diff --git a/compiler/rustc_data_structures/src/lib.rs b/compiler/rustc_data_structures/src/lib.rs index 30a3ddc6003..2cedee3b771 100644 --- a/compiler/rustc_data_structures/src/lib.rs +++ b/compiler/rustc_data_structures/src/lib.rs @@ -18,6 +18,7 @@ #![feature(generators)] #![feature(let_else)] #![feature(hash_raw_entry)] +#![feature(hasher_prefixfree_extras)] #![feature(maybe_uninit_uninit_array)] #![feature(min_specialization)] #![feature(never_type)] diff --git a/compiler/rustc_data_structures/src/sip128.rs b/compiler/rustc_data_structures/src/sip128.rs index 6e5c0617bf3..abd25f46ad5 100644 --- a/compiler/rustc_data_structures/src/sip128.rs +++ b/compiler/rustc_data_structures/src/sip128.rs @@ -462,6 +462,14 @@ impl Hasher for SipHasher128 { self.slice_write(msg); } + #[inline] + fn write_str(&mut self, s: &str) { + // This hasher works byte-wise, and `0xFF` cannot show up in a `str`, + // so just hashing the one extra byte is enough to be prefix-free. + self.write(s.as_bytes()); + self.write_u8(0xFF); + } + fn finish(&self) -> u64 { panic!("SipHasher128 cannot provide valid 64 bit hashes") } diff --git a/compiler/rustc_data_structures/src/stable_hasher.rs b/compiler/rustc_data_structures/src/stable_hasher.rs index 25353290fd5..c8bb4fc5e6a 100644 --- a/compiler/rustc_data_structures/src/stable_hasher.rs +++ b/compiler/rustc_data_structures/src/stable_hasher.rs @@ -74,6 +74,17 @@ impl Hasher for StableHasher { } #[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); } |
