Skip to content

Commit 6f251c2

Browse files
committed
Use LazySeq instead of Vec
1 parent 62c0501 commit 6f251c2

File tree

3 files changed

+40
-23
lines changed

3 files changed

+40
-23
lines changed

src/librustc_metadata/decoder.rs

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,9 @@ pub struct DecodeContext<'a, 'tcx: 'a> {
5959

6060
// interpreter allocation cache
6161
interpret_alloc_cache: FxHashMap<usize, interpret::AllocId>,
62+
63+
// Read from the LazySeq CrateRoot::inpterpret_alloc_index on demand
64+
interpret_alloc_index: Option<Vec<u32>>,
6265
}
6366

6467
/// Abstract over the various ways one can create metadata decoders.
@@ -78,6 +81,7 @@ pub trait Metadata<'a, 'tcx>: Copy {
7881
last_filemap_index: 0,
7982
lazy_state: LazyState::NoNode,
8083
interpret_alloc_cache: FxHashMap::default(),
84+
interpret_alloc_index: None,
8185
}
8286
}
8387
}
@@ -176,6 +180,17 @@ impl<'a, 'tcx> DecodeContext<'a, 'tcx> {
176180
self.lazy_state = LazyState::Previous(position + min_size);
177181
Ok(position)
178182
}
183+
184+
fn interpret_alloc(&mut self, idx: usize) -> usize {
185+
if let Some(index) = self.interpret_alloc_index.as_mut() {
186+
return index[idx] as usize;
187+
}
188+
let index = self.cdata().root.interpret_alloc_index;
189+
let index: Vec<u32> = index.decode(self.cdata()).collect();
190+
let pos = index[idx];
191+
self.interpret_alloc_index = Some(index);
192+
pos as usize
193+
}
179194
}
180195

181196
impl<'a, 'tcx: 'a> TyDecoder<'a, 'tcx> for DecodeContext<'a, 'tcx> {
@@ -292,11 +307,8 @@ impl<'a, 'tcx> SpecializedDecoder<interpret::AllocId> for DecodeContext<'a, 'tcx
292307
if let Some(cached) = self.interpret_alloc_cache.get(&idx).cloned() {
293308
return Ok(cached);
294309
}
295-
let pos = self
296-
.cdata()
297-
.root
298-
.interpret_alloc_index[idx];
299-
self.with_position(pos as usize, |this| {
310+
let pos = self.interpret_alloc(idx);
311+
self.with_position(pos, |this| {
300312
interpret::specialized_decode_alloc_id(
301313
this,
302314
tcx,

src/librustc_metadata/encoder.rs

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,11 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
265265
start - min_end
266266
}
267267
LazyState::Previous(last_min_end) => {
268-
assert!(last_min_end <= position);
268+
assert!(
269+
last_min_end <= position,
270+
"make sure that the calls to `lazy*` \
271+
are in the same order as the metadata fields",
272+
);
269273
position - last_min_end
270274
}
271275
};
@@ -439,21 +443,7 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
439443
IsolatedEncoder::encode_wasm_custom_sections,
440444
&wasm_custom_sections);
441445

442-
// Encode and index the items.
443-
i = self.position();
444-
let items = self.encode_info_for_items();
445-
let item_bytes = self.position() - i;
446-
447-
i = self.position();
448-
let index = items.write_index(&mut self.opaque.cursor);
449-
let index_bytes = self.position() - i;
450-
451446
let tcx = self.tcx;
452-
let link_meta = self.link_meta;
453-
let is_proc_macro = tcx.sess.crate_types.borrow().contains(&CrateTypeProcMacro);
454-
let has_default_lib_allocator =
455-
attr::contains_name(tcx.hir.krate_attrs(), "default_lib_allocator");
456-
let has_global_allocator = *tcx.sess.has_global_allocator.get();
457447

458448
// Encode the allocation index
459449
let interpret_alloc_index = {
@@ -478,9 +468,24 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
478468
}
479469
n = new_n;
480470
}
481-
interpret_alloc_index
471+
self.lazy_seq(interpret_alloc_index)
482472
};
483473

474+
// Encode and index the items.
475+
i = self.position();
476+
let items = self.encode_info_for_items();
477+
let item_bytes = self.position() - i;
478+
479+
i = self.position();
480+
let index = items.write_index(&mut self.opaque.cursor);
481+
let index_bytes = self.position() - i;
482+
483+
let link_meta = self.link_meta;
484+
let is_proc_macro = tcx.sess.crate_types.borrow().contains(&CrateTypeProcMacro);
485+
let has_default_lib_allocator =
486+
attr::contains_name(tcx.hir.krate_attrs(), "default_lib_allocator");
487+
let has_global_allocator = tcx.sess.has_global_allocator.get();
488+
484489
let root = self.lazy(&CrateRoot {
485490
name: tcx.crate_name(LOCAL_CRATE),
486491
extra_filename: tcx.sess.opts.cg.extra_filename.clone(),
@@ -512,8 +517,8 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
512517
impls,
513518
exported_symbols,
514519
wasm_custom_sections,
515-
index,
516520
interpret_alloc_index,
521+
index,
517522
});
518523

519524
let total_bytes = self.position();

src/librustc_metadata/schema.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ pub struct CrateRoot {
207207
pub impls: LazySeq<TraitImpls>,
208208
pub exported_symbols: EncodedExportedSymbols,
209209
pub wasm_custom_sections: LazySeq<DefIndex>,
210-
pub interpret_alloc_index: Vec<u32>,
210+
pub interpret_alloc_index: LazySeq<u32>,
211211

212212
pub index: LazySeq<index::Index>,
213213
}

0 commit comments

Comments
 (0)