Skip to content

Commit a173ddc

Browse files
committed
rustc_metadata: don't use more space than needed, for each Table.
1 parent 1ab0e24 commit a173ddc

File tree

2 files changed

+22
-29
lines changed

2 files changed

+22
-29
lines changed

src/librustc_metadata/encoder.rs

+2-19
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ pub struct EncodeContext<'a, 'tcx: 'a> {
5858
source_file_cache: Lrc<SourceFile>,
5959
}
6060

61+
#[derive(Default)]
6162
struct PerDefTables<'tcx> {
6263
kind: PerDefTable<Lazy<EntryKind<'tcx>>>,
6364
visibility: PerDefTable<Lazy<ty::Visibility>>,
@@ -1830,28 +1831,10 @@ pub fn encode_metadata<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>)
18301831
// Since encoding metadata is not in a query, and nothing is cached,
18311832
// there's no need to do dep-graph tracking for any of it.
18321833
let (root, mut result) = tcx.dep_graph.with_ignore(move || {
1833-
let def_counts = tcx.hir().definitions().def_index_counts_lo_hi();
18341834
let mut ecx = EncodeContext {
18351835
opaque: encoder,
18361836
tcx,
1837-
per_def: PerDefTables {
1838-
kind: PerDefTable::new(def_counts),
1839-
visibility: PerDefTable::new(def_counts),
1840-
span: PerDefTable::new(def_counts),
1841-
attributes: PerDefTable::new(def_counts),
1842-
children: PerDefTable::new(def_counts),
1843-
stability: PerDefTable::new(def_counts),
1844-
deprecation: PerDefTable::new(def_counts),
1845-
1846-
ty: PerDefTable::new(def_counts),
1847-
inherent_impls: PerDefTable::new(def_counts),
1848-
variances: PerDefTable::new(def_counts),
1849-
generics: PerDefTable::new(def_counts),
1850-
predicates: PerDefTable::new(def_counts),
1851-
predicates_defined_on: PerDefTable::new(def_counts),
1852-
1853-
mir: PerDefTable::new(def_counts),
1854-
},
1837+
per_def: Default::default(),
18551838
lazy_state: LazyState::NoNode,
18561839
type_shorthands: Default::default(),
18571840
predicate_shorthands: Default::default(),

src/librustc_metadata/table.rs

+20-10
Original file line numberDiff line numberDiff line change
@@ -88,17 +88,23 @@ pub struct Table<T> where Option<T>: FixedSizeEncoding {
8888
_marker: PhantomData<T>,
8989
}
9090

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

100+
impl<T> Table<T> where Option<T>: FixedSizeEncoding {
100101
pub fn set(&mut self, i: usize, value: T) {
101-
Some(value).write_to_bytes(&mut self.bytes[i * <Option<T>>::BYTE_LEN..]);
102+
let start = i * <Option<T>>::BYTE_LEN;
103+
let end = start + <Option<T>>::BYTE_LEN;
104+
if self.bytes.len() < end {
105+
self.bytes.resize(end, 0);
106+
}
107+
Some(value).write_to_bytes(&mut self.bytes[start..end]);
102108
}
103109

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

132138
let bytes = &metadata.raw_bytes()[self.position.get()..][..self.meta];
133-
<Option<T>>::from_bytes(&bytes[i * <Option<T>>::BYTE_LEN..])
139+
let start = i * <Option<T>>::BYTE_LEN;
140+
let end = start + <Option<T>>::BYTE_LEN;
141+
<Option<T>>::from_bytes(bytes.get(start..end)?)
134142
}
135143
}
136144

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

144-
impl<T> PerDefTable<T> where Option<T>: FixedSizeEncoding {
145-
pub fn new((max_index_lo, max_index_hi): (usize, usize)) -> Self {
152+
impl<T> Default for PerDefTable<T> where Option<T>: FixedSizeEncoding {
153+
fn default() -> Self {
146154
PerDefTable {
147-
lo: Table::new(max_index_lo),
148-
hi: Table::new(max_index_hi),
155+
lo: Table::default(),
156+
hi: Table::default(),
149157
}
150158
}
159+
}
151160

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

0 commit comments

Comments
 (0)