2019-02-08 16:36:22 +00:00
|
|
|
use crate::stable_hasher;
|
2021-03-11 21:16:15 +00:00
|
|
|
use rustc_serialize::{Decodable, Encodable};
|
2020-09-02 01:27:02 +00:00
|
|
|
use std::hash::{Hash, Hasher};
|
2020-12-17 03:46:19 +00:00
|
|
|
use std::mem::{self, MaybeUninit};
|
2016-10-07 13:13:11 +00:00
|
|
|
|
2020-09-02 01:27:02 +00:00
|
|
|
#[derive(Eq, PartialEq, Ord, PartialOrd, Debug, Clone, Copy)]
|
2021-01-27 13:28:07 +00:00
|
|
|
#[repr(C)]
|
2017-05-17 14:41:07 +00:00
|
|
|
pub struct Fingerprint(u64, u64);
|
2016-10-07 13:13:11 +00:00
|
|
|
|
|
|
|
impl Fingerprint {
|
2017-12-20 13:06:11 +00:00
|
|
|
pub const ZERO: Fingerprint = Fingerprint(0, 0);
|
2016-10-07 13:13:11 +00:00
|
|
|
|
2021-01-27 13:28:07 +00:00
|
|
|
#[inline]
|
|
|
|
pub fn new(_0: u64, _1: u64) -> Fingerprint {
|
|
|
|
Fingerprint(_0, _1)
|
|
|
|
}
|
|
|
|
|
2017-05-17 14:41:07 +00:00
|
|
|
#[inline]
|
2016-10-07 13:13:11 +00:00
|
|
|
pub fn from_smaller_hash(hash: u64) -> Fingerprint {
|
2017-05-17 14:41:07 +00:00
|
|
|
Fingerprint(hash, hash)
|
2016-10-07 13:13:11 +00:00
|
|
|
}
|
|
|
|
|
2017-05-17 14:41:07 +00:00
|
|
|
#[inline]
|
2016-10-07 13:13:11 +00:00
|
|
|
pub fn to_smaller_hash(&self) -> u64 {
|
2021-01-27 13:28:07 +00:00
|
|
|
// Even though both halves of the fingerprint are expected to be good
|
|
|
|
// quality hash values, let's still combine the two values because the
|
|
|
|
// Fingerprints in DefPathHash have the StableCrateId portion which is
|
|
|
|
// the same for all DefPathHashes from the same crate. Combining the
|
|
|
|
// two halfs makes sure we get a good quality hash in such cases too.
|
|
|
|
self.0.wrapping_mul(3).wrapping_add(self.1)
|
2016-10-07 13:13:11 +00:00
|
|
|
}
|
2016-12-13 23:45:03 +00:00
|
|
|
|
2017-10-27 21:53:57 +00:00
|
|
|
#[inline]
|
|
|
|
pub fn as_value(&self) -> (u64, u64) {
|
|
|
|
(self.0, self.1)
|
|
|
|
}
|
|
|
|
|
2017-06-12 15:00:55 +00:00
|
|
|
#[inline]
|
|
|
|
pub fn combine(self, other: Fingerprint) -> Fingerprint {
|
|
|
|
// See https://stackoverflow.com/a/27952689 on why this function is
|
|
|
|
// implemented this way.
|
|
|
|
Fingerprint(
|
|
|
|
self.0.wrapping_mul(3).wrapping_add(other.0),
|
2019-12-22 22:42:04 +00:00
|
|
|
self.1.wrapping_mul(3).wrapping_add(other.1),
|
2017-06-12 15:00:55 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2018-07-03 09:16:38 +00:00
|
|
|
// Combines two hashes in an order independent way. Make sure this is what
|
|
|
|
// you want.
|
|
|
|
#[inline]
|
|
|
|
pub fn combine_commutative(self, other: Fingerprint) -> Fingerprint {
|
2019-06-26 12:04:37 +00:00
|
|
|
let a = u128::from(self.1) << 64 | u128::from(self.0);
|
|
|
|
let b = u128::from(other.1) << 64 | u128::from(other.0);
|
2018-07-03 09:16:38 +00:00
|
|
|
|
|
|
|
let c = a.wrapping_add(b);
|
|
|
|
|
|
|
|
Fingerprint((c >> 64) as u64, c as u64)
|
|
|
|
}
|
|
|
|
|
2016-12-13 23:45:03 +00:00
|
|
|
pub fn to_hex(&self) -> String {
|
2017-05-17 14:41:07 +00:00
|
|
|
format!("{:x}{:x}", self.0, self.1)
|
2016-12-13 23:45:03 +00:00
|
|
|
}
|
2016-10-07 13:13:11 +00:00
|
|
|
}
|
|
|
|
|
2020-10-13 08:17:05 +00:00
|
|
|
impl std::fmt::Display for Fingerprint {
|
|
|
|
fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
2017-05-17 14:41:07 +00:00
|
|
|
write!(formatter, "{:x}-{:x}", self.0, self.1)
|
2016-10-07 13:13:11 +00:00
|
|
|
}
|
|
|
|
}
|
2016-12-13 23:45:03 +00:00
|
|
|
|
2020-09-02 01:27:02 +00:00
|
|
|
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) {
|
2021-02-04 09:37:11 +00:00
|
|
|
// Even though both halves of the fingerprint are expected to be good
|
|
|
|
// quality hash values, let's still combine the two values because the
|
|
|
|
// Fingerprints in DefPathHash have the StableCrateId portion which is
|
|
|
|
// the same for all DefPathHashes from the same crate. Combining the
|
|
|
|
// two halfs makes sure we get a good quality hash in such cases too.
|
|
|
|
//
|
|
|
|
// Since `Unhasher` is used only in the context of HashMaps, it is OK
|
|
|
|
// to combine the two components in an order-independent way (which is
|
|
|
|
// cheaper than the more robust Fingerprint::to_smaller_hash()). For
|
|
|
|
// HashMaps we don't really care if Fingerprint(x,y) and
|
|
|
|
// Fingerprint(y, x) result in the same hash value. Collision
|
|
|
|
// probability will still be much better than with FxHash.
|
2021-01-27 13:28:07 +00:00
|
|
|
self.write_u64(fingerprint.0.wrapping_add(fingerprint.1));
|
2020-09-02 01:27:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-13 23:45:03 +00:00
|
|
|
impl stable_hasher::StableHasherResult for Fingerprint {
|
2018-12-04 15:26:34 +00:00
|
|
|
#[inline]
|
2019-09-26 22:54:39 +00:00
|
|
|
fn finish(hasher: stable_hasher::StableHasher) -> Self {
|
2017-10-16 12:06:07 +00:00
|
|
|
let (_0, _1) = hasher.finalize();
|
|
|
|
Fingerprint(_0, _1)
|
2016-12-13 23:45:03 +00:00
|
|
|
}
|
|
|
|
}
|
2017-05-08 21:36:37 +00:00
|
|
|
|
2018-08-03 22:41:30 +00:00
|
|
|
impl_stable_hash_via_hash!(Fingerprint);
|
2017-12-23 03:41:09 +00:00
|
|
|
|
2020-06-11 14:49:57 +00:00
|
|
|
impl<E: rustc_serialize::Encoder> Encodable<E> for Fingerprint {
|
2021-03-11 21:16:15 +00:00
|
|
|
#[inline]
|
2020-06-11 14:49:57 +00:00
|
|
|
fn encode(&self, s: &mut E) -> Result<(), E::Error> {
|
2021-03-11 21:16:15 +00:00
|
|
|
let bytes: [u8; 16] = unsafe { mem::transmute([self.0.to_le(), self.1.to_le()]) };
|
|
|
|
s.emit_raw_bytes(&bytes)?;
|
|
|
|
Ok(())
|
2020-06-11 14:49:57 +00:00
|
|
|
}
|
|
|
|
}
|
2017-12-23 03:41:09 +00:00
|
|
|
|
2020-06-11 14:49:57 +00:00
|
|
|
impl<D: rustc_serialize::Decoder> Decodable<D> for Fingerprint {
|
2021-03-11 21:16:15 +00:00
|
|
|
#[inline]
|
2020-06-11 14:49:57 +00:00
|
|
|
fn decode(d: &mut D) -> Result<Self, D::Error> {
|
2021-03-11 21:16:15 +00:00
|
|
|
let mut bytes: [MaybeUninit<u8>; 16] = MaybeUninit::uninit_array();
|
|
|
|
d.read_raw_bytes(&mut bytes)?;
|
2020-11-04 06:23:08 +00:00
|
|
|
|
2021-03-11 21:16:15 +00:00
|
|
|
let [l, r]: [u64; 2] = unsafe { mem::transmute(bytes) };
|
|
|
|
Ok(Fingerprint(u64::from_le(l), u64::from_le(r)))
|
2017-12-23 03:41:09 +00:00
|
|
|
}
|
|
|
|
}
|
2020-11-04 06:23:08 +00:00
|
|
|
|
|
|
|
// `PackedFingerprint` wraps a `Fingerprint`. Its purpose is to, on certain
|
|
|
|
// architectures, behave like a `Fingerprint` without alignment requirements.
|
|
|
|
// This behavior is only enabled on x86 and x86_64, where the impact of
|
|
|
|
// unaligned accesses is tolerable in small doses.
|
|
|
|
//
|
|
|
|
// This may be preferable to use in large collections of structs containing
|
|
|
|
// fingerprints, as it can reduce memory consumption by preventing the padding
|
|
|
|
// that the more strictly-aligned `Fingerprint` can introduce. An application of
|
|
|
|
// this is in the query dependency graph, which contains a large collection of
|
|
|
|
// `DepNode`s. As of this writing, the size of a `DepNode` decreases by ~30%
|
|
|
|
// (from 24 bytes to 17) by using the packed representation here, which
|
|
|
|
// noticeably decreases total memory usage when compiling large crates.
|
2020-11-18 23:10:43 +00:00
|
|
|
//
|
|
|
|
// The wrapped `Fingerprint` is private to reduce the chance of a client
|
|
|
|
// invoking undefined behavior by taking a reference to the packed field.
|
2020-11-04 06:23:08 +00:00
|
|
|
#[cfg_attr(any(target_arch = "x86", target_arch = "x86_64"), repr(packed))]
|
|
|
|
#[derive(Eq, PartialEq, Ord, PartialOrd, Debug, Clone, Copy, Hash)]
|
2020-11-18 23:10:43 +00:00
|
|
|
pub struct PackedFingerprint(Fingerprint);
|
2020-11-04 06:23:08 +00:00
|
|
|
|
|
|
|
impl std::fmt::Display for PackedFingerprint {
|
|
|
|
#[inline]
|
|
|
|
fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
|
|
// Copy to avoid taking reference to packed field.
|
|
|
|
let copy = self.0;
|
|
|
|
copy.fmt(formatter)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<E: rustc_serialize::Encoder> Encodable<E> for PackedFingerprint {
|
|
|
|
#[inline]
|
|
|
|
fn encode(&self, s: &mut E) -> Result<(), E::Error> {
|
|
|
|
// Copy to avoid taking reference to packed field.
|
|
|
|
let copy = self.0;
|
|
|
|
copy.encode(s)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<D: rustc_serialize::Decoder> Decodable<D> for PackedFingerprint {
|
|
|
|
#[inline]
|
|
|
|
fn decode(d: &mut D) -> Result<Self, D::Error> {
|
2020-12-07 01:30:55 +00:00
|
|
|
Fingerprint::decode(d).map(PackedFingerprint)
|
2020-11-04 06:23:08 +00:00
|
|
|
}
|
|
|
|
}
|
2020-11-18 23:10:43 +00:00
|
|
|
|
|
|
|
impl From<Fingerprint> for PackedFingerprint {
|
|
|
|
#[inline]
|
|
|
|
fn from(f: Fingerprint) -> PackedFingerprint {
|
|
|
|
PackedFingerprint(f)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<PackedFingerprint> for Fingerprint {
|
|
|
|
#[inline]
|
|
|
|
fn from(f: PackedFingerprint) -> Fingerprint {
|
|
|
|
f.0
|
|
|
|
}
|
|
|
|
}
|