Skip to content
Browse files

rustc_metadata: replace PerDefTable<T> with Table<DefIndex, T>.

  • Loading branch information
eddyb committed Nov 13, 2019
1 parent 876a72a commit 67421bdfdf278f3737595c27a77d90bacd4215f3
@@ -1,9 +1,9 @@
// Decoding metadata from a single crate's metadata

use crate::rmeta::*;
use crate::rmeta::table::{FixedSizeEncoding, PerDefTable};
use crate::rmeta::table::{FixedSizeEncoding, Table};

use rustc_index::vec::IndexVec;
use rustc_index::vec::{Idx, IndexVec};
use rustc_data_structures::sync::{Lrc, Lock, Once, AtomicCell};
use rustc::hir::map::{DefKey, DefPath, DefPathData, DefPathHash};
use rustc::hir::map::definitions::DefPathTable;
@@ -341,10 +341,10 @@ impl<'a, 'tcx, T: Encodable> SpecializedDecoder<Lazy<[T]>> for DecodeContext<'a,

impl<'a, 'tcx, T> SpecializedDecoder<Lazy<PerDefTable<T>>> for DecodeContext<'a, 'tcx>
impl<'a, 'tcx, I: Idx, T> SpecializedDecoder<Lazy<Table<I, T>>> for DecodeContext<'a, 'tcx>
where Option<T>: FixedSizeEncoding,
fn specialized_decode(&mut self) -> Result<Lazy<PerDefTable<T>>, Self::Error> {
fn specialized_decode(&mut self) -> Result<Lazy<Table<I, T>>, Self::Error> {
let len = self.read_usize()?;
@@ -1,5 +1,5 @@
use crate::rmeta::*;
use crate::rmeta::table::{FixedSizeEncoding, PerDefTable};
use crate::rmeta::table::{FixedSizeEncoding, Table};

use rustc::middle::cstore::{LinkagePreference, NativeLibrary,
EncodedMetadata, ForeignModule};
@@ -8,7 +8,7 @@ use rustc::hir::def_id::{CrateNum, CRATE_DEF_INDEX, DefIndex, DefId, LocalDefId,
use rustc::hir::{GenericParamKind, AnonConst};
use rustc::hir::map::definitions::DefPathTable;
use rustc_data_structures::fingerprint::Fingerprint;
use rustc_index::vec::IndexVec;
use rustc_index::vec::{Idx, IndexVec};
use rustc::middle::dependency_format::Linkage;
use rustc::middle::exported_symbols::{ExportedSymbol, SymbolExportLevel,
@@ -62,26 +62,26 @@ struct EncodeContext<'tcx> {

struct PerDefTables<'tcx> {
kind: PerDefTable<Lazy<EntryKind<'tcx>>>,
visibility: PerDefTable<Lazy<ty::Visibility>>,
span: PerDefTable<Lazy<Span>>,
attributes: PerDefTable<Lazy<[ast::Attribute]>>,
children: PerDefTable<Lazy<[DefIndex]>>,
stability: PerDefTable<Lazy<attr::Stability>>,
deprecation: PerDefTable<Lazy<attr::Deprecation>>,

ty: PerDefTable<Lazy<Ty<'tcx>>>,
fn_sig: PerDefTable<Lazy<ty::PolyFnSig<'tcx>>>,
impl_trait_ref: PerDefTable<Lazy<ty::TraitRef<'tcx>>>,
inherent_impls: PerDefTable<Lazy<[DefIndex]>>,
variances: PerDefTable<Lazy<[ty::Variance]>>,
generics: PerDefTable<Lazy<ty::Generics>>,
explicit_predicates: PerDefTable<Lazy<ty::GenericPredicates<'tcx>>>,
inferred_outlives: PerDefTable<Lazy<&'tcx [(ty::Predicate<'tcx>, Span)]>>,
super_predicates: PerDefTable<Lazy<ty::GenericPredicates<'tcx>>>,

mir: PerDefTable<Lazy<mir::Body<'tcx>>>,
promoted_mir: PerDefTable<Lazy<IndexVec<mir::Promoted, mir::Body<'tcx>>>>,
kind: Table<DefIndex, Lazy<EntryKind<'tcx>>>,
visibility: Table<DefIndex, Lazy<ty::Visibility>>,
span: Table<DefIndex, Lazy<Span>>,
attributes: Table<DefIndex, Lazy<[ast::Attribute]>>,
children: Table<DefIndex, Lazy<[DefIndex]>>,
stability: Table<DefIndex, Lazy<attr::Stability>>,
deprecation: Table<DefIndex, Lazy<attr::Deprecation>>,

ty: Table<DefIndex, Lazy<Ty<'tcx>>>,
fn_sig: Table<DefIndex, Lazy<ty::PolyFnSig<'tcx>>>,
impl_trait_ref: Table<DefIndex, Lazy<ty::TraitRef<'tcx>>>,
inherent_impls: Table<DefIndex, Lazy<[DefIndex]>>,
variances: Table<DefIndex, Lazy<[ty::Variance]>>,
generics: Table<DefIndex, Lazy<ty::Generics>>,
explicit_predicates: Table<DefIndex, Lazy<ty::GenericPredicates<'tcx>>>,
inferred_outlives: Table<DefIndex, Lazy<&'tcx [(ty::Predicate<'tcx>, Span)]>>,
super_predicates: Table<DefIndex, Lazy<ty::GenericPredicates<'tcx>>>,

mir: Table<DefIndex, Lazy<mir::Body<'tcx>>>,
promoted_mir: Table<DefIndex, Lazy<IndexVec<mir::Promoted, mir::Body<'tcx>>>>,

macro_rules! encoder_methods {
@@ -138,10 +138,10 @@ impl<'tcx, T: Encodable> SpecializedEncoder<Lazy<[T]>> for EncodeContext<'tcx> {

impl<'tcx, T> SpecializedEncoder<Lazy<PerDefTable<T>>> for EncodeContext<'tcx>
impl<'tcx, I: Idx, T> SpecializedEncoder<Lazy<Table<I, T>>> for EncodeContext<'tcx>
where Option<T>: FixedSizeEncoding,
fn specialized_encode(&mut self, lazy: &Lazy<PerDefTable<T>>) -> Result<(), Self::Error> {
fn specialized_encode(&mut self, lazy: &Lazy<Table<I, T>>) -> Result<(), Self::Error> {
@@ -307,14 +307,14 @@ impl<I, T: Encodable> EncodeContentsForLazy<[T]> for I

// Shorthand for `$self.$tables.$table.set($key, $self.lazy($value))`, which would
// Shorthand for `$self.$tables.$table.set($def_id.index, $self.lazy($value))`, which would
// normally need extra variables to avoid errors about multiple mutable borrows.
macro_rules! record {
($self:ident.$tables:ident.$table:ident[$key:expr] <- $value:expr) => {{
($self:ident.$tables:ident.$table:ident[$def_id:expr] <- $value:expr) => {{
let value = $value;
let lazy = $self.lazy(value);
$self.$tables.$table.set($key, lazy);
$self.$tables.$table.set($def_id.index, lazy);
@@ -1,5 +1,5 @@
use decoder::Metadata;
use table::PerDefTable;
use table::Table;

use rustc::hir;
use rustc::hir::def::{self, CtorKind};
@@ -166,8 +166,7 @@ enum LazyState {
// manually, instead of relying on the default, to get the correct variance.
// Only needed when `T` itself contains a parameter (e.g. `'tcx`).
macro_rules! Lazy {
(Table<$T:ty>) => {Lazy<Table<$T>, usize>};
(PerDefTable<$T:ty>) => {Lazy<PerDefTable<$T>, usize>};
(Table<$I:ty, $T:ty>) => {Lazy<Table<$I, $T>, usize>};
([$T:ty]) => {Lazy<[$T], usize>};
($T:ty) => {Lazy<$T, ()>};
@@ -234,29 +233,29 @@ crate struct TraitImpls {

#[derive(RustcEncodable, RustcDecodable)]
crate struct LazyPerDefTables<'tcx> {
kind: Lazy!(PerDefTable<Lazy!(EntryKind<'tcx>)>),
visibility: Lazy!(PerDefTable<Lazy<ty::Visibility>>),
span: Lazy!(PerDefTable<Lazy<Span>>),
attributes: Lazy!(PerDefTable<Lazy<[ast::Attribute]>>),
children: Lazy!(PerDefTable<Lazy<[DefIndex]>>),
stability: Lazy!(PerDefTable<Lazy<attr::Stability>>),
deprecation: Lazy!(PerDefTable<Lazy<attr::Deprecation>>),
ty: Lazy!(PerDefTable<Lazy!(Ty<'tcx>)>),
fn_sig: Lazy!(PerDefTable<Lazy!(ty::PolyFnSig<'tcx>)>),
impl_trait_ref: Lazy!(PerDefTable<Lazy!(ty::TraitRef<'tcx>)>),
inherent_impls: Lazy!(PerDefTable<Lazy<[DefIndex]>>),
variances: Lazy!(PerDefTable<Lazy<[ty::Variance]>>),
generics: Lazy!(PerDefTable<Lazy<ty::Generics>>),
explicit_predicates: Lazy!(PerDefTable<Lazy!(ty::GenericPredicates<'tcx>)>),
kind: Lazy!(Table<DefIndex, Lazy!(EntryKind<'tcx>)>),
visibility: Lazy!(Table<DefIndex, Lazy<ty::Visibility>>),
span: Lazy!(Table<DefIndex, Lazy<Span>>),
attributes: Lazy!(Table<DefIndex, Lazy<[ast::Attribute]>>),
children: Lazy!(Table<DefIndex, Lazy<[DefIndex]>>),
stability: Lazy!(Table<DefIndex, Lazy<attr::Stability>>),
deprecation: Lazy!(Table<DefIndex, Lazy<attr::Deprecation>>),
ty: Lazy!(Table<DefIndex, Lazy!(Ty<'tcx>)>),
fn_sig: Lazy!(Table<DefIndex, Lazy!(ty::PolyFnSig<'tcx>)>),
impl_trait_ref: Lazy!(Table<DefIndex, Lazy!(ty::TraitRef<'tcx>)>),
inherent_impls: Lazy!(Table<DefIndex, Lazy<[DefIndex]>>),
variances: Lazy!(Table<DefIndex, Lazy<[ty::Variance]>>),
generics: Lazy!(Table<DefIndex, Lazy<ty::Generics>>),
explicit_predicates: Lazy!(Table<DefIndex, Lazy!(ty::GenericPredicates<'tcx>)>),
// FIXME(eddyb) this would ideally be `Lazy<[...]>` but `ty::Predicate`
// doesn't handle shorthands in its own (de)serialization impls,
// as it's an `enum` for which we want to derive (de)serialization,
// so the `ty::codec` APIs handle the whole `&'tcx [...]` at once.
// Also, as an optimization, a missing entry indicates an empty `&[]`.
inferred_outlives: Lazy!(PerDefTable<Lazy!(&'tcx [(ty::Predicate<'tcx>, Span)])>),
super_predicates: Lazy!(PerDefTable<Lazy!(ty::GenericPredicates<'tcx>)>),
mir: Lazy!(PerDefTable<Lazy!(mir::Body<'tcx>)>),
promoted_mir: Lazy!(PerDefTable<Lazy!(IndexVec<mir::Promoted, mir::Body<'tcx>>)>),
inferred_outlives: Lazy!(Table<DefIndex, Lazy!(&'tcx [(ty::Predicate<'tcx>, Span)])>),
super_predicates: Lazy!(Table<DefIndex, Lazy!(ty::GenericPredicates<'tcx>)>),
mir: Lazy!(Table<DefIndex, Lazy!(mir::Body<'tcx>)>),
promoted_mir: Lazy!(Table<DefIndex, Lazy!(IndexVec<mir::Promoted, mir::Body<'tcx>>)>),

#[derive(Copy, Clone, RustcEncodable, RustcDecodable)]
@@ -1,6 +1,6 @@
use crate::rmeta::*;

use rustc::hir::def_id::{DefId, DefIndex};
use rustc_index::vec::Idx;
use rustc_serialize::{Encodable, opaque::Encoder};
use std::convert::TryInto;
use std::marker::PhantomData;
@@ -125,14 +125,16 @@ impl<T: Encodable> FixedSizeEncoding for Option<Lazy<[T]>> {
// FIXME(eddyb) replace `Vec` with `[_]` here, such that `Box<Table<T>>` would be used
// when building it, and `Lazy<Table<T>>` or `&Table<T>` when reading it.
// (not sure if that is possible given that the `Vec` is being resized now)
pub(super) struct Table<T> where Option<T>: FixedSizeEncoding {
// FIXME(eddyb) store `[u8; <Option<T>>::BYTE_LEN]` instead of `u8` in `Vec`,
// once that starts being allowed by the compiler (i.e. lazy normalization).
pub(super) struct Table<I: Idx, T> where Option<T>: FixedSizeEncoding {
// FIXME(eddyb) use `IndexVec<I, [u8; <Option<T>>::BYTE_LEN]>` instead of
// `Vec<u8>`, once that starts working (i.e. lazy normalization).
// Then again, that has the downside of not allowing `Table::encode` to
// obtain a `&[u8]` entirely in safe code, for writing the bytes out.
bytes: Vec<u8>,
_marker: PhantomData<T>,
_marker: PhantomData<(fn(&I), T)>,

impl<T> Default for Table<T> where Option<T>: FixedSizeEncoding {
impl<I: Idx, T> Default for Table<I, T> where Option<T>: FixedSizeEncoding {
fn default() -> Self {
Table {
bytes: vec![],
@@ -141,13 +143,14 @@ impl<T> Default for Table<T> where Option<T>: FixedSizeEncoding {

impl<T> Table<T> where Option<T>: FixedSizeEncoding {
fn set(&mut self, i: usize, value: T) {
impl<I: Idx, T> Table<I, T> where Option<T>: FixedSizeEncoding {
pub(super) fn set(&mut self, i: I, value: T) {
// FIXME(eddyb) investigate more compact encodings for sparse tables.
// On the PR @michaelwoerister mentioned:
// > Space requirements could perhaps be optimized by using the HAMT `popcnt`
// > trick (i.e. divide things into buckets of 32 or 64 items and then
// > store bit-masks of which item in each bucket is actually serialized).
let i = i.index();
let needed = (i + 1) * <Option<T>>::BYTE_LEN;
if self.bytes.len() < needed {
self.bytes.resize(needed, 0);
@@ -156,7 +159,7 @@ impl<T> Table<T> where Option<T>: FixedSizeEncoding {
Some(value).write_to_bytes_at(&mut self.bytes, i);

fn encode(&self, buf: &mut Encoder) -> Lazy<Self> {
pub(super) fn encode(&self, buf: &mut Encoder) -> Lazy<Self> {
let pos = buf.position();
@@ -166,73 +169,26 @@ impl<T> Table<T> where Option<T>: FixedSizeEncoding {

impl<T> LazyMeta for Table<T> where Option<T>: FixedSizeEncoding {
impl<I: Idx, T> LazyMeta for Table<I, T> where Option<T>: FixedSizeEncoding {
type Meta = usize;

fn min_size(len: usize) -> usize {

impl<T> Lazy<Table<T>> where Option<T>: FixedSizeEncoding {
impl<I: Idx, T> Lazy<Table<I, T>> where Option<T>: FixedSizeEncoding {
/// Given the metadata, extract out the value at a particular index (if any).
fn get<'a, 'tcx, M: Metadata<'a, 'tcx>>(
pub(super) fn get<'a, 'tcx, M: Metadata<'a, 'tcx>>(
metadata: M,
i: usize,
i: I,
) -> Option<T> {
debug!("Table::lookup: index={:?} len={:?}", i, self.meta);

let start = self.position.get();
let bytes = &metadata.raw_bytes()[start..start + self.meta];
<Option<T>>::maybe_read_from_bytes_at(bytes, i)?

/// Like a `Table` but using `DefIndex` instead of `usize` as keys.
// FIXME(eddyb) replace by making `Table` behave like `IndexVec`,
// and by using `newtype_index!` to define `DefIndex`.
pub(super) struct PerDefTable<T>(Table<T>) where Option<T>: FixedSizeEncoding;

impl<T> Default for PerDefTable<T> where Option<T>: FixedSizeEncoding {
fn default() -> Self {

impl<T> PerDefTable<T> where Option<T>: FixedSizeEncoding {
pub(super) fn set(&mut self, def_id: DefId, value: T) {
self.0.set(def_id.index.index(), value);

pub(super) fn encode(&self, buf: &mut Encoder) -> Lazy<Self> {
let lazy = self.0.encode(buf);
Lazy::from_position_and_meta(lazy.position, lazy.meta)

impl<T> LazyMeta for PerDefTable<T> where Option<T>: FixedSizeEncoding {
type Meta = <Table<T> as LazyMeta>::Meta;

fn min_size(meta: Self::Meta) -> usize {

impl<T> Lazy<PerDefTable<T>> where Option<T>: FixedSizeEncoding {
fn as_table(&self) -> Lazy<Table<T>> {
Lazy::from_position_and_meta(self.position, self.meta)

/// Given the metadata, extract out the value at a particular DefIndex (if any).
pub(super) fn get<'a, 'tcx, M: Metadata<'a, 'tcx>>(
metadata: M,
def_index: DefIndex,
) -> Option<T> {
self.as_table().get(metadata, def_index.index())
<Option<T>>::maybe_read_from_bytes_at(bytes, i.index())?

0 comments on commit 67421bd

Please sign in to comment.
You can’t perform that action at this time.