Directly encode DefPathHash in metadata.
This commit is contained in:
parent
72be5b81df
commit
6142f50845
4 changed files with 25 additions and 7 deletions
|
@ -1459,9 +1459,9 @@ impl<'a, 'tcx> CrateMetadataRef<'a> {
|
|||
index: DefIndex,
|
||||
def_path_hashes: &mut FxHashMap<DefIndex, DefPathHash>,
|
||||
) -> DefPathHash {
|
||||
*def_path_hashes.entry(index).or_insert_with(|| {
|
||||
self.root.tables.def_path_hashes.get(self, index).unwrap().decode(self)
|
||||
})
|
||||
*def_path_hashes
|
||||
.entry(index)
|
||||
.or_insert_with(|| self.root.tables.def_path_hashes.get(self, index).unwrap())
|
||||
}
|
||||
|
||||
#[inline]
|
||||
|
|
|
@ -461,16 +461,15 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
|
|||
.chain(self.tcx.resolutions(()).proc_macros.iter().map(|p| p.local_def_index))
|
||||
{
|
||||
let def_key = self.lazy(table.def_key(def_index));
|
||||
let def_path_hash = self.lazy(table.def_path_hash(def_index));
|
||||
let def_path_hash = table.def_path_hash(def_index);
|
||||
self.tables.def_keys.set(def_index, def_key);
|
||||
self.tables.def_path_hashes.set(def_index, def_path_hash);
|
||||
}
|
||||
} else {
|
||||
for (def_index, def_key, def_path_hash) in table.enumerated_keys_and_path_hashes() {
|
||||
let def_key = self.lazy(def_key);
|
||||
let def_path_hash = self.lazy(def_path_hash);
|
||||
self.tables.def_keys.set(def_index, def_key);
|
||||
self.tables.def_path_hashes.set(def_index, def_path_hash);
|
||||
self.tables.def_path_hashes.set(def_index, *def_path_hash);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -332,7 +332,7 @@ define_tables! {
|
|||
// `DefPathTable` up front, since we may only ever use a few
|
||||
// definitions from any given crate.
|
||||
def_keys: Table<DefIndex, Lazy<DefKey>>,
|
||||
def_path_hashes: Table<DefIndex, Lazy<DefPathHash>>,
|
||||
def_path_hashes: Table<DefIndex, DefPathHash>,
|
||||
proc_macro_quoted_spans: Table<usize, Lazy<Span>>,
|
||||
}
|
||||
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
use crate::rmeta::*;
|
||||
|
||||
use rustc_data_structures::fingerprint::Fingerprint;
|
||||
use rustc_hir::def::{CtorKind, CtorOf};
|
||||
use rustc_index::vec::Idx;
|
||||
use rustc_serialize::opaque::Encoder;
|
||||
|
@ -181,6 +182,24 @@ fixed_size_enum! {
|
|||
}
|
||||
}
|
||||
|
||||
// We directly encode `DefPathHash` because a `Lazy` would encur a 25% cost.
|
||||
impl FixedSizeEncoding for Option<DefPathHash> {
|
||||
fixed_size_encoding_byte_len_and_defaults!(16);
|
||||
|
||||
#[inline]
|
||||
fn from_bytes(b: &[u8]) -> Self {
|
||||
Some(DefPathHash(Fingerprint::from_le_bytes(b.try_into().unwrap())))
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn write_to_bytes(self, b: &mut [u8]) {
|
||||
let Some(DefPathHash(fingerprint)) = self else {
|
||||
panic!("Trying to encode absent DefPathHash.")
|
||||
};
|
||||
b[..Self::BYTE_LEN].copy_from_slice(&fingerprint.to_le_bytes());
|
||||
}
|
||||
}
|
||||
|
||||
// NOTE(eddyb) there could be an impl for `usize`, which would enable a more
|
||||
// generic `Lazy<T>` impl, but in the general case we might not need / want to
|
||||
// fit every `usize` in `u32`.
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue