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.
67use std::collections::HashMap;
8use std::hash::{BuildHasherDefault, Hasher};
9use std::ops::BitXor;
1011/// Type alias for a hashmap using the `fx` hash algorithm.
12pub(super) type FxHashMap<K, V> = HashMap<K, V, BuildHasherDefault<FxHasher>>;
1314/// 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}
2930impl FxHasher {
31#[inline]
32fn add_to_hash(&mut self, i: usize) {
33const 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};
38self.hash = self.hash.rotate_left(5).bitxor(i).wrapping_mul(K);
39 }
40}
4142impl Hasherfor FxHasher {
43#[inline]
44fn write(&mut self, mut bytes: &[u8]) {
45let mut hash = FxHasher { hash: self.hash };
46if !(size_of::<usize>() <= 8) {
::core::panicking::panic("assertion failed: size_of::<usize>() <= 8")
};assert!(size_of::<usize>() <= 8);
47while 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 }
51if (size_of::<usize>() > 4) && (bytes.len() >= 4) {
52hash.add_to_hash(u32::from_ne_bytes(bytes[..4].try_into().unwrap()) as usize);
53bytes = &bytes[4..];
54 }
55if (size_of::<usize>() > 2) && bytes.len() >= 2 {
56hash.add_to_hash(u16::from_ne_bytes(bytes[..2].try_into().unwrap()) as usize);
57bytes = &bytes[2..];
58 }
59if (size_of::<usize>() > 1) && !bytes.is_empty() {
60hash.add_to_hash(bytes[0] as usize);
61 }
62self.hash = hash.hash;
63 }
6465#[inline]
66fn write_u8(&mut self, i: u8) {
67self.add_to_hash(ias usize);
68 }
6970#[inline]
71fn write_u16(&mut self, i: u16) {
72self.add_to_hash(ias usize);
73 }
7475#[inline]
76fn write_u32(&mut self, i: u32) {
77self.add_to_hash(ias usize);
78 }
7980#[cfg(target_pointer_width = "32")]
81 #[inline]
82fn write_u64(&mut self, i: u64) {
83self.add_to_hash(i as usize);
84self.add_to_hash((i >> 32) as usize);
85 }
8687#[cfg(target_pointer_width = "64")]
88 #[inline]
89fn write_u64(&mut self, i: u64) {
90self.add_to_hash(ias usize);
91 }
9293#[inline]
94fn write_usize(&mut self, i: usize) {
95self.add_to_hash(i);
96 }
9798#[inline]
99fn finish(&self) -> u64 {
100self.hash as u64101 }
102}