get rustc_hash from external crate
This commit is contained in:
parent
6f425a9201
commit
9c548bf26d
4 changed files with 34 additions and 85 deletions
|
|
@ -17,6 +17,7 @@ cfg-if = "0.1.2"
|
|||
stable_deref_trait = "1.0.0"
|
||||
parking_lot_core = "0.2.8"
|
||||
rustc-rayon = "0.1.0"
|
||||
rustc-hash = "1.0.0"
|
||||
|
||||
[dependencies.parking_lot]
|
||||
version = "0.5"
|
||||
|
|
|
|||
|
|
@ -10,11 +10,11 @@
|
|||
|
||||
use std::collections::{HashMap, HashSet};
|
||||
use std::default::Default;
|
||||
use std::hash::{Hasher, Hash, BuildHasherDefault};
|
||||
use std::ops::BitXor;
|
||||
use std::hash::Hash;
|
||||
|
||||
pub type FxHashMap<K, V> = HashMap<K, V, BuildHasherDefault<FxHasher>>;
|
||||
pub type FxHashSet<V> = HashSet<V, BuildHasherDefault<FxHasher>>;
|
||||
pub use rustc_hash::FxHashMap;
|
||||
pub use rustc_hash::FxHashSet;
|
||||
pub use rustc_hash::FxHasher;
|
||||
|
||||
#[allow(non_snake_case)]
|
||||
pub fn FxHashMap<K: Hash + Eq, V>() -> FxHashMap<K, V> {
|
||||
|
|
@ -26,84 +26,3 @@ pub fn FxHashSet<V: Hash + Eq>() -> FxHashSet<V> {
|
|||
HashSet::default()
|
||||
}
|
||||
|
||||
/// A speedy hash algorithm for use within rustc. The hashmap in liballoc
|
||||
/// by default uses SipHash which isn't quite as speedy as we want. In the
|
||||
/// compiler we're not really worried about DOS attempts, so we use a fast
|
||||
/// non-cryptographic hash.
|
||||
///
|
||||
/// This is the same as the algorithm used by Firefox -- which is a homespun
|
||||
/// one not based on any widely-known algorithm -- though modified to produce
|
||||
/// 64-bit hash values instead of 32-bit hash values. It consistently
|
||||
/// out-performs an FNV-based hash within rustc itself -- the collision rate is
|
||||
/// similar or slightly worse than FNV, but the speed of the hash function
|
||||
/// itself is much higher because it works on up to 8 bytes at a time.
|
||||
pub struct FxHasher {
|
||||
hash: usize
|
||||
}
|
||||
|
||||
#[cfg(target_pointer_width = "32")]
|
||||
const K: usize = 0x9e3779b9;
|
||||
#[cfg(target_pointer_width = "64")]
|
||||
const K: usize = 0x517cc1b727220a95;
|
||||
|
||||
impl Default for FxHasher {
|
||||
#[inline]
|
||||
fn default() -> FxHasher {
|
||||
FxHasher { hash: 0 }
|
||||
}
|
||||
}
|
||||
|
||||
impl FxHasher {
|
||||
#[inline]
|
||||
fn add_to_hash(&mut self, i: usize) {
|
||||
self.hash = self.hash.rotate_left(5).bitxor(i).wrapping_mul(K);
|
||||
}
|
||||
}
|
||||
|
||||
impl Hasher for FxHasher {
|
||||
#[inline]
|
||||
fn write(&mut self, bytes: &[u8]) {
|
||||
for byte in bytes {
|
||||
let i = *byte;
|
||||
self.add_to_hash(i as usize);
|
||||
}
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn write_u8(&mut self, i: u8) {
|
||||
self.add_to_hash(i as usize);
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn write_u16(&mut self, i: u16) {
|
||||
self.add_to_hash(i as usize);
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn write_u32(&mut self, i: u32) {
|
||||
self.add_to_hash(i as usize);
|
||||
}
|
||||
|
||||
#[cfg(target_pointer_width = "32")]
|
||||
#[inline]
|
||||
fn write_u64(&mut self, i: u64) {
|
||||
self.add_to_hash(i as usize);
|
||||
self.add_to_hash((i >> 32) as usize);
|
||||
}
|
||||
|
||||
#[cfg(target_pointer_width = "64")]
|
||||
#[inline]
|
||||
fn write_u64(&mut self, i: u64) {
|
||||
self.add_to_hash(i as usize);
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn write_usize(&mut self, i: usize) {
|
||||
self.add_to_hash(i);
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn finish(&self) -> u64 {
|
||||
self.hash as u64
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -44,6 +44,7 @@ extern crate parking_lot;
|
|||
extern crate cfg_if;
|
||||
extern crate stable_deref_trait;
|
||||
extern crate rustc_rayon as rayon;
|
||||
extern crate rustc_hash;
|
||||
|
||||
// See librustc_cratesio_shim/Cargo.toml for a comment explaining this.
|
||||
#[allow(unused_extern_crates)]
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue