rustc_metadata: Encode/decode DefPathHashes without an Option

This commit is contained in:
Vadim Petrochenkov 2023-02-05 18:53:47 +04:00
parent c60cc43985
commit f4e2b954a1
5 changed files with 20 additions and 11 deletions

View File

@ -1319,7 +1319,7 @@ fn def_path_hash_unlocked(
) -> DefPathHash {
*def_path_hashes
.entry(index)
.or_insert_with(|| self.root.tables.def_path_hashes.get(self, index).unwrap())
.or_insert_with(|| self.root.tables.def_path_hashes.get(self, index))
}
#[inline]

View File

@ -478,13 +478,13 @@ fn encode_def_path_table(&mut self) {
let def_key = self.lazy(table.def_key(def_index));
let def_path_hash = table.def_path_hash(def_index);
self.tables.def_keys.set_some(def_index, def_key);
self.tables.def_path_hashes.set_some(def_index, def_path_hash);
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);
self.tables.def_keys.set_some(def_index, def_key);
self.tables.def_path_hashes.set_some(def_index, *def_path_hash);
self.tables.def_path_hashes.set(def_index, *def_path_hash);
}
}
}

View File

@ -350,6 +350,7 @@ fn encode(&self, buf: &mut FileEncoder) -> LazyTables {
is_macro_rules: Table<DefIndex, bool>,
is_type_alias_impl_trait: Table<DefIndex, bool>,
attr_flags: Table<DefIndex, AttrFlags>,
def_path_hashes: Table<DefIndex, DefPathHash>,
explicit_item_bounds: Table<DefIndex, LazyArray<(ty::Predicate<'static>, Span)>>,
inferred_outlives_of: Table<DefIndex, LazyArray<(ty::Clause<'static>, Span)>>,
inherent_impls: Table<DefIndex, LazyArray<DefIndex>>,
@ -403,7 +404,6 @@ fn encode(&self, buf: &mut FileEncoder) -> LazyTables {
// `DefPathTable` up front, since we may only ever use a few
// definitions from any given crate.
def_keys: Table<DefIndex, LazyValue<DefKey>>,
def_path_hashes: Table<DefIndex, DefPathHash>,
proc_macro_quoted_spans: Table<usize, LazyValue<Span>>,
generator_diagnostic_data: Table<DefIndex, LazyValue<GeneratorDiagnosticData<'static>>>,
variant_data: Table<DefIndex, LazyValue<VariantData>>,

View File

@ -44,6 +44,12 @@ fn is_default(&self) -> bool {
}
}
impl IsDefault for DefPathHash {
fn is_default(&self) -> bool {
self.0 == Fingerprint::ZERO
}
}
/// Helper trait, for encoding to, and decoding from, a fixed number of bytes.
/// Used mainly for Lazy positions and lengths.
/// Unchecked invariant: `Self::default()` should encode as `[0; BYTE_LEN]`,
@ -191,21 +197,18 @@ impl FixedSizeEncoding for Option<$ty> {
}
// We directly encode `DefPathHash` because a `LazyValue` would incur a 25% cost.
impl FixedSizeEncoding for Option<DefPathHash> {
impl FixedSizeEncoding for DefPathHash {
type ByteArray = [u8; 16];
#[inline]
fn from_bytes(b: &[u8; 16]) -> Self {
// NOTE: There's a collision between `None` and `Some(0)`.
Some(DefPathHash(Fingerprint::from_le_bytes(*b)))
DefPathHash(Fingerprint::from_le_bytes(*b))
}
#[inline]
fn write_to_bytes(self, b: &mut [u8; 16]) {
match self {
None => unreachable!(),
Some(DefPathHash(fingerprint)) => *b = fingerprint.to_le_bytes(),
}
debug_assert!(!self.is_default());
*b = self.0.to_le_bytes();
}
}

View File

@ -119,6 +119,12 @@ pub fn new(stable_crate_id: StableCrateId, local_hash: u64) -> DefPathHash {
}
}
impl Default for DefPathHash {
fn default() -> Self {
DefPathHash(Fingerprint::ZERO)
}
}
impl Borrow<Fingerprint> for DefPathHash {
#[inline]
fn borrow(&self) -> &Fingerprint {