From a173ddc39094cd6d884671cedf1fb6b3aad042cc Mon Sep 17 00:00:00 2001 From: Eduard-Mihai Burtescu Date: Sun, 14 Apr 2019 05:21:39 +0300 Subject: [PATCH] rustc_metadata: don't use more space than needed, for each Table. --- src/librustc_metadata/encoder.rs | 21 ++------------------- src/librustc_metadata/table.rs | 30 ++++++++++++++++++++---------- 2 files changed, 22 insertions(+), 29 deletions(-) diff --git a/src/librustc_metadata/encoder.rs b/src/librustc_metadata/encoder.rs index 1e4fb86f2b39d..41a5bc6e6b341 100644 --- a/src/librustc_metadata/encoder.rs +++ b/src/librustc_metadata/encoder.rs @@ -58,6 +58,7 @@ pub struct EncodeContext<'a, 'tcx: 'a> { source_file_cache: Lrc, } +#[derive(Default)] struct PerDefTables<'tcx> { kind: PerDefTable>>, visibility: PerDefTable>, @@ -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(), diff --git a/src/librustc_metadata/table.rs b/src/librustc_metadata/table.rs index bac6ff7d70ffa..4ba6b56bd0fc5 100644 --- a/src/librustc_metadata/table.rs +++ b/src/librustc_metadata/table.rs @@ -88,17 +88,23 @@ pub struct Table where Option: FixedSizeEncoding { _marker: PhantomData, } -impl Table where Option: FixedSizeEncoding { - pub fn new(len: usize) -> Self { +impl Default for Table where Option: FixedSizeEncoding { + fn default() -> Self { Table { - // FIXME(eddyb) only allocate and encode as many entries as needed. - bytes: vec![0; len * >::BYTE_LEN], + bytes: vec![], _marker: PhantomData, } } +} +impl Table where Option: FixedSizeEncoding { pub fn set(&mut self, i: usize, value: T) { - Some(value).write_to_bytes(&mut self.bytes[i * >::BYTE_LEN..]); + let start = i * >::BYTE_LEN; + let end = start + >::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 { @@ -130,7 +136,9 @@ impl Lazy> where Option: FixedSizeEncoding { debug!("Table::lookup: index={:?} len={:?}", i, self.meta); let bytes = &metadata.raw_bytes()[self.position.get()..][..self.meta]; - >::from_bytes(&bytes[i * >::BYTE_LEN..]) + let start = i * >::BYTE_LEN; + let end = start + >::BYTE_LEN; + >::from_bytes(bytes.get(start..end)?) } } @@ -141,14 +149,16 @@ pub struct PerDefTable where Option: FixedSizeEncoding { hi: Table, } -impl PerDefTable where Option: FixedSizeEncoding { - pub fn new((max_index_lo, max_index_hi): (usize, usize)) -> Self { +impl Default for PerDefTable where Option: FixedSizeEncoding { + fn default() -> Self { PerDefTable { - lo: Table::new(max_index_lo), - hi: Table::new(max_index_hi), + lo: Table::default(), + hi: Table::default(), } } +} +impl PerDefTable where Option: 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();