Do not store stable crate id in on-disk hash map.

This commit is contained in:
Camille GILLOT 2023-12-24 12:41:35 +00:00
parent 75ad81ce93
commit 821920b2a3
4 changed files with 17 additions and 13 deletions

View File

@ -25,7 +25,7 @@ impl Hash64 {
pub const ZERO: Hash64 = Hash64 { inner: 0 }; pub const ZERO: Hash64 = Hash64 { inner: 0 };
#[inline] #[inline]
pub(crate) fn new(n: u64) -> Self { pub fn new(n: u64) -> Self {
Self { inner: n } Self { inner: n }
} }

View File

@ -1,21 +1,22 @@
use rustc_data_structures::fingerprint::Fingerprint; use rustc_data_structures::stable_hasher::Hash64;
use rustc_span::def_id::{DefIndex, DefPathHash}; use rustc_span::def_id::DefIndex;
#[derive(Clone, Default)] #[derive(Clone, Default)]
pub struct Config; pub struct Config;
impl odht::Config for Config { impl odht::Config for Config {
type Key = DefPathHash; // This hash-map is single-crate, so we only need to key by the local hash.
type Key = Hash64;
type Value = DefIndex; type Value = DefIndex;
type EncodedKey = [u8; 16]; type EncodedKey = [u8; 8];
type EncodedValue = [u8; 4]; type EncodedValue = [u8; 4];
type H = odht::UnHashFn; type H = odht::UnHashFn;
#[inline] #[inline]
fn encode_key(k: &DefPathHash) -> [u8; 16] { fn encode_key(k: &Hash64) -> [u8; 8] {
k.0.to_le_bytes() k.as_u64().to_le_bytes()
} }
#[inline] #[inline]
@ -24,8 +25,8 @@ impl odht::Config for Config {
} }
#[inline] #[inline]
fn decode_key(k: &[u8; 16]) -> DefPathHash { fn decode_key(k: &[u8; 8]) -> Hash64 {
DefPathHash(Fingerprint::from_le_bytes(*k)) Hash64::new(u64::from_le_bytes(*k))
} }
#[inline] #[inline]

View File

@ -42,6 +42,7 @@ impl DefPathTable {
fn allocate(&mut self, key: DefKey, def_path_hash: DefPathHash) -> DefIndex { fn allocate(&mut self, key: DefKey, def_path_hash: DefPathHash) -> DefIndex {
// Assert that all DefPathHashes correctly contain the local crate's StableCrateId. // Assert that all DefPathHashes correctly contain the local crate's StableCrateId.
debug_assert_eq!(self.stable_crate_id, def_path_hash.stable_crate_id()); debug_assert_eq!(self.stable_crate_id, def_path_hash.stable_crate_id());
let local_hash = def_path_hash.local_hash();
let index = { let index = {
let index = DefIndex::from(self.index_to_key.len()); let index = DefIndex::from(self.index_to_key.len());
@ -49,12 +50,12 @@ impl DefPathTable {
self.index_to_key.push(key); self.index_to_key.push(key);
index index
}; };
self.def_path_hashes.push(def_path_hash.local_hash()); self.def_path_hashes.push(local_hash);
debug_assert!(self.def_path_hashes.len() == self.index_to_key.len()); debug_assert!(self.def_path_hashes.len() == self.index_to_key.len());
// 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(&local_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));
@ -382,7 +383,7 @@ impl Definitions {
debug_assert!(hash.stable_crate_id() == self.table.stable_crate_id); debug_assert!(hash.stable_crate_id() == self.table.stable_crate_id);
self.table self.table
.def_path_hash_to_index .def_path_hash_to_index
.get(&hash) .get(&hash.local_hash())
.map(|local_def_index| LocalDefId { local_def_index }) .map(|local_def_index| LocalDefId { local_def_index })
.unwrap_or_else(|| err()) .unwrap_or_else(|| err())
} }

View File

@ -19,7 +19,9 @@ impl DefPathHashMapRef<'_> {
#[inline] #[inline]
pub fn def_path_hash_to_def_index(&self, def_path_hash: &DefPathHash) -> DefIndex { pub fn def_path_hash_to_def_index(&self, def_path_hash: &DefPathHash) -> DefIndex {
match *self { match *self {
DefPathHashMapRef::OwnedFromMetadata(ref map) => map.get(def_path_hash).unwrap(), DefPathHashMapRef::OwnedFromMetadata(ref map) => {
map.get(&def_path_hash.local_hash()).unwrap()
}
DefPathHashMapRef::BorrowedFromTcx(_) => { DefPathHashMapRef::BorrowedFromTcx(_) => {
panic!("DefPathHashMap::BorrowedFromTcx variant only exists for serialization") panic!("DefPathHashMap::BorrowedFromTcx variant only exists for serialization")
} }