Skip to content

Commit

Permalink
rustc_metadata: Encode/decode DefPathHashes without an Option
Browse files Browse the repository at this point in the history
  • Loading branch information
petrochenkov committed Feb 5, 2023
1 parent c60cc43 commit f4e2b95
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 11 deletions.
2 changes: 1 addition & 1 deletion compiler/rustc_metadata/src/rmeta/decoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1319,7 +1319,7 @@ impl<'a, 'tcx> CrateMetadataRef<'a> {
) -> 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]
Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_metadata/src/rmeta/encoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -478,13 +478,13 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
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);
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_metadata/src/rmeta/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,7 @@ define_tables! {
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>>,
Expand Down Expand Up @@ -403,7 +404,6 @@ define_tables! {
// `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>>,
Expand Down
17 changes: 10 additions & 7 deletions compiler/rustc_metadata/src/rmeta/table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,12 @@ impl<T> IsDefault for LazyArray<T> {
}
}

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]`,
Expand Down Expand Up @@ -191,21 +197,18 @@ fixed_size_enum! {
}

// 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();
}
}

Expand Down
6 changes: 6 additions & 0 deletions compiler/rustc_span/src/def_id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,12 @@ impl DefPathHash {
}
}

impl Default for DefPathHash {
fn default() -> Self {
DefPathHash(Fingerprint::ZERO)
}
}

impl Borrow<Fingerprint> for DefPathHash {
#[inline]
fn borrow(&self) -> &Fingerprint {
Expand Down

0 comments on commit f4e2b95

Please sign in to comment.