@@ -95,10 +95,8 @@ crate struct CrateMetadata {
95
95
raw_proc_macros : Option < & ' static [ ProcMacro ] > ,
96
96
/// Source maps for code from the crate.
97
97
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 > ,
102
100
/// Likewise for ExpnHash.
103
101
expn_hash_map : OnceCell < UnhashMap < ExpnHash , ExpnIndex > > ,
104
102
/// Used for decoding interpret::AllocIds in a cached & thread-safe manner.
@@ -320,6 +318,11 @@ impl<'a, 'tcx> DecodeContext<'a, 'tcx> {
320
318
self . lazy_state = LazyState :: Previous ( NonZeroUsize :: new ( position + min_size) . unwrap ( ) ) ;
321
319
Ok ( Lazy :: from_position_and_meta ( NonZeroUsize :: new ( position) . unwrap ( ) , meta) )
322
320
}
321
+
322
+ #[ inline]
323
+ pub fn read_raw_bytes ( & mut self , len : usize ) -> & ' a [ u8 ] {
324
+ self . opaque . read_raw_bytes ( len)
325
+ }
323
326
}
324
327
325
328
impl < ' a , ' tcx > TyDecoder < ' tcx > for DecodeContext < ' a , ' tcx > {
@@ -1596,58 +1599,6 @@ impl<'a, 'tcx> CrateMetadataRef<'a> {
1596
1599
. or_insert_with ( || self . root . tables . def_keys . get ( self , index) . unwrap ( ) . decode ( self ) )
1597
1600
}
1598
1601
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
-
1651
1602
// Returns the path leading to the thing with this `id`.
1652
1603
fn def_path ( & self , id : DefIndex ) -> DefPath {
1653
1604
debug ! ( "def_path(cnum={:?}, id={:?})" , self . cnum, id) ;
@@ -1670,6 +1621,11 @@ impl<'a, 'tcx> CrateMetadataRef<'a> {
1670
1621
self . def_path_hash_unlocked ( index, & mut def_path_hashes)
1671
1622
}
1672
1623
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
+
1673
1629
fn expn_hash_to_expn_id ( & self , index_guess : u32 , hash : ExpnHash ) -> ExpnId {
1674
1630
debug_assert_eq ! ( ExpnId :: from_hash( hash) , None ) ;
1675
1631
let index_guess = ExpnIndex :: from_u32 ( index_guess) ;
@@ -1936,13 +1892,18 @@ impl CrateMetadata {
1936
1892
let alloc_decoding_state =
1937
1893
AllocDecodingState :: new ( root. interpret_alloc_index . decode ( & blob) . collect ( ) ) ;
1938
1894
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
+
1939
1900
CrateMetadata {
1940
1901
blob,
1941
1902
root,
1942
1903
trait_impls,
1943
1904
raw_proc_macros,
1944
1905
source_map_import_info : OnceCell :: new ( ) ,
1945
- def_path_hash_map : Default :: default ( ) ,
1906
+ def_path_hash_map,
1946
1907
expn_hash_map : Default :: default ( ) ,
1947
1908
alloc_decoding_state,
1948
1909
cnum,
0 commit comments