Use on-disk-hash-table format for DefPathHashMap in hir::definitions.

This commit is contained in:
Michael Woerister 2021-07-20 13:54:37 +02:00
parent 0debea1377
commit d0be27c8ec
7 changed files with 66 additions and 5 deletions

View File

@ -2321,6 +2321,15 @@ dependencies = [
"rustc-std-workspace-core", "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]] [[package]]
name = "once_cell" name = "once_cell"
version = "1.7.2" version = "1.7.2"
@ -3858,6 +3867,7 @@ version = "0.0.0"
name = "rustc_hir" name = "rustc_hir"
version = "0.0.0" version = "0.0.0"
dependencies = [ dependencies = [
"odht",
"rustc_ast", "rustc_ast",
"rustc_data_structures", "rustc_data_structures",
"rustc_feature", "rustc_feature",
@ -4044,6 +4054,7 @@ name = "rustc_metadata"
version = "0.0.0" version = "0.0.0"
dependencies = [ dependencies = [
"libc", "libc",
"odht",
"rustc_ast", "rustc_ast",
"rustc_attr", "rustc_attr",
"rustc_data_structures", "rustc_data_structures",

View File

@ -17,3 +17,4 @@ rustc_serialize = { path = "../rustc_serialize" }
rustc_ast = { path = "../rustc_ast" } rustc_ast = { path = "../rustc_ast" }
tracing = "0.1" tracing = "0.1"
smallvec = { version = "1.6.1", features = ["union", "may_dangle"] } smallvec = { version = "1.6.1", features = ["union", "may_dangle"] }
odht = { version = "0.1", features = ["nightly"] }

View File

@ -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<Config>;

View File

@ -6,11 +6,11 @@
pub use crate::def_id::DefPathHash; pub use crate::def_id::DefPathHash;
use crate::def_id::{CrateNum, DefIndex, LocalDefId, StableCrateId, CRATE_DEF_INDEX, LOCAL_CRATE}; use crate::def_id::{CrateNum, DefIndex, LocalDefId, StableCrateId, CRATE_DEF_INDEX, LOCAL_CRATE};
use crate::def_path_hash_map::DefPathHashMap;
use crate::hir; use crate::hir;
use rustc_data_structures::fx::FxHashMap; use rustc_data_structures::fx::FxHashMap;
use rustc_data_structures::stable_hasher::StableHasher; use rustc_data_structures::stable_hasher::StableHasher;
use rustc_data_structures::unhash::UnhashMap;
use rustc_index::vec::IndexVec; use rustc_index::vec::IndexVec;
use rustc_span::hygiene::ExpnId; use rustc_span::hygiene::ExpnId;
use rustc_span::symbol::{kw, sym, Symbol}; use rustc_span::symbol::{kw, sym, Symbol};
@ -28,7 +28,7 @@ use tracing::debug;
pub struct DefPathTable { pub struct DefPathTable {
index_to_key: IndexVec<DefIndex, DefKey>, index_to_key: IndexVec<DefIndex, DefKey>,
def_path_hashes: IndexVec<DefIndex, DefPathHash>, def_path_hashes: IndexVec<DefIndex, DefPathHash>,
def_path_hash_to_index: UnhashMap<DefPathHash, DefIndex>, def_path_hash_to_index: DefPathHashMap,
} }
impl DefPathTable { impl DefPathTable {
@ -44,7 +44,7 @@ impl DefPathTable {
// Check for hash collisions of DefPathHashes. These should be // Check for hash collisions of DefPathHashes. These should be
// exceedingly rare. // 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_path1 = DefPath::make(LOCAL_CRATE, existing, |idx| self.def_key(idx));
let def_path2 = DefPath::make(LOCAL_CRATE, index, |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( pub fn enumerated_keys_and_path_hashes(
&self, &self,
) -> impl Iterator<Item = (DefIndex, &DefKey, &DefPathHash)> + '_ { ) -> impl Iterator<Item = (DefIndex, &DefKey, &DefPathHash)> + ExactSizeIterator + '_ {
self.index_to_key self.index_to_key
.iter_enumerated() .iter_enumerated()
.map(move |(index, key)| (index, key, &self.def_path_hashes[index])) .map(move |(index, key)| (index, key, &self.def_path_hashes[index]))
@ -110,6 +110,9 @@ pub struct Definitions {
expansions_that_defined: FxHashMap<LocalDefId, ExpnId>, expansions_that_defined: FxHashMap<LocalDefId, ExpnId>,
def_id_to_span: IndexVec<LocalDefId, Span>, def_id_to_span: IndexVec<LocalDefId, Span>,
/// The [StableCrateId] of the local crate.
stable_crate_id: StableCrateId,
} }
/// A unique identifier that we can use to lookup a definition /// A unique identifier that we can use to lookup a definition
@ -356,6 +359,7 @@ impl Definitions {
hir_id_to_def_id: Default::default(), hir_id_to_def_id: Default::default(),
expansions_that_defined: Default::default(), expansions_that_defined: Default::default(),
def_id_to_span, def_id_to_span,
stable_crate_id,
} }
} }
@ -440,10 +444,15 @@ impl Definitions {
#[inline(always)] #[inline(always)]
pub fn local_def_path_hash_to_def_id(&self, hash: DefPathHash) -> Option<LocalDefId> { pub fn local_def_path_hash_to_def_id(&self, hash: DefPathHash) -> Option<LocalDefId> {
debug_assert!(hash.stable_crate_id() == self.stable_crate_id);
self.table self.table
.def_path_hash_to_index .def_path_hash_to_index
.get(&hash) .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
} }
} }

View File

@ -16,6 +16,7 @@ extern crate rustc_data_structures;
mod arena; mod arena;
pub mod def; pub mod def;
pub mod def_path_hash_map;
pub mod definitions; pub mod definitions;
pub use rustc_span::def_id; pub use rustc_span::def_id;
mod hir; mod hir;

View File

@ -8,6 +8,7 @@ doctest = false
[dependencies] [dependencies]
libc = "0.2" libc = "0.2"
odht = { version = "0.1", features = ["nightly"] }
snap = "1" snap = "1"
tracing = "0.1" tracing = "0.1"
smallvec = { version = "1.6.1", features = ["union", "may_dangle"] } smallvec = { version = "1.6.1", features = ["union", "may_dangle"] }

View File

@ -144,6 +144,7 @@ const PERMITTED_DEPENDENCIES: &[&str] = &[
"num-integer", "num-integer",
"num-traits", "num-traits",
"object", "object",
"odht",
"once_cell", "once_cell",
"opaque-debug", "opaque-debug",
"parking_lot", "parking_lot",