Skip to content

Commit dc450f9

Browse files
committed
Auto merge of #119259 - cjgillot:single-crate-id, r=Mark-Simulacrum
Only store StableCrateId once in DefPathTable. #119238 made me think of this. cc `@Mark-Simulacrum`
2 parents 1a7e97f + 821920b commit dc450f9

File tree

4 files changed

+40
-32
lines changed

4 files changed

+40
-32
lines changed

compiler/rustc_data_structures/src/hashes.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ impl Hash64 {
2525
pub const ZERO: Hash64 = Hash64 { inner: 0 };
2626

2727
#[inline]
28-
pub(crate) fn new(n: u64) -> Self {
28+
pub fn new(n: u64) -> Self {
2929
Self { inner: n }
3030
}
3131

compiler/rustc_hir/src/def_path_hash_map.rs

+9-8
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,22 @@
1-
use rustc_data_structures::fingerprint::Fingerprint;
2-
use rustc_span::def_id::{DefIndex, DefPathHash};
1+
use rustc_data_structures::stable_hasher::Hash64;
2+
use rustc_span::def_id::DefIndex;
33

44
#[derive(Clone, Default)]
55
pub struct Config;
66

77
impl odht::Config for Config {
8-
type Key = DefPathHash;
8+
// This hash-map is single-crate, so we only need to key by the local hash.
9+
type Key = Hash64;
910
type Value = DefIndex;
1011

11-
type EncodedKey = [u8; 16];
12+
type EncodedKey = [u8; 8];
1213
type EncodedValue = [u8; 4];
1314

1415
type H = odht::UnHashFn;
1516

1617
#[inline]
17-
fn encode_key(k: &DefPathHash) -> [u8; 16] {
18-
k.0.to_le_bytes()
18+
fn encode_key(k: &Hash64) -> [u8; 8] {
19+
k.as_u64().to_le_bytes()
1920
}
2021

2122
#[inline]
@@ -24,8 +25,8 @@ impl odht::Config for Config {
2425
}
2526

2627
#[inline]
27-
fn decode_key(k: &[u8; 16]) -> DefPathHash {
28-
DefPathHash(Fingerprint::from_le_bytes(*k))
28+
fn decode_key(k: &[u8; 8]) -> Hash64 {
29+
Hash64::new(u64::from_le_bytes(*k))
2930
}
3031

3132
#[inline]

compiler/rustc_hir/src/definitions.rs

+27-22
Original file line numberDiff line numberDiff line change
@@ -20,27 +20,42 @@ use std::hash::Hash;
2020
/// Internally the `DefPathTable` holds a tree of `DefKey`s, where each `DefKey`
2121
/// stores the `DefIndex` of its parent.
2222
/// There is one `DefPathTable` for each crate.
23-
#[derive(Clone, Default, Debug)]
23+
#[derive(Debug)]
2424
pub struct DefPathTable {
25+
stable_crate_id: StableCrateId,
2526
index_to_key: IndexVec<DefIndex, DefKey>,
26-
def_path_hashes: IndexVec<DefIndex, DefPathHash>,
27+
// We do only store the local hash, as all the definitions are from the current crate.
28+
def_path_hashes: IndexVec<DefIndex, Hash64>,
2729
def_path_hash_to_index: DefPathHashMap,
2830
}
2931

3032
impl DefPathTable {
33+
fn new(stable_crate_id: StableCrateId) -> DefPathTable {
34+
DefPathTable {
35+
stable_crate_id,
36+
index_to_key: Default::default(),
37+
def_path_hashes: Default::default(),
38+
def_path_hash_to_index: Default::default(),
39+
}
40+
}
41+
3142
fn allocate(&mut self, key: DefKey, def_path_hash: DefPathHash) -> DefIndex {
43+
// Assert that all DefPathHashes correctly contain the local crate's StableCrateId.
44+
debug_assert_eq!(self.stable_crate_id, def_path_hash.stable_crate_id());
45+
let local_hash = def_path_hash.local_hash();
46+
3247
let index = {
3348
let index = DefIndex::from(self.index_to_key.len());
3449
debug!("DefPathTable::insert() - {:?} <-> {:?}", key, index);
3550
self.index_to_key.push(key);
3651
index
3752
};
38-
self.def_path_hashes.push(def_path_hash);
53+
self.def_path_hashes.push(local_hash);
3954
debug_assert!(self.def_path_hashes.len() == self.index_to_key.len());
4055

4156
// Check for hash collisions of DefPathHashes. These should be
4257
// exceedingly rare.
43-
if let Some(existing) = self.def_path_hash_to_index.insert(&def_path_hash, &index) {
58+
if let Some(existing) = self.def_path_hash_to_index.insert(&local_hash, &index) {
4459
let def_path1 = DefPath::make(LOCAL_CRATE, existing, |idx| self.def_key(idx));
4560
let def_path2 = DefPath::make(LOCAL_CRATE, index, |idx| self.def_key(idx));
4661

@@ -58,13 +73,6 @@ impl DefPathTable {
5873
);
5974
}
6075

61-
// Assert that all DefPathHashes correctly contain the local crate's
62-
// StableCrateId
63-
#[cfg(debug_assertions)]
64-
if let Some(root) = self.def_path_hashes.get(CRATE_DEF_INDEX) {
65-
assert!(def_path_hash.stable_crate_id() == root.stable_crate_id());
66-
}
67-
6876
index
6977
}
7078

@@ -73,19 +81,19 @@ impl DefPathTable {
7381
self.index_to_key[index]
7482
}
7583

84+
#[instrument(level = "trace", skip(self), ret)]
7685
#[inline(always)]
7786
pub fn def_path_hash(&self, index: DefIndex) -> DefPathHash {
7887
let hash = self.def_path_hashes[index];
79-
debug!("def_path_hash({:?}) = {:?}", index, hash);
80-
hash
88+
DefPathHash::new(self.stable_crate_id, hash)
8189
}
8290

8391
pub fn enumerated_keys_and_path_hashes(
8492
&self,
85-
) -> impl Iterator<Item = (DefIndex, &DefKey, &DefPathHash)> + ExactSizeIterator + '_ {
93+
) -> impl Iterator<Item = (DefIndex, &DefKey, DefPathHash)> + ExactSizeIterator + '_ {
8694
self.index_to_key
8795
.iter_enumerated()
88-
.map(move |(index, key)| (index, key, &self.def_path_hashes[index]))
96+
.map(move |(index, key)| (index, key, self.def_path_hash(index)))
8997
}
9098
}
9199

@@ -96,9 +104,6 @@ impl DefPathTable {
96104
pub struct Definitions {
97105
table: DefPathTable,
98106
next_disambiguator: UnordMap<(LocalDefId, DefPathData), u32>,
99-
100-
/// The [StableCrateId] of the local crate.
101-
stable_crate_id: StableCrateId,
102107
}
103108

104109
/// A unique identifier that we can use to lookup a definition
@@ -329,11 +334,11 @@ impl Definitions {
329334
let def_path_hash = key.compute_stable_hash(parent_hash);
330335

331336
// Create the root definition.
332-
let mut table = DefPathTable::default();
337+
let mut table = DefPathTable::new(stable_crate_id);
333338
let root = LocalDefId { local_def_index: table.allocate(key, def_path_hash) };
334339
assert_eq!(root.local_def_index, CRATE_DEF_INDEX);
335340

336-
Definitions { table, next_disambiguator: Default::default(), stable_crate_id }
341+
Definitions { table, next_disambiguator: Default::default() }
337342
}
338343

339344
/// Adds a definition with a parent definition.
@@ -375,10 +380,10 @@ impl Definitions {
375380
hash: DefPathHash,
376381
err: &mut dyn FnMut() -> !,
377382
) -> LocalDefId {
378-
debug_assert!(hash.stable_crate_id() == self.stable_crate_id);
383+
debug_assert!(hash.stable_crate_id() == self.table.stable_crate_id);
379384
self.table
380385
.def_path_hash_to_index
381-
.get(&hash)
386+
.get(&hash.local_hash())
382387
.map(|local_def_index| LocalDefId { local_def_index })
383388
.unwrap_or_else(|| err())
384389
}

compiler/rustc_metadata/src/rmeta/def_path_hash_map.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@ impl DefPathHashMapRef<'_> {
1919
#[inline]
2020
pub fn def_path_hash_to_def_index(&self, def_path_hash: &DefPathHash) -> DefIndex {
2121
match *self {
22-
DefPathHashMapRef::OwnedFromMetadata(ref map) => map.get(def_path_hash).unwrap(),
22+
DefPathHashMapRef::OwnedFromMetadata(ref map) => {
23+
map.get(&def_path_hash.local_hash()).unwrap()
24+
}
2325
DefPathHashMapRef::BorrowedFromTcx(_) => {
2426
panic!("DefPathHashMap::BorrowedFromTcx variant only exists for serialization")
2527
}

0 commit comments

Comments
 (0)