@@ -25,12 +25,12 @@ use rustc::hir::def_id::{CrateNum, DefId, DefIndex,
2525use rustc:: ich:: Fingerprint ;
2626use rustc:: middle:: lang_items;
2727use rustc:: mir:: { self , interpret} ;
28+ use rustc:: mir:: interpret:: AllocDecodingSession ;
2829use rustc:: session:: Session ;
2930use rustc:: ty:: { self , Ty , TyCtxt } ;
3031use rustc:: ty:: codec:: TyDecoder ;
3132use rustc:: mir:: Mir ;
3233use rustc:: util:: captures:: Captures ;
33- use rustc:: util:: nodemap:: FxHashMap ;
3434
3535use std:: io;
3636use std:: mem;
@@ -55,11 +55,8 @@ pub struct DecodeContext<'a, 'tcx: 'a> {
5555
5656 lazy_state : LazyState ,
5757
58- // interpreter allocation cache
59- interpret_alloc_cache : FxHashMap < usize , interpret:: AllocId > ,
60-
61- // Read from the LazySeq CrateRoot::inpterpret_alloc_index on demand
62- interpret_alloc_index : Option < Vec < u32 > > ,
58+ // Used for decoding interpret::AllocIds in a cached & thread-safe manner.
59+ alloc_decoding_session : Option < AllocDecodingSession < ' a > > ,
6360}
6461
6562/// Abstract over the various ways one can create metadata decoders.
@@ -78,8 +75,9 @@ pub trait Metadata<'a, 'tcx>: Copy {
7875 tcx,
7976 last_filemap_index : 0 ,
8077 lazy_state : LazyState :: NoNode ,
81- interpret_alloc_cache : FxHashMap :: default ( ) ,
82- interpret_alloc_index : None ,
78+ alloc_decoding_session : self . cdata ( ) . map ( |cdata| {
79+ cdata. alloc_decoding_state . new_decoding_session ( )
80+ } ) ,
8381 }
8482 }
8583}
@@ -178,17 +176,6 @@ impl<'a, 'tcx> DecodeContext<'a, 'tcx> {
178176 self . lazy_state = LazyState :: Previous ( position + min_size) ;
179177 Ok ( position)
180178 }
181-
182- fn interpret_alloc ( & mut self , idx : usize ) -> usize {
183- if let Some ( index) = self . interpret_alloc_index . as_mut ( ) {
184- return index[ idx] as usize ;
185- }
186- let cdata = self . cdata ( ) ;
187- let index: Vec < u32 > = cdata. root . interpret_alloc_index . decode ( cdata) . collect ( ) ;
188- let pos = index[ idx] ;
189- self . interpret_alloc_index = Some ( index) ;
190- pos as usize
191- }
192179}
193180
194181impl < ' a , ' tcx : ' a > TyDecoder < ' a , ' tcx > for DecodeContext < ' a , ' tcx > {
@@ -299,22 +286,11 @@ impl<'a, 'tcx> SpecializedDecoder<LocalDefId> for DecodeContext<'a, 'tcx> {
299286
300287impl < ' a , ' tcx > SpecializedDecoder < interpret:: AllocId > for DecodeContext < ' a , ' tcx > {
301288 fn specialized_decode ( & mut self ) -> Result < interpret:: AllocId , Self :: Error > {
302- let tcx = self . tcx . unwrap ( ) ;
303- let idx = usize:: decode ( self ) ?;
304-
305- if let Some ( cached) = self . interpret_alloc_cache . get ( & idx) . cloned ( ) {
306- return Ok ( cached) ;
289+ if let Some ( alloc_decoding_session) = self . alloc_decoding_session {
290+ alloc_decoding_session. decode_alloc_id ( self )
291+ } else {
292+ bug ! ( "Attempting to decode interpret::AllocId without CrateMetadata" )
307293 }
308- let pos = self . interpret_alloc ( idx) ;
309- self . with_position ( pos, |this| {
310- interpret:: specialized_decode_alloc_id (
311- this,
312- tcx,
313- |this, alloc_id| {
314- assert ! ( this. interpret_alloc_cache. insert( idx, alloc_id) . is_none( ) ) ;
315- } ,
316- )
317- } )
318294 }
319295}
320296
0 commit comments