diff --git a/Cargo.lock b/Cargo.lock index cde73166bab..a03d542139b 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2321,6 +2321,15 @@ dependencies = [ "rustc-std-workspace-core", ] +[[package]] +name = "odht" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "91e547991b4135151428251dbff22e39ab0f766e0f37cc7d78c5343132be295f" +dependencies = [ + "cfg-if 1.0.0", +] + [[package]] name = "once_cell" version = "1.7.2" @@ -3858,6 +3867,7 @@ version = "0.0.0" name = "rustc_hir" version = "0.0.0" dependencies = [ + "odht", "rustc_ast", "rustc_data_structures", "rustc_feature", @@ -4044,6 +4054,7 @@ name = "rustc_metadata" version = "0.0.0" dependencies = [ "libc", + "odht", "rustc_ast", "rustc_attr", "rustc_data_structures", diff --git a/compiler/rustc_hir/Cargo.toml b/compiler/rustc_hir/Cargo.toml index c92551c2930..ee1b9544e02 100644 --- a/compiler/rustc_hir/Cargo.toml +++ b/compiler/rustc_hir/Cargo.toml @@ -17,3 +17,4 @@ rustc_serialize = { path = "../rustc_serialize" } rustc_ast = { path = "../rustc_ast" } tracing = "0.1" smallvec = { version = "1.6.1", features = ["union", "may_dangle"] } +odht = { version = "0.1", features = ["nightly"] } diff --git a/compiler/rustc_hir/src/def_path_hash_map.rs b/compiler/rustc_hir/src/def_path_hash_map.rs new file mode 100644 index 00000000000..8bfb47af26f --- /dev/null +++ b/compiler/rustc_hir/src/def_path_hash_map.rs @@ -0,0 +1,37 @@ +use rustc_data_structures::fingerprint::Fingerprint; +use rustc_span::def_id::{DefIndex, DefPathHash}; + +#[derive(Clone, Default)] +pub struct Config; + +impl odht::Config for Config { + type Key = DefPathHash; + type Value = DefIndex; + + type EncodedKey = [u8; 16]; + type EncodedValue = [u8; 4]; + + type H = odht::UnHashFn; + + #[inline] + fn encode_key(k: &DefPathHash) -> [u8; 16] { + k.0.to_le_bytes() + } + + #[inline] + fn encode_value(v: &DefIndex) -> [u8; 4] { + v.as_u32().to_le_bytes() + } + + #[inline] + fn decode_key(k: &[u8; 16]) -> DefPathHash { + DefPathHash(Fingerprint::from_le_bytes(*k)) + } + + #[inline] + fn decode_value(v: &[u8; 4]) -> DefIndex { + DefIndex::from_u32(u32::from_le_bytes(*v)) + } +} + +pub type DefPathHashMap = odht::HashTableOwned; diff --git a/compiler/rustc_hir/src/definitions.rs b/compiler/rustc_hir/src/definitions.rs index 5f56f3a32ad..72a0318479f 100644 --- a/compiler/rustc_hir/src/definitions.rs +++ b/compiler/rustc_hir/src/definitions.rs @@ -6,11 +6,11 @@ pub use crate::def_id::DefPathHash; use crate::def_id::{CrateNum, DefIndex, LocalDefId, StableCrateId, CRATE_DEF_INDEX, LOCAL_CRATE}; +use crate::def_path_hash_map::DefPathHashMap; use crate::hir; use rustc_data_structures::fx::FxHashMap; use rustc_data_structures::stable_hasher::StableHasher; -use rustc_data_structures::unhash::UnhashMap; use rustc_index::vec::IndexVec; use rustc_span::hygiene::ExpnId; use rustc_span::symbol::{kw, sym, Symbol}; @@ -28,7 +28,7 @@ use tracing::debug; pub struct DefPathTable { index_to_key: IndexVec, def_path_hashes: IndexVec, - def_path_hash_to_index: UnhashMap, + def_path_hash_to_index: DefPathHashMap, } impl DefPathTable { @@ -44,7 +44,7 @@ impl DefPathTable { // Check for hash collisions of DefPathHashes. These should be // exceedingly rare. - if let Some(existing) = self.def_path_hash_to_index.insert(def_path_hash, index) { + if let Some(existing) = self.def_path_hash_to_index.insert(&def_path_hash, &index) { let def_path1 = DefPath::make(LOCAL_CRATE, existing, |idx| self.def_key(idx)); let def_path2 = DefPath::make(LOCAL_CRATE, index, |idx| self.def_key(idx)); @@ -87,7 +87,7 @@ impl DefPathTable { pub fn enumerated_keys_and_path_hashes( &self, - ) -> impl Iterator + '_ { + ) -> impl Iterator + ExactSizeIterator + '_ { self.index_to_key .iter_enumerated() .map(move |(index, key)| (index, key, &self.def_path_hashes[index])) @@ -110,6 +110,9 @@ pub struct Definitions { expansions_that_defined: FxHashMap, def_id_to_span: IndexVec, + + /// The [StableCrateId] of the local crate. + stable_crate_id: StableCrateId, } /// A unique identifier that we can use to lookup a definition @@ -356,6 +359,7 @@ impl Definitions { hir_id_to_def_id: Default::default(), expansions_that_defined: Default::default(), def_id_to_span, + stable_crate_id, } } @@ -440,10 +444,15 @@ impl Definitions { #[inline(always)] pub fn local_def_path_hash_to_def_id(&self, hash: DefPathHash) -> Option { + debug_assert!(hash.stable_crate_id() == self.stable_crate_id); self.table .def_path_hash_to_index .get(&hash) - .map(|&local_def_index| LocalDefId { local_def_index }) + .map(|local_def_index| LocalDefId { local_def_index }) + } + + pub fn def_path_hash_to_def_index_map(&self) -> &DefPathHashMap { + &self.table.def_path_hash_to_index } } diff --git a/compiler/rustc_hir/src/lib.rs b/compiler/rustc_hir/src/lib.rs index 42575273839..ce36648d6df 100644 --- a/compiler/rustc_hir/src/lib.rs +++ b/compiler/rustc_hir/src/lib.rs @@ -16,6 +16,7 @@ extern crate rustc_data_structures; mod arena; pub mod def; +pub mod def_path_hash_map; pub mod definitions; pub use rustc_span::def_id; mod hir; diff --git a/compiler/rustc_metadata/Cargo.toml b/compiler/rustc_metadata/Cargo.toml index 7c79aa5e00b..b45320c7e8a 100644 --- a/compiler/rustc_metadata/Cargo.toml +++ b/compiler/rustc_metadata/Cargo.toml @@ -8,6 +8,7 @@ doctest = false [dependencies] libc = "0.2" +odht = { version = "0.1", features = ["nightly"] } snap = "1" tracing = "0.1" smallvec = { version = "1.6.1", features = ["union", "may_dangle"] } diff --git a/src/tools/tidy/src/deps.rs b/src/tools/tidy/src/deps.rs index 5f1267fc3d2..60703384e9e 100644 --- a/src/tools/tidy/src/deps.rs +++ b/src/tools/tidy/src/deps.rs @@ -144,6 +144,7 @@ const PERMITTED_DEPENDENCIES: &[&str] = &[ "num-integer", "num-traits", "object", + "odht", "once_cell", "opaque-debug", "parking_lot",