Skip to content
Browse files

rustc_metadata: use a separate TableBuilder type to build a Table.

  • Loading branch information
eddyb committed Nov 14, 2019
1 parent 67421bd commit ee42979eeb4b13bfb71116f330c735bc48fb38bc
Showing with 39 additions and 33 deletions.
  1. +23 −23 src/librustc_metadata/rmeta/
  2. +16 −10 src/librustc_metadata/rmeta/
@@ -1,5 +1,5 @@
use crate::rmeta::*;
use crate::rmeta::table::{FixedSizeEncoding, Table};
use crate::rmeta::table::{FixedSizeEncoding, TableBuilder};

use rustc::middle::cstore::{LinkagePreference, NativeLibrary,
EncodedMetadata, ForeignModule};
@@ -47,7 +47,7 @@ struct EncodeContext<'tcx> {
opaque: opaque::Encoder,
tcx: TyCtxt<'tcx>,

per_def: PerDefTables<'tcx>,
per_def: PerDefTableBuilders<'tcx>,

lazy_state: LazyState,
type_shorthands: FxHashMap<Ty<'tcx>, usize>,
@@ -61,27 +61,27 @@ struct EncodeContext<'tcx> {

struct PerDefTables<'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>>>>,
struct PerDefTableBuilders<'tcx> {
kind: TableBuilder<DefIndex, Lazy<EntryKind<'tcx>>>,
visibility: TableBuilder<DefIndex, Lazy<ty::Visibility>>,
span: TableBuilder<DefIndex, Lazy<Span>>,
attributes: TableBuilder<DefIndex, Lazy<[ast::Attribute]>>,
children: TableBuilder<DefIndex, Lazy<[DefIndex]>>,
stability: TableBuilder<DefIndex, Lazy<attr::Stability>>,
deprecation: TableBuilder<DefIndex, Lazy<attr::Deprecation>>,

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

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

macro_rules! encoder_methods {
@@ -117,33 +117,39 @@ impl<T: Encodable> FixedSizeEncoding for Option<Lazy<[T]>> {

/// Random-access table (i.e. offeringconstant-time `get`/`set`), similar to
/// Random-access table (i.e. offering constant-time `get`/`set`), similar to
/// `Vec<Option<T>>`, but without requiring encoding or decoding all the values
/// eagerly and in-order.
/// A total of `(max_idx + 1) * <Option<T> as FixedSizeEncoding>::BYTE_LEN` bytes
/// are used for a table, where `max_idx` is the largest index passed to `set`.
// 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)
/// are used for a table, where `max_idx` is the largest index passed to
/// `TableBuilder::set`.
pub(super) struct Table<I: Idx, T> where Option<T>: FixedSizeEncoding {
_marker: PhantomData<(fn(&I), T)>,
// NOTE(eddyb) this makes `Table` not implement `Sized`, but no
// value of `Table` is ever created (it's always behind `Lazy`).
_bytes: [u8],

/// Helper for constructing a table's serialization (also see `Table`).
pub(super) struct TableBuilder<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
// Then again, that has the downside of not allowing `TableBuilder::encode` to
// obtain a `&[u8]` entirely in safe code, for writing the bytes out.
bytes: Vec<u8>,
_marker: PhantomData<(fn(&I), T)>,

impl<I: Idx, T> Default for Table<I, T> where Option<T>: FixedSizeEncoding {
impl<I: Idx, T> Default for TableBuilder<I, T> where Option<T>: FixedSizeEncoding {
fn default() -> Self {
Table {
TableBuilder {
bytes: vec![],
_marker: PhantomData,

impl<I: Idx, T> Table<I, T> where Option<T>: FixedSizeEncoding {
impl<I: Idx, T> TableBuilder<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:
@@ -159,7 +165,7 @@ impl<I: Idx, T> Table<I, T> where Option<T>: FixedSizeEncoding {
Some(value).write_to_bytes_at(&mut self.bytes, i);

pub(super) fn encode(&self, buf: &mut Encoder) -> Lazy<Self> {
pub(super) fn encode(&self, buf: &mut Encoder) -> Lazy<Table<I, T>> {
let pos = buf.position();

0 comments on commit ee42979

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