diff --git a/src/librustc_metadata/decoder.rs b/src/librustc_metadata/decoder.rs index 9173d128274..c69ee180dc9 100644 --- a/src/librustc_metadata/decoder.rs +++ b/src/librustc_metadata/decoder.rs @@ -59,6 +59,9 @@ pub struct DecodeContext<'a, 'tcx: 'a> { // interpreter allocation cache interpret_alloc_cache: FxHashMap, + + // Read from the LazySeq CrateRoot::inpterpret_alloc_index on demand + interpret_alloc_index: Option>, } /// 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 = 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 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, diff --git a/src/librustc_metadata/encoder.rs b/src/librustc_metadata/encoder.rs index cc2d0eab233..d6c673f550d 100644 --- a/src/librustc_metadata/encoder.rs +++ b/src/librustc_metadata/encoder.rs @@ -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(); diff --git a/src/librustc_metadata/schema.rs b/src/librustc_metadata/schema.rs index 4eaf08742ec..23ea5e4cc55 100644 --- a/src/librustc_metadata/schema.rs +++ b/src/librustc_metadata/schema.rs @@ -207,7 +207,7 @@ pub struct CrateRoot { pub impls: LazySeq, pub exported_symbols: EncodedExportedSymbols, pub wasm_custom_sections: LazySeq, - pub interpret_alloc_index: Vec, + pub interpret_alloc_index: LazySeq, pub index: LazySeq, }