rustc_data_structures/
fingerprint.rs

1use std::hash::{Hash, Hasher};
2
3use rustc_hashes::Hash64;
4use rustc_serialize::{Decodable, Decoder, Encodable, Encoder};
5
6use crate::stable_hasher::{FromStableHash, StableHasherHash, impl_stable_traits_for_trivial_type};
7
8#[cfg(test)]
9mod tests;
10
11#[derive(Eq, PartialEq, Ord, PartialOrd, Debug, Clone, Copy)]
12#[repr(C)]
13pub struct Fingerprint(u64, u64);
14
15pub trait FingerprintComponent {
16    fn as_u64(&self) -> u64;
17}
18
19impl FingerprintComponent for Hash64 {
20    #[inline]
21    fn as_u64(&self) -> u64 {
22        Hash64::as_u64(*self)
23    }
24}
25
26impl FingerprintComponent for u64 {
27    #[inline]
28    fn as_u64(&self) -> u64 {
29        *self
30    }
31}
32
33impl Fingerprint {
34    pub const ZERO: Fingerprint = Fingerprint(0, 0);
35
36    #[inline]
37    pub fn new<A, B>(_0: A, _1: B) -> Fingerprint
38    where
39        A: FingerprintComponent,
40        B: FingerprintComponent,
41    {
42        Fingerprint(_0.as_u64(), _1.as_u64())
43    }
44
45    #[inline]
46    pub fn to_smaller_hash(&self) -> Hash64 {
47        // Even though both halves of the fingerprint are expected to be good
48        // quality hash values, let's still combine the two values because the
49        // Fingerprints in DefPathHash have the StableCrateId portion which is
50        // the same for all DefPathHashes from the same crate. Combining the
51        // two halves makes sure we get a good quality hash in such cases too.
52        Hash64::new(self.0.wrapping_mul(3).wrapping_add(self.1))
53    }
54
55    #[inline]
56    pub fn split(&self) -> (Hash64, Hash64) {
57        (Hash64::new(self.0), Hash64::new(self.1))
58    }
59
60    #[inline]
61    pub fn combine(self, other: Fingerprint) -> Fingerprint {
62        // See https://stackoverflow.com/a/27952689 on why this function is
63        // implemented this way.
64        Fingerprint(
65            self.0.wrapping_mul(3).wrapping_add(other.0),
66            self.1.wrapping_mul(3).wrapping_add(other.1),
67        )
68    }
69
70    #[inline]
71    pub(crate) fn as_u128(self) -> u128 {
72        u128::from(self.1) << 64 | u128::from(self.0)
73    }
74
75    // Combines two hashes in an order independent way. Make sure this is what
76    // you want.
77    #[inline]
78    pub fn combine_commutative(self, other: Fingerprint) -> Fingerprint {
79        let a = u128::from(self.1) << 64 | u128::from(self.0);
80        let b = u128::from(other.1) << 64 | u128::from(other.0);
81
82        let c = a.wrapping_add(b);
83
84        Fingerprint(c as u64, (c >> 64) as u64)
85    }
86
87    pub fn to_hex(&self) -> String {
88        format!("{:x}{:x}", self.0, self.1)
89    }
90
91    #[inline]
92    pub fn to_le_bytes(&self) -> [u8; 16] {
93        // This seems to optimize to the same machine code as
94        // `unsafe { mem::transmute(*k) }`. Well done, LLVM! :)
95        let mut result = [0u8; 16];
96
97        let first_half: &mut [u8; 8] = (&mut result[0..8]).try_into().unwrap();
98        *first_half = self.0.to_le_bytes();
99
100        let second_half: &mut [u8; 8] = (&mut result[8..16]).try_into().unwrap();
101        *second_half = self.1.to_le_bytes();
102
103        result
104    }
105
106    #[inline]
107    pub fn from_le_bytes(bytes: [u8; 16]) -> Fingerprint {
108        Fingerprint(
109            u64::from_le_bytes(bytes[0..8].try_into().unwrap()),
110            u64::from_le_bytes(bytes[8..16].try_into().unwrap()),
111        )
112    }
113}
114
115impl std::fmt::Display for Fingerprint {
116    fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
117        write!(formatter, "{:x}-{:x}", self.0, self.1)
118    }
119}
120
121impl Hash for Fingerprint {
122    #[inline]
123    fn hash<H: Hasher>(&self, state: &mut H) {
124        state.write_fingerprint(self);
125    }
126}
127
128trait FingerprintHasher {
129    fn write_fingerprint(&mut self, fingerprint: &Fingerprint);
130}
131
132impl<H: Hasher> FingerprintHasher for H {
133    #[inline]
134    default fn write_fingerprint(&mut self, fingerprint: &Fingerprint) {
135        self.write_u64(fingerprint.0);
136        self.write_u64(fingerprint.1);
137    }
138}
139
140impl FingerprintHasher for crate::unhash::Unhasher {
141    #[inline]
142    fn write_fingerprint(&mut self, fingerprint: &Fingerprint) {
143        // Even though both halves of the fingerprint are expected to be good
144        // quality hash values, let's still combine the two values because the
145        // Fingerprints in DefPathHash have the StableCrateId portion which is
146        // the same for all DefPathHashes from the same crate. Combining the
147        // two halves makes sure we get a good quality hash in such cases too.
148        //
149        // Since `Unhasher` is used only in the context of HashMaps, it is OK
150        // to combine the two components in an order-independent way (which is
151        // cheaper than the more robust Fingerprint::to_smaller_hash()). For
152        // HashMaps we don't really care if Fingerprint(x,y) and
153        // Fingerprint(y, x) result in the same hash value. Collision
154        // probability will still be much better than with FxHash.
155        self.write_u64(fingerprint.0.wrapping_add(fingerprint.1));
156    }
157}
158
159impl FromStableHash for Fingerprint {
160    type Hash = StableHasherHash;
161
162    #[inline]
163    fn from(StableHasherHash([_0, _1]): Self::Hash) -> Self {
164        Fingerprint(_0, _1)
165    }
166}
167
168impl_stable_traits_for_trivial_type!(Fingerprint);
169
170impl<E: Encoder> Encodable<E> for Fingerprint {
171    #[inline]
172    fn encode(&self, s: &mut E) {
173        s.emit_raw_bytes(&self.to_le_bytes());
174    }
175}
176
177impl<D: Decoder> Decodable<D> for Fingerprint {
178    #[inline]
179    fn decode(d: &mut D) -> Self {
180        Fingerprint::from_le_bytes(d.read_raw_bytes(16).try_into().unwrap())
181    }
182}
183
184// `PackedFingerprint` wraps a `Fingerprint`. Its purpose is to, on certain
185// architectures, behave like a `Fingerprint` without alignment requirements.
186// This behavior is only enabled on x86 and x86_64, where the impact of
187// unaligned accesses is tolerable in small doses.
188//
189// This may be preferable to use in large collections of structs containing
190// fingerprints, as it can reduce memory consumption by preventing the padding
191// that the more strictly-aligned `Fingerprint` can introduce. An application of
192// this is in the query dependency graph, which contains a large collection of
193// `DepNode`s. As of this writing, the size of a `DepNode` decreases by ~30%
194// (from 24 bytes to 17) by using the packed representation here, which
195// noticeably decreases total memory usage when compiling large crates.
196//
197// The wrapped `Fingerprint` is private to reduce the chance of a client
198// invoking undefined behavior by taking a reference to the packed field.
199#[cfg_attr(any(target_arch = "x86", target_arch = "x86_64"), repr(packed))]
200#[derive(Eq, PartialEq, Ord, PartialOrd, Debug, Clone, Copy, Hash)]
201pub struct PackedFingerprint(Fingerprint);
202
203impl std::fmt::Display for PackedFingerprint {
204    #[inline]
205    fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
206        // Copy to avoid taking reference to packed field.
207        let copy = self.0;
208        copy.fmt(formatter)
209    }
210}
211
212impl<E: Encoder> Encodable<E> for PackedFingerprint {
213    #[inline]
214    fn encode(&self, s: &mut E) {
215        // Copy to avoid taking reference to packed field.
216        let copy = self.0;
217        copy.encode(s);
218    }
219}
220
221impl<D: Decoder> Decodable<D> for PackedFingerprint {
222    #[inline]
223    fn decode(d: &mut D) -> Self {
224        Self(Fingerprint::decode(d))
225    }
226}
227
228impl From<Fingerprint> for PackedFingerprint {
229    #[inline]
230    fn from(f: Fingerprint) -> PackedFingerprint {
231        PackedFingerprint(f)
232    }
233}
234
235impl From<PackedFingerprint> for Fingerprint {
236    #[inline]
237    fn from(f: PackedFingerprint) -> Fingerprint {
238        f.0
239    }
240}