cranelift_codegen/
fx.rs

1// This file is taken from the Rust compiler: src/librustc_data_structures/fx.rs
2
3// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
4// file at the top-level directory of this distribution and at
5// http://rust-lang.org/COPYRIGHT.
6//
7// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
8// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
9// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
10// option. This file may not be copied, modified, or distributed
11// except according to those terms.
12
13use super::{HashMap, HashSet};
14use core::default::Default;
15use core::hash::{BuildHasherDefault, Hash, Hasher};
16use core::ops::BitXor;
17
18pub type FxHashMap<K, V> = HashMap<K, V, BuildHasherDefault<FxHasher>>;
19pub type FxHashSet<V> = HashSet<V, BuildHasherDefault<FxHasher>>;
20
21#[allow(non_snake_case)]
22pub fn FxHashMap<K: Hash + Eq, V>() -> FxHashMap<K, V> {
23    HashMap::default()
24}
25
26#[allow(non_snake_case)]
27pub fn FxHashSet<V: Hash + Eq>() -> FxHashSet<V> {
28    HashSet::default()
29}
30
31/// A speedy hash algorithm for use within rustc. The hashmap in liballoc
32/// by default uses SipHash which isn't quite as speedy as we want. In the
33/// compiler we're not really worried about DOS attempts, so we use a fast
34/// non-cryptographic hash.
35///
36/// This is the same as the algorithm used by Firefox -- which is a homespun
37/// one not based on any widely-known algorithm -- though modified to produce
38/// 64-bit hash values instead of 32-bit hash values. It consistently
39/// out-performs an FNV-based hash within rustc itself -- the collision rate is
40/// similar or slightly worse than FNV, but the speed of the hash function
41/// itself is much higher because it works on up to 8 bytes at a time.
42pub struct FxHasher {
43    hash: usize,
44}
45
46#[cfg(target_pointer_width = "32")]
47const K: usize = 0x9e3779b9;
48#[cfg(target_pointer_width = "64")]
49const K: usize = 0x517cc1b727220a95;
50
51impl Default for FxHasher {
52    #[inline]
53    fn default() -> Self {
54        Self { hash: 0 }
55    }
56}
57
58impl FxHasher {
59    #[inline]
60    fn add_to_hash(&mut self, i: usize) {
61        self.hash = self.hash.rotate_left(5).bitxor(i).wrapping_mul(K);
62    }
63}
64
65impl Hasher for FxHasher {
66    #[inline]
67    fn write(&mut self, bytes: &[u8]) {
68        for byte in bytes {
69            let i = *byte;
70            self.add_to_hash(i as usize);
71        }
72    }
73
74    #[inline]
75    fn write_u8(&mut self, i: u8) {
76        self.add_to_hash(i as usize);
77    }
78
79    #[inline]
80    fn write_u16(&mut self, i: u16) {
81        self.add_to_hash(i as usize);
82    }
83
84    #[inline]
85    fn write_u32(&mut self, i: u32) {
86        self.add_to_hash(i as usize);
87    }
88
89    #[cfg(target_pointer_width = "32")]
90    #[inline]
91    fn write_u64(&mut self, i: u64) {
92        self.add_to_hash(i as usize);
93        self.add_to_hash((i >> 32) as usize);
94    }
95
96    #[cfg(target_pointer_width = "64")]
97    #[inline]
98    fn write_u64(&mut self, i: u64) {
99        self.add_to_hash(i as usize);
100    }
101
102    #[inline]
103    fn write_usize(&mut self, i: usize) {
104        self.add_to_hash(i);
105    }
106
107    #[inline]
108    fn finish(&self) -> u64 {
109        self.hash as u64
110    }
111}