@@ -95,10 +95,8 @@ crate struct CrateMetadata {
9595 raw_proc_macros : Option < & ' static [ ProcMacro ] > ,
9696 /// Source maps for code from the crate.
9797 source_map_import_info : OnceCell < Vec < ImportedSourceFile > > ,
98- /// For every definition in this crate, maps its `DefPathHash` to its
99- /// `DefIndex`. See `raw_def_id_to_def_id` for more details about how
100- /// this is used.
101- def_path_hash_map : OnceCell < UnhashMap < DefPathHash , DefIndex > > ,
98+ /// For every definition in this crate, maps its `DefPathHash` to its `DefIndex`.
99+ def_path_hash_map : DefPathHashMap < ' static > ,
102100 /// Likewise for ExpnHash.
103101 expn_hash_map : OnceCell < UnhashMap < ExpnHash , ExpnIndex > > ,
104102 /// Used for decoding interpret::AllocIds in a cached & thread-safe manner.
@@ -320,6 +318,11 @@ impl<'a, 'tcx> DecodeContext<'a, 'tcx> {
320318 self . lazy_state = LazyState :: Previous ( NonZeroUsize :: new ( position + min_size) . unwrap ( ) ) ;
321319 Ok ( Lazy :: from_position_and_meta ( NonZeroUsize :: new ( position) . unwrap ( ) , meta) )
322320 }
321+
322+ #[ inline]
323+ pub fn read_raw_bytes ( & mut self , len : usize ) -> & ' a [ u8 ] {
324+ self . opaque . read_raw_bytes ( len)
325+ }
323326}
324327
325328impl < ' a , ' tcx > TyDecoder < ' tcx > for DecodeContext < ' a , ' tcx > {
@@ -1596,58 +1599,6 @@ impl<'a, 'tcx> CrateMetadataRef<'a> {
15961599 . or_insert_with ( || self . root . tables . def_keys . get ( self , index) . unwrap ( ) . decode ( self ) )
15971600 }
15981601
1599- /// Finds the corresponding `DefId` for the provided `DefPathHash`, if it exists.
1600- /// This is used by incremental compilation to map a serialized `DefPathHash` to
1601- /// its `DefId` in the current session.
1602- /// Normally, only one 'main' crate will change between incremental compilation sessions:
1603- /// all dependencies will be completely unchanged. In this case, we can avoid
1604- /// decoding every `DefPathHash` in the crate, since the `DefIndex` from the previous
1605- /// session will still be valid. If our 'guess' is wrong (the `DefIndex` no longer exists,
1606- /// or has a different `DefPathHash`, then we need to decode all `DefPathHashes` to determine
1607- /// the correct mapping).
1608- fn def_path_hash_to_def_id (
1609- & self ,
1610- krate : CrateNum ,
1611- index_guess : u32 ,
1612- hash : DefPathHash ,
1613- ) -> Option < DefId > {
1614- let def_index_guess = DefIndex :: from_u32 ( index_guess) ;
1615- let old_hash = self
1616- . root
1617- . tables
1618- . def_path_hashes
1619- . get ( self , def_index_guess)
1620- . map ( |lazy| lazy. decode ( self ) ) ;
1621-
1622- // Fast path: the definition and its index is unchanged from the
1623- // previous compilation session. There is no need to decode anything
1624- // else
1625- if old_hash == Some ( hash) {
1626- return Some ( DefId { krate, index : def_index_guess } ) ;
1627- }
1628-
1629- let is_proc_macro = self . is_proc_macro_crate ( ) ;
1630-
1631- // Slow path: We need to find out the new `DefIndex` of the provided
1632- // `DefPathHash`, if its still exists. This requires decoding every `DefPathHash`
1633- // stored in this crate.
1634- let map = self . cdata . def_path_hash_map . get_or_init ( || {
1635- let end_id = self . root . tables . def_path_hashes . size ( ) as u32 ;
1636- let mut map = UnhashMap :: with_capacity_and_hasher ( end_id as usize , Default :: default ( ) ) ;
1637- for i in 0 ..end_id {
1638- let def_index = DefIndex :: from_u32 ( i) ;
1639- // There may be gaps in the encoded table if we're decoding a proc-macro crate
1640- if let Some ( hash) = self . root . tables . def_path_hashes . get ( self , def_index) {
1641- map. insert ( hash. decode ( self ) , def_index) ;
1642- } else if !is_proc_macro {
1643- panic ! ( "Missing def_path_hashes entry for {:?}" , def_index) ;
1644- }
1645- }
1646- map
1647- } ) ;
1648- map. get ( & hash) . map ( |index| DefId { krate, index : * index } )
1649- }
1650-
16511602 // Returns the path leading to the thing with this `id`.
16521603 fn def_path ( & self , id : DefIndex ) -> DefPath {
16531604 debug ! ( "def_path(cnum={:?}, id={:?})" , self . cnum, id) ;
@@ -1670,6 +1621,11 @@ impl<'a, 'tcx> CrateMetadataRef<'a> {
16701621 self . def_path_hash_unlocked ( index, & mut def_path_hashes)
16711622 }
16721623
1624+ #[ inline]
1625+ fn def_path_hash_to_def_index ( & self , hash : DefPathHash ) -> Option < DefIndex > {
1626+ self . def_path_hash_map . def_path_hash_to_def_index ( & hash)
1627+ }
1628+
16731629 fn expn_hash_to_expn_id ( & self , index_guess : u32 , hash : ExpnHash ) -> ExpnId {
16741630 debug_assert_eq ! ( ExpnId :: from_hash( hash) , None ) ;
16751631 let index_guess = ExpnIndex :: from_u32 ( index_guess) ;
@@ -1936,13 +1892,18 @@ impl CrateMetadata {
19361892 let alloc_decoding_state =
19371893 AllocDecodingState :: new ( root. interpret_alloc_index . decode ( & blob) . collect ( ) ) ;
19381894 let dependencies = Lock :: new ( cnum_map. iter ( ) . cloned ( ) . collect ( ) ) ;
1895+
1896+ // Pre-decode the DefPathHash->DefIndex table. This is a cheap operation
1897+ // that does not copy any data. It just does some data verification.
1898+ let def_path_hash_map = root. def_path_hash_map . decode ( & blob) ;
1899+
19391900 CrateMetadata {
19401901 blob,
19411902 root,
19421903 trait_impls,
19431904 raw_proc_macros,
19441905 source_map_import_info : OnceCell :: new ( ) ,
1945- def_path_hash_map : Default :: default ( ) ,
1906+ def_path_hash_map,
19461907 expn_hash_map : Default :: default ( ) ,
19471908 alloc_decoding_state,
19481909 cnum,
0 commit comments