Skip to content

Commit

Permalink
rustc_metadata: don't use more space than needed, for each Table.
Browse files Browse the repository at this point in the history
  • Loading branch information
eddyb committed Apr 14, 2019
1 parent 1ab0e24 commit a173ddc
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 29 deletions.
21 changes: 2 additions & 19 deletions src/librustc_metadata/encoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ pub struct EncodeContext<'a, 'tcx: 'a> {
source_file_cache: Lrc<SourceFile>,
}

#[derive(Default)]
struct PerDefTables<'tcx> {
kind: PerDefTable<Lazy<EntryKind<'tcx>>>,
visibility: PerDefTable<Lazy<ty::Visibility>>,
Expand Down Expand Up @@ -1830,28 +1831,10 @@ pub fn encode_metadata<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>)
// Since encoding metadata is not in a query, and nothing is cached,
// there's no need to do dep-graph tracking for any of it.
let (root, mut result) = tcx.dep_graph.with_ignore(move || {
let def_counts = tcx.hir().definitions().def_index_counts_lo_hi();
let mut ecx = EncodeContext {
opaque: encoder,
tcx,
per_def: PerDefTables {
kind: PerDefTable::new(def_counts),
visibility: PerDefTable::new(def_counts),
span: PerDefTable::new(def_counts),
attributes: PerDefTable::new(def_counts),
children: PerDefTable::new(def_counts),
stability: PerDefTable::new(def_counts),
deprecation: PerDefTable::new(def_counts),

ty: PerDefTable::new(def_counts),
inherent_impls: PerDefTable::new(def_counts),
variances: PerDefTable::new(def_counts),
generics: PerDefTable::new(def_counts),
predicates: PerDefTable::new(def_counts),
predicates_defined_on: PerDefTable::new(def_counts),

mir: PerDefTable::new(def_counts),
},
per_def: Default::default(),
lazy_state: LazyState::NoNode,
type_shorthands: Default::default(),
predicate_shorthands: Default::default(),
Expand Down
30 changes: 20 additions & 10 deletions src/librustc_metadata/table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,17 +88,23 @@ pub struct Table<T> where Option<T>: FixedSizeEncoding {
_marker: PhantomData<T>,
}

impl<T> Table<T> where Option<T>: FixedSizeEncoding {
pub fn new(len: usize) -> Self {
impl<T> Default for Table<T> where Option<T>: FixedSizeEncoding {
fn default() -> Self {
Table {
// FIXME(eddyb) only allocate and encode as many entries as needed.
bytes: vec![0; len * <Option<T>>::BYTE_LEN],
bytes: vec![],
_marker: PhantomData,
}
}
}

impl<T> Table<T> where Option<T>: FixedSizeEncoding {
pub fn set(&mut self, i: usize, value: T) {
Some(value).write_to_bytes(&mut self.bytes[i * <Option<T>>::BYTE_LEN..]);
let start = i * <Option<T>>::BYTE_LEN;
let end = start + <Option<T>>::BYTE_LEN;
if self.bytes.len() < end {
self.bytes.resize(end, 0);
}
Some(value).write_to_bytes(&mut self.bytes[start..end]);
}

pub fn encode(&self, buf: &mut Encoder) -> Lazy<Self> {
Expand Down Expand Up @@ -130,7 +136,9 @@ impl<T> Lazy<Table<T>> where Option<T>: FixedSizeEncoding {
debug!("Table::lookup: index={:?} len={:?}", i, self.meta);

let bytes = &metadata.raw_bytes()[self.position.get()..][..self.meta];
<Option<T>>::from_bytes(&bytes[i * <Option<T>>::BYTE_LEN..])
let start = i * <Option<T>>::BYTE_LEN;
let end = start + <Option<T>>::BYTE_LEN;
<Option<T>>::from_bytes(bytes.get(start..end)?)
}
}

Expand All @@ -141,14 +149,16 @@ pub struct PerDefTable<T> where Option<T>: FixedSizeEncoding {
hi: Table<T>,
}

impl<T> PerDefTable<T> where Option<T>: FixedSizeEncoding {
pub fn new((max_index_lo, max_index_hi): (usize, usize)) -> Self {
impl<T> Default for PerDefTable<T> where Option<T>: FixedSizeEncoding {
fn default() -> Self {
PerDefTable {
lo: Table::new(max_index_lo),
hi: Table::new(max_index_hi),
lo: Table::default(),
hi: Table::default(),
}
}
}

impl<T> PerDefTable<T> where Option<T>: FixedSizeEncoding {
pub fn set(&mut self, def_id: DefId, value: T) {
assert!(def_id.is_local());
let space_index = def_id.index.address_space().index();
Expand Down

0 comments on commit a173ddc

Please sign in to comment.