Use `LazySeq` instead of `Vec`
This commit is contained in:
parent
62c0501be8
commit
6f251c2a03
|
@ -59,6 +59,9 @@ pub struct DecodeContext<'a, 'tcx: 'a> {
|
|||
|
||||
// interpreter allocation cache
|
||||
interpret_alloc_cache: FxHashMap<usize, interpret::AllocId>,
|
||||
|
||||
// Read from the LazySeq CrateRoot::inpterpret_alloc_index on demand
|
||||
interpret_alloc_index: Option<Vec<u32>>,
|
||||
}
|
||||
|
||||
/// Abstract over the various ways one can create metadata decoders.
|
||||
|
@ -78,6 +81,7 @@ pub trait Metadata<'a, 'tcx>: Copy {
|
|||
last_filemap_index: 0,
|
||||
lazy_state: LazyState::NoNode,
|
||||
interpret_alloc_cache: FxHashMap::default(),
|
||||
interpret_alloc_index: None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -176,6 +180,17 @@ impl<'a, 'tcx> DecodeContext<'a, 'tcx> {
|
|||
self.lazy_state = LazyState::Previous(position + min_size);
|
||||
Ok(position)
|
||||
}
|
||||
|
||||
fn interpret_alloc(&mut self, idx: usize) -> usize {
|
||||
if let Some(index) = self.interpret_alloc_index.as_mut() {
|
||||
return index[idx] as usize;
|
||||
}
|
||||
let index = self.cdata().root.interpret_alloc_index;
|
||||
let index: Vec<u32> = index.decode(self.cdata()).collect();
|
||||
let pos = index[idx];
|
||||
self.interpret_alloc_index = Some(index);
|
||||
pos as usize
|
||||
}
|
||||
}
|
||||
|
||||
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
|
|||
if let Some(cached) = self.interpret_alloc_cache.get(&idx).cloned() {
|
||||
return Ok(cached);
|
||||
}
|
||||
let pos = self
|
||||
.cdata()
|
||||
.root
|
||||
.interpret_alloc_index[idx];
|
||||
self.with_position(pos as usize, |this| {
|
||||
let pos = self.interpret_alloc(idx);
|
||||
self.with_position(pos, |this| {
|
||||
interpret::specialized_decode_alloc_id(
|
||||
this,
|
||||
tcx,
|
||||
|
|
|
@ -265,7 +265,11 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
|
|||
start - min_end
|
||||
}
|
||||
LazyState::Previous(last_min_end) => {
|
||||
assert!(last_min_end <= position);
|
||||
assert!(
|
||||
last_min_end <= position,
|
||||
"make sure that the calls to `lazy*` \
|
||||
are in the same order as the metadata fields",
|
||||
);
|
||||
position - last_min_end
|
||||
}
|
||||
};
|
||||
|
@ -439,21 +443,7 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
|
|||
IsolatedEncoder::encode_wasm_custom_sections,
|
||||
&wasm_custom_sections);
|
||||
|
||||
// Encode and index the items.
|
||||
i = self.position();
|
||||
let items = self.encode_info_for_items();
|
||||
let item_bytes = self.position() - i;
|
||||
|
||||
i = self.position();
|
||||
let index = items.write_index(&mut self.opaque.cursor);
|
||||
let index_bytes = self.position() - i;
|
||||
|
||||
let tcx = self.tcx;
|
||||
let link_meta = self.link_meta;
|
||||
let is_proc_macro = tcx.sess.crate_types.borrow().contains(&CrateTypeProcMacro);
|
||||
let has_default_lib_allocator =
|
||||
attr::contains_name(tcx.hir.krate_attrs(), "default_lib_allocator");
|
||||
let has_global_allocator = *tcx.sess.has_global_allocator.get();
|
||||
|
||||
// Encode the allocation index
|
||||
let interpret_alloc_index = {
|
||||
|
@ -478,9 +468,24 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
|
|||
}
|
||||
n = new_n;
|
||||
}
|
||||
interpret_alloc_index
|
||||
self.lazy_seq(interpret_alloc_index)
|
||||
};
|
||||
|
||||
// Encode and index the items.
|
||||
i = self.position();
|
||||
let items = self.encode_info_for_items();
|
||||
let item_bytes = self.position() - i;
|
||||
|
||||
i = self.position();
|
||||
let index = items.write_index(&mut self.opaque.cursor);
|
||||
let index_bytes = self.position() - i;
|
||||
|
||||
let link_meta = self.link_meta;
|
||||
let is_proc_macro = tcx.sess.crate_types.borrow().contains(&CrateTypeProcMacro);
|
||||
let has_default_lib_allocator =
|
||||
attr::contains_name(tcx.hir.krate_attrs(), "default_lib_allocator");
|
||||
let has_global_allocator = tcx.sess.has_global_allocator.get();
|
||||
|
||||
let root = self.lazy(&CrateRoot {
|
||||
name: tcx.crate_name(LOCAL_CRATE),
|
||||
extra_filename: tcx.sess.opts.cg.extra_filename.clone(),
|
||||
|
@ -512,8 +517,8 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
|
|||
impls,
|
||||
exported_symbols,
|
||||
wasm_custom_sections,
|
||||
index,
|
||||
interpret_alloc_index,
|
||||
index,
|
||||
});
|
||||
|
||||
let total_bytes = self.position();
|
||||
|
|
|
@ -207,7 +207,7 @@ pub struct CrateRoot {
|
|||
pub impls: LazySeq<TraitImpls>,
|
||||
pub exported_symbols: EncodedExportedSymbols,
|
||||
pub wasm_custom_sections: LazySeq<DefIndex>,
|
||||
pub interpret_alloc_index: Vec<u32>,
|
||||
pub interpret_alloc_index: LazySeq<u32>,
|
||||
|
||||
pub index: LazySeq<index::Index>,
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue