Skip to content

Commit

Permalink
Use on-disk-hash-table format for DefPathHashMap in hir::definitions.
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelwoerister committed Sep 14, 2021
1 parent 0debea1 commit d0be27c
Show file tree
Hide file tree
Showing 7 changed files with 66 additions and 5 deletions.
11 changes: 11 additions & 0 deletions Cargo.lock
Expand Up @@ -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"
Expand Down Expand Up @@ -3858,6 +3867,7 @@ version = "0.0.0"
name = "rustc_hir"
version = "0.0.0"
dependencies = [
"odht",
"rustc_ast",
"rustc_data_structures",
"rustc_feature",
Expand Down Expand Up @@ -4044,6 +4054,7 @@ name = "rustc_metadata"
version = "0.0.0"
dependencies = [
"libc",
"odht",
"rustc_ast",
"rustc_attr",
"rustc_data_structures",
Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_hir/Cargo.toml
Expand Up @@ -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"] }
37 changes: 37 additions & 0 deletions 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<Config>;
19 changes: 14 additions & 5 deletions compiler/rustc_hir/src/definitions.rs
Expand Up @@ -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};
Expand All @@ -28,7 +28,7 @@ use tracing::debug;
pub struct DefPathTable {
index_to_key: IndexVec<DefIndex, DefKey>,
def_path_hashes: IndexVec<DefIndex, DefPathHash>,
def_path_hash_to_index: UnhashMap<DefPathHash, DefIndex>,
def_path_hash_to_index: DefPathHashMap,
}

impl DefPathTable {
Expand All @@ -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));

Expand Down Expand Up @@ -87,7 +87,7 @@ impl DefPathTable {

pub fn enumerated_keys_and_path_hashes(
&self,
) -> impl Iterator<Item = (DefIndex, &DefKey, &DefPathHash)> + '_ {
) -> impl Iterator<Item = (DefIndex, &DefKey, &DefPathHash)> + ExactSizeIterator + '_ {
self.index_to_key
.iter_enumerated()
.map(move |(index, key)| (index, key, &self.def_path_hashes[index]))
Expand All @@ -110,6 +110,9 @@ pub struct Definitions {
expansions_that_defined: FxHashMap<LocalDefId, ExpnId>,

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
Expand Down Expand Up @@ -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,
}
}

Expand Down Expand Up @@ -440,10 +444,15 @@ impl Definitions {

#[inline(always)]
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
.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
}
}

Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_hir/src/lib.rs
Expand Up @@ -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;
Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_metadata/Cargo.toml
Expand Up @@ -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"] }
Expand Down
1 change: 1 addition & 0 deletions src/tools/tidy/src/deps.rs
Expand Up @@ -144,6 +144,7 @@ const PERMITTED_DEPENDENCIES: &[&str] = &[
"num-integer",
"num-traits",
"object",
"odht",
"once_cell",
"opaque-debug",
"parking_lot",
Expand Down

0 comments on commit d0be27c

Please sign in to comment.