forked from rust-lang/rust
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Auto merge of rust-lang#76233 - cuviper:unhasher, r=Mark-Simulacrum
Avoid rehashing Fingerprint as a map key This introduces a no-op `Unhasher` for map keys that are already hash- like, for example `Fingerprint` and its wrapper `DefPathHash`. For these we can directly produce the `u64` hash for maps. The first use of this is `def_path_hash_to_def_id: Option<UnhashMap<DefPathHash, DefId>>`. cc rust-lang#56308 r? @eddyb
- Loading branch information
Showing
4 changed files
with
62 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
use std::collections::{HashMap, HashSet}; | ||
use std::hash::{BuildHasherDefault, Hasher}; | ||
|
||
pub type UnhashMap<K, V> = HashMap<K, V, BuildHasherDefault<Unhasher>>; | ||
pub type UnhashSet<V> = HashSet<V, BuildHasherDefault<Unhasher>>; | ||
|
||
/// This no-op hasher expects only a single `write_u64` call. It's intended for | ||
/// map keys that already have hash-like quality, like `Fingerprint`. | ||
#[derive(Default)] | ||
pub struct Unhasher { | ||
value: u64, | ||
} | ||
|
||
impl Hasher for Unhasher { | ||
#[inline] | ||
fn finish(&self) -> u64 { | ||
self.value | ||
} | ||
|
||
fn write(&mut self, _bytes: &[u8]) { | ||
unimplemented!("use write_u64"); | ||
} | ||
|
||
#[inline] | ||
fn write_u64(&mut self, value: u64) { | ||
debug_assert_eq!(0, self.value, "Unhasher doesn't mix values!"); | ||
self.value = value; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters