diff options
Diffstat (limited to 'src/librustc_data_structures')
| -rw-r--r-- | src/librustc_data_structures/fx.rs | 115 | ||||
| -rw-r--r-- | src/librustc_data_structures/lib.rs | 1 | ||||
| -rw-r--r-- | src/librustc_data_structures/obligation_forest/mod.rs | 10 | ||||
| -rw-r--r-- | src/librustc_data_structures/snapshot_map/mod.rs | 6 |
4 files changed, 124 insertions, 8 deletions
diff --git a/src/librustc_data_structures/fx.rs b/src/librustc_data_structures/fx.rs new file mode 100644 index 00000000000..1fb7673521d --- /dev/null +++ b/src/librustc_data_structures/fx.rs @@ -0,0 +1,115 @@ +// Copyright 2015 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or +// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license +// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +use std::collections::{HashMap, HashSet}; +use std::default::Default; +use std::hash::{Hasher, Hash, BuildHasherDefault}; +use std::ops::BitXor; + +pub type FxHashMap<K, V> = HashMap<K, V, BuildHasherDefault<FxHasher>>; +pub type FxHashSet<V> = HashSet<V, BuildHasherDefault<FxHasher>>; + +#[allow(non_snake_case)] +pub fn FxHashMap<K: Hash + Eq, V>() -> FxHashMap<K, V> { + HashMap::default() +} + +#[allow(non_snake_case)] +pub fn FxHashSet<V: Hash + Eq>() -> FxHashSet<V> { + HashSet::default() +} + +/// A speedy hash algorithm for use within rustc. The hashmap in libcollections +/// 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 + } +} + +pub fn hash<T: Hash>(v: &T) -> u64 { + let mut state = FxHasher::default(); + v.hash(&mut state); + state.finish() +} diff --git a/src/librustc_data_structures/lib.rs b/src/librustc_data_structures/lib.rs index fc963dac949..fdcbec6bac1 100644 --- a/src/librustc_data_structures/lib.rs +++ b/src/librustc_data_structures/lib.rs @@ -60,6 +60,7 @@ pub mod snapshot_vec; pub mod transitive_relation; pub mod unify; pub mod fnv; +pub mod fx; pub mod tuple_slice; pub mod veccell; pub mod control_flow_graph; diff --git a/src/librustc_data_structures/obligation_forest/mod.rs b/src/librustc_data_structures/obligation_forest/mod.rs index a2bfa784e8a..a46238309bb 100644 --- a/src/librustc_data_structures/obligation_forest/mod.rs +++ b/src/librustc_data_structures/obligation_forest/mod.rs @@ -15,7 +15,7 @@ //! in the first place). See README.md for a general overview of how //! to use this class. -use fnv::{FnvHashMap, FnvHashSet}; +use fx::{FxHashMap, FxHashSet}; use std::cell::Cell; use std::collections::hash_map::Entry; @@ -68,9 +68,9 @@ pub struct ObligationForest<O: ForestObligation> { /// backtrace iterator (which uses `split_at`). nodes: Vec<Node<O>>, /// A cache of predicates that have been successfully completed. - done_cache: FnvHashSet<O::Predicate>, + done_cache: FxHashSet<O::Predicate>, /// An cache of the nodes in `nodes`, indexed by predicate. - waiting_cache: FnvHashMap<O::Predicate, NodeIndex>, + waiting_cache: FxHashMap<O::Predicate, NodeIndex>, /// A list of the obligations added in snapshots, to allow /// for their removal. cache_list: Vec<O::Predicate>, @@ -158,8 +158,8 @@ impl<O: ForestObligation> ObligationForest<O> { ObligationForest { nodes: vec![], snapshots: vec![], - done_cache: FnvHashSet(), - waiting_cache: FnvHashMap(), + done_cache: FxHashSet(), + waiting_cache: FxHashMap(), cache_list: vec![], scratch: Some(vec![]), } diff --git a/src/librustc_data_structures/snapshot_map/mod.rs b/src/librustc_data_structures/snapshot_map/mod.rs index a4e6166032d..cd7143ad3ce 100644 --- a/src/librustc_data_structures/snapshot_map/mod.rs +++ b/src/librustc_data_structures/snapshot_map/mod.rs @@ -8,7 +8,7 @@ // option. This file may not be copied, modified, or distributed // except according to those terms. -use fnv::FnvHashMap; +use fx::FxHashMap; use std::hash::Hash; use std::ops; use std::mem; @@ -19,7 +19,7 @@ mod test; pub struct SnapshotMap<K, V> where K: Hash + Clone + Eq { - map: FnvHashMap<K, V>, + map: FxHashMap<K, V>, undo_log: Vec<UndoLog<K, V>>, } @@ -40,7 +40,7 @@ impl<K, V> SnapshotMap<K, V> { pub fn new() -> Self { SnapshotMap { - map: FnvHashMap(), + map: FxHashMap(), undo_log: vec![], } } |
