Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions compiler/rustc_metadata/src/rmeta/decoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ pub(super) trait LazyDecoder: BlobDecoder {
self.read_lazy_offset_then(|pos| LazyArray::from_position_and_num_elems(pos, len))
}

fn read_lazy_table<I, T>(&mut self, width: usize, len: usize) -> LazyTable<I, T> {
fn read_lazy_table<Ie, Id, T>(&mut self, width: usize, len: usize) -> LazyTable<Ie, Id, T> {
self.read_lazy_offset_then(|pos| LazyTable::from_position_and_encoded_size(pos, width, len))
}

Expand Down Expand Up @@ -667,7 +667,7 @@ impl<D: LazyDecoder, T> Decodable<D> for LazyArray<T> {
}
}

impl<I: Idx, D: LazyDecoder, T> Decodable<D> for LazyTable<I, T> {
impl<Ie: Idx, Id: Idx, D: LazyDecoder, T> Decodable<D> for LazyTable<Ie, Id, T> {
fn decode(decoder: &mut D) -> Self {
let width = decoder.read_usize();
let len = decoder.read_usize();
Expand Down
12 changes: 7 additions & 5 deletions compiler/rustc_metadata/src/rmeta/encoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ impl<'a, 'tcx, T> Encodable<EncodeContext<'a, 'tcx>> for LazyArray<T> {
}
}

impl<'a, 'tcx, I, T> Encodable<EncodeContext<'a, 'tcx>> for LazyTable<I, T> {
impl<'a, 'tcx, Ie, Id, T> Encodable<EncodeContext<'a, 'tcx>> for LazyTable<Ie, Id, T> {
fn encode(&self, e: &mut EncodeContext<'a, 'tcx>) {
e.emit_usize(self.width);
e.emit_usize(self.len);
Expand Down Expand Up @@ -547,7 +547,9 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
self.lazy(DefPathHashMapRef::BorrowedFromTcx(self.tcx.def_path_hash_to_def_index_map()))
}

fn encode_source_map(&mut self) -> LazyTable<u32, Option<LazyValue<rustc_span::SourceFile>>> {
fn encode_source_map(
&mut self,
) -> LazyTable<u32, u32, Option<LazyValue<rustc_span::SourceFile>>> {
let source_map = self.tcx.sess.source_map();
let all_source_files = source_map.files();

Expand Down Expand Up @@ -2004,9 +2006,9 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
}

fn encode_hygiene(&mut self) -> (SyntaxContextTable, ExpnDataTable, ExpnHashTable) {
let mut syntax_contexts: TableBuilder<_, _> = Default::default();
let mut expn_data_table: TableBuilder<_, _> = Default::default();
let mut expn_hash_table: TableBuilder<_, _> = Default::default();
let mut syntax_contexts: TableBuilder<_, _, _> = Default::default();
let mut expn_data_table: TableBuilder<_, _, _> = Default::default();
let mut expn_hash_table: TableBuilder<_, _, _> = Default::default();

HygieneEncodeContext::encode(
&Rc::clone(&self.hygiene_ctxt),
Expand Down
34 changes: 20 additions & 14 deletions compiler/rustc_metadata/src/rmeta/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ use rustc_target::spec::{PanicStrategy, TargetTuple};
use table::TableBuilder;

use crate::eii::EiiMapEncodedKeyValue;
use crate::rmeta::table::TableBuilderSingleIdx;

mod decoder;
mod def_path_hash_map;
Expand Down Expand Up @@ -143,22 +144,27 @@ impl<T> LazyArray<T> {
/// Random-access table (i.e. offering constant-time `get`/`set`), similar to
/// `LazyArray<T>`, but without requiring encoding or decoding all the values
/// eagerly and in-order.
struct LazyTable<I, T> {
///
/// `IdxEncode` - is a type of index that is used to write to the table,
/// `IdxDecode` - is a type of index that is used to read from the table.
struct LazyTable<IdxEncode, IdxDecode, T> {
position: NonZero<usize>,
/// The encoded size of the elements of a table is selected at runtime to drop
/// trailing zeroes. This is the number of bytes used for each table element.
width: usize,
/// How many elements are in the table.
len: usize,
_marker: PhantomData<fn(I) -> T>,
_marker: PhantomData<fn(IdxEncode, IdxDecode) -> T>,
}

impl<I, T> LazyTable<I, T> {
type LazyTableSingleIdx<TIdx, TValue> = LazyTable<TIdx, TIdx, TValue>;

impl<Ie, Id, T> LazyTable<Ie, Id, T> {
fn from_position_and_encoded_size(
position: NonZero<usize>,
width: usize,
len: usize,
) -> LazyTable<I, T> {
) -> LazyTable<Ie, Id, T> {
LazyTable { position, width, len, _marker: PhantomData }
}
}
Expand All @@ -177,8 +183,8 @@ impl<T> Clone for LazyArray<T> {
}
}

impl<I, T> Copy for LazyTable<I, T> {}
impl<I, T> Clone for LazyTable<I, T> {
impl<Ie, Id, T> Copy for LazyTable<Ie, Id, T> {}
impl<Ie, Id, T> Clone for LazyTable<Ie, Id, T> {
fn clone(&self) -> Self {
*self
}
Expand All @@ -199,9 +205,9 @@ enum LazyState {
Previous(NonZero<usize>),
}

type SyntaxContextTable = LazyTable<u32, Option<LazyValue<SyntaxContextKey>>>;
type ExpnDataTable = LazyTable<ExpnIndex, Option<LazyValue<ExpnData>>>;
type ExpnHashTable = LazyTable<ExpnIndex, Option<LazyValue<ExpnHash>>>;
type SyntaxContextTable = LazyTableSingleIdx<u32, Option<LazyValue<SyntaxContextKey>>>;
type ExpnDataTable = LazyTableSingleIdx<ExpnIndex, Option<LazyValue<ExpnData>>>;
type ExpnHashTable = LazyTableSingleIdx<ExpnIndex, Option<LazyValue<ExpnHash>>>;

#[derive(MetadataEncodable, LazyDecodable)]
pub(crate) struct ProcMacroData {
Expand Down Expand Up @@ -305,7 +311,7 @@ pub(crate) struct CrateRoot {

def_path_hash_map: LazyValue<DefPathHashMapRef<'static>>,

source_map: LazyTable<u32, Option<LazyValue<rustc_span::SourceFile>>>,
source_map: LazyTableSingleIdx<u32, Option<LazyValue<rustc_span::SourceFile>>>,
target_modifiers: LazyArray<TargetModifier>,
denied_partial_mitigations: LazyArray<DeniedPartialMitigation>,

Expand Down Expand Up @@ -389,14 +395,14 @@ macro_rules! define_tables {
) => {
#[derive(MetadataEncodable, LazyDecodable)]
pub(crate) struct LazyTables {
$($name1: LazyTable<$IDX1, $T1>,)+
$($name2: LazyTable<$IDX2, Option<$T2>>,)+
$($name1: LazyTableSingleIdx<$IDX1, $T1>,)+
$($name2: LazyTableSingleIdx<$IDX2, Option<$T2>>,)+
}

#[derive(Default)]
struct TableBuilders {
$($name1: TableBuilder<$IDX1, $T1>,)+
$($name2: TableBuilder<$IDX2, Option<$T2>>,)+
$($name1: TableBuilderSingleIdx<$IDX1, $T1>,)+
$($name2: TableBuilderSingleIdx<$IDX2, Option<$T2>>,)+
}

impl TableBuilders {
Expand Down
32 changes: 20 additions & 12 deletions compiler/rustc_metadata/src/rmeta/table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -427,34 +427,38 @@ impl<T> FixedSizeEncoding for Option<LazyArray<T>> {
}

/// Helper for constructing a table's serialization (also see `Table`).
pub(super) struct TableBuilder<I: Idx, T: FixedSizeEncoding> {
pub(super) struct TableBuilder<IdxEncode: Idx, IdxDecode: Idx, T: FixedSizeEncoding> {
width: usize,
blocks: IndexVec<I, T::ByteArray>,
_marker: PhantomData<T>,
blocks: IndexVec<IdxEncode, T::ByteArray>,
_marker: PhantomData<(IdxDecode, T)>,
}

impl<I: Idx, T: FixedSizeEncoding> Default for TableBuilder<I, T> {
pub(super) type TableBuilderSingleIdx<TIdx, TValue> = TableBuilder<TIdx, TIdx, TValue>;

impl<Ie: Idx, Id: Idx, T: FixedSizeEncoding> Default for TableBuilder<Ie, Id, T> {
fn default() -> Self {
TableBuilder { width: 0, blocks: Default::default(), _marker: PhantomData }
}
}

impl<I: Idx, const N: usize, T> TableBuilder<I, Option<T>>
impl<Ie: Idx, Id: Idx, const N: usize, T> TableBuilder<Ie, Id, Option<T>>
where
Option<T>: FixedSizeEncoding<ByteArray = [u8; N]>,
{
pub(crate) fn set_some(&mut self, i: I, value: T) {
pub(crate) fn set_some(&mut self, i: Ie, value: T) {
self.set(i, Some(value))
}
}

impl<I: Idx, const N: usize, T: FixedSizeEncoding<ByteArray = [u8; N]>> TableBuilder<I, T> {
impl<Ie: Idx, Id: Idx, const N: usize, T: FixedSizeEncoding<ByteArray = [u8; N]>>
TableBuilder<Ie, Id, T>
{
/// Sets the table value if it is not default.
/// ATTENTION: For optimization default values are simply ignored by this function, because
/// right now metadata tables never need to reset non-default values to default. If such need
/// arises in the future then a new method (e.g. `clear` or `reset`) will need to be introduced
/// for doing that explicitly.
pub(crate) fn set(&mut self, i: I, value: T) {
pub(crate) fn set(&mut self, i: Ie, value: T) {
#[cfg(debug_assertions)]
{
debug_assert!(
Expand All @@ -477,7 +481,7 @@ impl<I: Idx, const N: usize, T: FixedSizeEncoding<ByteArray = [u8; N]>> TableBui
}
}

pub(crate) fn encode(&self, buf: &mut FileEncoder<'_>) -> LazyTable<I, T> {
pub(crate) fn encode(&self, buf: &mut FileEncoder<'_>) -> LazyTable<Ie, Id, T> {
let pos = buf.position();

let width = self.width;
Expand All @@ -500,13 +504,17 @@ fn trailing_zeros(x: &[u8]) -> usize {
x.iter().rev().take_while(|b| **b == 0).count()
}

impl<I: Idx, const N: usize, T: FixedSizeEncoding<ByteArray = [u8; N]> + ParameterizedOverTcx>
LazyTable<I, T>
impl<
Ie: Idx,
Id: Idx,
const N: usize,
T: FixedSizeEncoding<ByteArray = [u8; N]> + ParameterizedOverTcx,
> LazyTable<Ie, Id, T>
where
for<'tcx> T::Value<'tcx>: FixedSizeEncoding<ByteArray = [u8; N]>,
{
/// Given the metadata, extract out the value at a particular index (if any).
pub(super) fn get<'a, 'tcx, M: MetaBlob<'a>>(&self, metadata: M, i: I) -> T::Value<'tcx> {
pub(super) fn get<'a, 'tcx, M: MetaBlob<'a>>(&self, metadata: M, i: Id) -> T::Value<'tcx> {
// Access past the end of the table returns a Default
if i.index() >= self.len {
return Default::default();
Expand Down
Loading