Skip to content

Commit 6142f50

Browse files
committed
Directly encode DefPathHash in metadata.
1 parent 72be5b8 commit 6142f50

File tree

4 files changed

+25
-7
lines changed

4 files changed

+25
-7
lines changed

compiler/rustc_metadata/src/rmeta/decoder.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -1459,9 +1459,9 @@ impl<'a, 'tcx> CrateMetadataRef<'a> {
14591459
index: DefIndex,
14601460
def_path_hashes: &mut FxHashMap<DefIndex, DefPathHash>,
14611461
) -> DefPathHash {
1462-
*def_path_hashes.entry(index).or_insert_with(|| {
1463-
self.root.tables.def_path_hashes.get(self, index).unwrap().decode(self)
1464-
})
1462+
*def_path_hashes
1463+
.entry(index)
1464+
.or_insert_with(|| self.root.tables.def_path_hashes.get(self, index).unwrap())
14651465
}
14661466

14671467
#[inline]

compiler/rustc_metadata/src/rmeta/encoder.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -461,16 +461,15 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
461461
.chain(self.tcx.resolutions(()).proc_macros.iter().map(|p| p.local_def_index))
462462
{
463463
let def_key = self.lazy(table.def_key(def_index));
464-
let def_path_hash = self.lazy(table.def_path_hash(def_index));
464+
let def_path_hash = table.def_path_hash(def_index);
465465
self.tables.def_keys.set(def_index, def_key);
466466
self.tables.def_path_hashes.set(def_index, def_path_hash);
467467
}
468468
} else {
469469
for (def_index, def_key, def_path_hash) in table.enumerated_keys_and_path_hashes() {
470470
let def_key = self.lazy(def_key);
471-
let def_path_hash = self.lazy(def_path_hash);
472471
self.tables.def_keys.set(def_index, def_key);
473-
self.tables.def_path_hashes.set(def_index, def_path_hash);
472+
self.tables.def_path_hashes.set(def_index, *def_path_hash);
474473
}
475474
}
476475
}

compiler/rustc_metadata/src/rmeta/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -332,7 +332,7 @@ define_tables! {
332332
// `DefPathTable` up front, since we may only ever use a few
333333
// definitions from any given crate.
334334
def_keys: Table<DefIndex, Lazy<DefKey>>,
335-
def_path_hashes: Table<DefIndex, Lazy<DefPathHash>>,
335+
def_path_hashes: Table<DefIndex, DefPathHash>,
336336
proc_macro_quoted_spans: Table<usize, Lazy<Span>>,
337337
}
338338

compiler/rustc_metadata/src/rmeta/table.rs

+19
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use crate::rmeta::*;
22

3+
use rustc_data_structures::fingerprint::Fingerprint;
34
use rustc_hir::def::{CtorKind, CtorOf};
45
use rustc_index::vec::Idx;
56
use rustc_serialize::opaque::Encoder;
@@ -181,6 +182,24 @@ fixed_size_enum! {
181182
}
182183
}
183184

185+
// We directly encode `DefPathHash` because a `Lazy` would encur a 25% cost.
186+
impl FixedSizeEncoding for Option<DefPathHash> {
187+
fixed_size_encoding_byte_len_and_defaults!(16);
188+
189+
#[inline]
190+
fn from_bytes(b: &[u8]) -> Self {
191+
Some(DefPathHash(Fingerprint::from_le_bytes(b.try_into().unwrap())))
192+
}
193+
194+
#[inline]
195+
fn write_to_bytes(self, b: &mut [u8]) {
196+
let Some(DefPathHash(fingerprint)) = self else {
197+
panic!("Trying to encode absent DefPathHash.")
198+
};
199+
b[..Self::BYTE_LEN].copy_from_slice(&fingerprint.to_le_bytes());
200+
}
201+
}
202+
184203
// NOTE(eddyb) there could be an impl for `usize`, which would enable a more
185204
// generic `Lazy<T>` impl, but in the general case we might not need / want to
186205
// fit every `usize` in `u32`.

0 commit comments

Comments
 (0)