Skip to content

Commit ee42979

Browse files
committed
rustc_metadata: use a separate TableBuilder type to build a Table.
1 parent 67421bd commit ee42979

File tree

2 files changed

+39
-33
lines changed

2 files changed

+39
-33
lines changed

src/librustc_metadata/rmeta/encoder.rs

+23-23
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use crate::rmeta::*;
2-
use crate::rmeta::table::{FixedSizeEncoding, Table};
2+
use crate::rmeta::table::{FixedSizeEncoding, TableBuilder};
33

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

50-
per_def: PerDefTables<'tcx>,
50+
per_def: PerDefTableBuilders<'tcx>,
5151

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

6363
#[derive(Default)]
64-
struct PerDefTables<'tcx> {
65-
kind: Table<DefIndex, Lazy<EntryKind<'tcx>>>,
66-
visibility: Table<DefIndex, Lazy<ty::Visibility>>,
67-
span: Table<DefIndex, Lazy<Span>>,
68-
attributes: Table<DefIndex, Lazy<[ast::Attribute]>>,
69-
children: Table<DefIndex, Lazy<[DefIndex]>>,
70-
stability: Table<DefIndex, Lazy<attr::Stability>>,
71-
deprecation: Table<DefIndex, Lazy<attr::Deprecation>>,
72-
73-
ty: Table<DefIndex, Lazy<Ty<'tcx>>>,
74-
fn_sig: Table<DefIndex, Lazy<ty::PolyFnSig<'tcx>>>,
75-
impl_trait_ref: Table<DefIndex, Lazy<ty::TraitRef<'tcx>>>,
76-
inherent_impls: Table<DefIndex, Lazy<[DefIndex]>>,
77-
variances: Table<DefIndex, Lazy<[ty::Variance]>>,
78-
generics: Table<DefIndex, Lazy<ty::Generics>>,
79-
explicit_predicates: Table<DefIndex, Lazy<ty::GenericPredicates<'tcx>>>,
80-
inferred_outlives: Table<DefIndex, Lazy<&'tcx [(ty::Predicate<'tcx>, Span)]>>,
81-
super_predicates: Table<DefIndex, Lazy<ty::GenericPredicates<'tcx>>>,
82-
83-
mir: Table<DefIndex, Lazy<mir::Body<'tcx>>>,
84-
promoted_mir: Table<DefIndex, Lazy<IndexVec<mir::Promoted, mir::Body<'tcx>>>>,
64+
struct PerDefTableBuilders<'tcx> {
65+
kind: TableBuilder<DefIndex, Lazy<EntryKind<'tcx>>>,
66+
visibility: TableBuilder<DefIndex, Lazy<ty::Visibility>>,
67+
span: TableBuilder<DefIndex, Lazy<Span>>,
68+
attributes: TableBuilder<DefIndex, Lazy<[ast::Attribute]>>,
69+
children: TableBuilder<DefIndex, Lazy<[DefIndex]>>,
70+
stability: TableBuilder<DefIndex, Lazy<attr::Stability>>,
71+
deprecation: TableBuilder<DefIndex, Lazy<attr::Deprecation>>,
72+
73+
ty: TableBuilder<DefIndex, Lazy<Ty<'tcx>>>,
74+
fn_sig: TableBuilder<DefIndex, Lazy<ty::PolyFnSig<'tcx>>>,
75+
impl_trait_ref: TableBuilder<DefIndex, Lazy<ty::TraitRef<'tcx>>>,
76+
inherent_impls: TableBuilder<DefIndex, Lazy<[DefIndex]>>,
77+
variances: TableBuilder<DefIndex, Lazy<[ty::Variance]>>,
78+
generics: TableBuilder<DefIndex, Lazy<ty::Generics>>,
79+
explicit_predicates: TableBuilder<DefIndex, Lazy<ty::GenericPredicates<'tcx>>>,
80+
inferred_outlives: TableBuilder<DefIndex, Lazy<&'tcx [(ty::Predicate<'tcx>, Span)]>>,
81+
super_predicates: TableBuilder<DefIndex, Lazy<ty::GenericPredicates<'tcx>>>,
82+
83+
mir: TableBuilder<DefIndex, Lazy<mir::Body<'tcx>>>,
84+
promoted_mir: TableBuilder<DefIndex, Lazy<IndexVec<mir::Promoted, mir::Body<'tcx>>>>,
8585
}
8686

8787
macro_rules! encoder_methods {

src/librustc_metadata/rmeta/table.rs

+16-10
Original file line numberDiff line numberDiff line change
@@ -117,33 +117,39 @@ impl<T: Encodable> FixedSizeEncoding for Option<Lazy<[T]>> {
117117
}
118118
}
119119

120-
/// Random-access table (i.e. offeringconstant-time `get`/`set`), similar to
120+
/// Random-access table (i.e. offering constant-time `get`/`set`), similar to
121121
/// `Vec<Option<T>>`, but without requiring encoding or decoding all the values
122122
/// eagerly and in-order.
123123
/// A total of `(max_idx + 1) * <Option<T> as FixedSizeEncoding>::BYTE_LEN` bytes
124-
/// are used for a table, where `max_idx` is the largest index passed to `set`.
125-
// FIXME(eddyb) replace `Vec` with `[_]` here, such that `Box<Table<T>>` would be used
126-
// when building it, and `Lazy<Table<T>>` or `&Table<T>` when reading it.
127-
// (not sure if that is possible given that the `Vec` is being resized now)
124+
/// are used for a table, where `max_idx` is the largest index passed to
125+
/// `TableBuilder::set`.
128126
pub(super) struct Table<I: Idx, T> where Option<T>: FixedSizeEncoding {
127+
_marker: PhantomData<(fn(&I), T)>,
128+
// NOTE(eddyb) this makes `Table` not implement `Sized`, but no
129+
// value of `Table` is ever created (it's always behind `Lazy`).
130+
_bytes: [u8],
131+
}
132+
133+
/// Helper for constructing a table's serialization (also see `Table`).
134+
pub(super) struct TableBuilder<I: Idx, T> where Option<T>: FixedSizeEncoding {
129135
// FIXME(eddyb) use `IndexVec<I, [u8; <Option<T>>::BYTE_LEN]>` instead of
130136
// `Vec<u8>`, once that starts working (i.e. lazy normalization).
131-
// Then again, that has the downside of not allowing `Table::encode` to
137+
// Then again, that has the downside of not allowing `TableBuilder::encode` to
132138
// obtain a `&[u8]` entirely in safe code, for writing the bytes out.
133139
bytes: Vec<u8>,
134140
_marker: PhantomData<(fn(&I), T)>,
135141
}
136142

137-
impl<I: Idx, T> Default for Table<I, T> where Option<T>: FixedSizeEncoding {
143+
impl<I: Idx, T> Default for TableBuilder<I, T> where Option<T>: FixedSizeEncoding {
138144
fn default() -> Self {
139-
Table {
145+
TableBuilder {
140146
bytes: vec![],
141147
_marker: PhantomData,
142148
}
143149
}
144150
}
145151

146-
impl<I: Idx, T> Table<I, T> where Option<T>: FixedSizeEncoding {
152+
impl<I: Idx, T> TableBuilder<I, T> where Option<T>: FixedSizeEncoding {
147153
pub(super) fn set(&mut self, i: I, value: T) {
148154
// FIXME(eddyb) investigate more compact encodings for sparse tables.
149155
// On the PR @michaelwoerister mentioned:
@@ -159,7 +165,7 @@ impl<I: Idx, T> Table<I, T> where Option<T>: FixedSizeEncoding {
159165
Some(value).write_to_bytes_at(&mut self.bytes, i);
160166
}
161167

162-
pub(super) fn encode(&self, buf: &mut Encoder) -> Lazy<Self> {
168+
pub(super) fn encode(&self, buf: &mut Encoder) -> Lazy<Table<I, T>> {
163169
let pos = buf.position();
164170
buf.emit_raw_bytes(&self.bytes);
165171
Lazy::from_position_and_meta(

0 commit comments

Comments
 (0)