1
Fork 0

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>>`.
This commit is contained in:
Josh Stone 2020-09-01 18:27:02 -07:00
parent 130359cb05
commit 469ca379d6
4 changed files with 62 additions and 3 deletions

View file

@ -3,9 +3,10 @@ use rustc_serialize::{
opaque::{self, EncodeResult},
Decodable, Encodable,
};
use std::hash::{Hash, Hasher};
use std::mem;
#[derive(Eq, PartialEq, Ord, PartialOrd, Hash, Debug, Clone, Copy)]
#[derive(Eq, PartialEq, Ord, PartialOrd, Debug, Clone, Copy)]
pub struct Fingerprint(u64, u64);
impl Fingerprint {
@ -76,6 +77,33 @@ impl ::std::fmt::Display for Fingerprint {
}
}
impl Hash for Fingerprint {
#[inline]
fn hash<H: Hasher>(&self, state: &mut H) {
state.write_fingerprint(self);
}
}
trait FingerprintHasher {
fn write_fingerprint(&mut self, fingerprint: &Fingerprint);
}
impl<H: Hasher> FingerprintHasher for H {
#[inline]
default fn write_fingerprint(&mut self, fingerprint: &Fingerprint) {
self.write_u64(fingerprint.0);
self.write_u64(fingerprint.1);
}
}
impl FingerprintHasher for crate::unhash::Unhasher {
#[inline]
fn write_fingerprint(&mut self, fingerprint: &Fingerprint) {
// `Unhasher` only wants a single `u64`
self.write_u64(fingerprint.0);
}
}
impl stable_hasher::StableHasherResult for Fingerprint {
#[inline]
fn finish(hasher: stable_hasher::StableHasher) -> Self {