Skip to main content

proc_macro/bridge/
fxhash.rs

1//! This is a copy of the `rustc_hash` crate, adapted to work as a module.
2//!
3//! If in the future it becomes more reasonable to add dependencies to
4//! `proc_macro`, this module should be removed and replaced with a dependency
5//! on the `rustc_hash` crate.
6
7use std::collections::HashMap;
8use std::hash::{BuildHasherDefault, Hasher};
9use std::ops::BitXor;
10
11/// Type alias for a hashmap using the `fx` hash algorithm.
12pub(super) type FxHashMap<K, V> = HashMap<K, V, BuildHasherDefault<FxHasher>>;
13
14/// A speedy hash algorithm for use within rustc. The hashmap in alloc by
15/// default uses SipHash which isn't quite as speedy as we want. In the compiler
16/// we're not really worried about DOS attempts, so we use a fast
17/// non-cryptographic hash.
18///
19/// This is the same as the algorithm used by Firefox -- which is a homespun
20/// one not based on any widely-known algorithm -- though modified to produce
21/// 64-bit hash values instead of 32-bit hash values. It consistently
22/// out-performs an FNV-based hash within rustc itself -- the collision rate is
23/// similar or slightly worse than FNV, but the speed of the hash function
24/// itself is much higher because it works on up to 8 bytes at a time.
25#[derive(#[automatically_derived]
impl ::core::default::Default for FxHasher {
    #[inline]
    fn default() -> FxHasher {
        FxHasher { hash: ::core::default::Default::default() }
    }
}Default)]
26pub(super) struct FxHasher {
27    hash: usize,
28}
29
30impl FxHasher {
31    #[inline]
32    fn add_to_hash(&mut self, i: usize) {
33        const K: usize = cfg_select! {
34            target_pointer_width = "64" => 0x517cc1b727220a95,
35            target_pointer_width = "32" => 0x9e3779b9,
36            _ => 0, // just make it compile for -Zbuild-std
37        };
38        self.hash = self.hash.rotate_left(5).bitxor(i).wrapping_mul(K);
39    }
40}
41
42impl Hasher for FxHasher {
43    #[inline]
44    fn write(&mut self, mut bytes: &[u8]) {
45        let mut hash = FxHasher { hash: self.hash };
46        if !(size_of::<usize>() <= 8) {
    ::core::panicking::panic("assertion failed: size_of::<usize>() <= 8")
};assert!(size_of::<usize>() <= 8);
47        while bytes.len() >= size_of::<usize>() {
48            hash.add_to_hash(usize::from_ne_bytes(bytes[..size_of::<usize>()].try_into().unwrap()));
49            bytes = &bytes[size_of::<usize>()..];
50        }
51        if (size_of::<usize>() > 4) && (bytes.len() >= 4) {
52            hash.add_to_hash(u32::from_ne_bytes(bytes[..4].try_into().unwrap()) as usize);
53            bytes = &bytes[4..];
54        }
55        if (size_of::<usize>() > 2) && bytes.len() >= 2 {
56            hash.add_to_hash(u16::from_ne_bytes(bytes[..2].try_into().unwrap()) as usize);
57            bytes = &bytes[2..];
58        }
59        if (size_of::<usize>() > 1) && !bytes.is_empty() {
60            hash.add_to_hash(bytes[0] as usize);
61        }
62        self.hash = hash.hash;
63    }
64
65    #[inline]
66    fn write_u8(&mut self, i: u8) {
67        self.add_to_hash(i as usize);
68    }
69
70    #[inline]
71    fn write_u16(&mut self, i: u16) {
72        self.add_to_hash(i as usize);
73    }
74
75    #[inline]
76    fn write_u32(&mut self, i: u32) {
77        self.add_to_hash(i as usize);
78    }
79
80    #[cfg(target_pointer_width = "32")]
81    #[inline]
82    fn write_u64(&mut self, i: u64) {
83        self.add_to_hash(i as usize);
84        self.add_to_hash((i >> 32) as usize);
85    }
86
87    #[cfg(target_pointer_width = "64")]
88    #[inline]
89    fn write_u64(&mut self, i: u64) {
90        self.add_to_hash(i as usize);
91    }
92
93    #[inline]
94    fn write_usize(&mut self, i: usize) {
95        self.add_to_hash(i);
96    }
97
98    #[inline]
99    fn finish(&self) -> u64 {
100        self.hash as u64
101    }
102}