@@ -16,7 +16,6 @@ use rustc_expand::proc_macro::{AttrProcMacro, BangProcMacro, ProcMacroDerive};
1616use rustc_hir as hir;
1717use rustc_hir:: def:: { CtorKind , CtorOf , DefKind , Res } ;
1818use rustc_hir:: def_id:: { CrateNum , DefId , DefIndex , CRATE_DEF_INDEX , LOCAL_CRATE } ;
19- use rustc_hir:: definitions:: DefPathTable ;
2019use rustc_hir:: definitions:: { DefKey , DefPath , DefPathData , DefPathHash } ;
2120use rustc_hir:: lang_items;
2221use rustc_index:: vec:: { Idx , IndexVec } ;
@@ -29,7 +28,6 @@ use rustc_middle::mir::interpret::{AllocDecodingSession, AllocDecodingState};
2928use rustc_middle:: mir:: { self , Body , Promoted } ;
3029use rustc_middle:: ty:: codec:: TyDecoder ;
3130use rustc_middle:: ty:: { self , Ty , TyCtxt } ;
32- use rustc_middle:: util:: common:: record_time;
3331use rustc_serialize:: { opaque, Decodable , Decoder } ;
3432use rustc_session:: Session ;
3533use rustc_span:: hygiene:: ExpnDataDecodeMode ;
@@ -69,12 +67,6 @@ crate struct CrateMetadata {
6967 /// universal (`for<'tcx>`), that is paired up with whichever `TyCtxt`
7068 /// is being used to decode those values.
7169 root : CrateRoot < ' static > ,
72- /// For each definition in this crate, we encode a key. When the
73- /// crate is loaded, we read all the keys and put them in this
74- /// hashmap, which gives the reverse mapping. This allows us to
75- /// quickly retrace a `DefPath`, which is needed for incremental
76- /// compilation support.
77- def_path_table : DefPathTable ,
7870 /// Trait impl data.
7971 /// FIXME: Used only from queries and can use query cache,
8072 /// so pre-decoding can probably be avoided.
@@ -91,6 +83,10 @@ crate struct CrateMetadata {
9183 /// Do not access the value directly, as it might not have been initialized yet.
9284 /// The field must always be initialized to `DepNodeIndex::INVALID`.
9385 dep_node_index : AtomicCell < DepNodeIndex > ,
86+ /// Caches decoded `DefKey`s.
87+ def_key_cache : Lock < FxHashMap < DefIndex , DefKey > > ,
88+ /// Caches decoded `DefPathHash`es.
89+ def_path_hash_cache : Lock < FxHashMap < DefIndex , DefPathHash > > ,
9490
9591 // --- Other significant crate properties ---
9692 /// ID of this crate, from the current compilation session's point of view.
@@ -807,7 +803,7 @@ impl<'a, 'tcx> CrateMetadataRef<'a> {
807803 data. has_auto_impl ,
808804 data. is_marker ,
809805 data. specialization_kind ,
810- self . def_path_table . def_path_hash ( item_id) ,
806+ self . def_path_hash ( item_id) ,
811807 )
812808 }
813809 EntryKind :: TraitAlias => ty:: TraitDef :: new (
@@ -817,7 +813,7 @@ impl<'a, 'tcx> CrateMetadataRef<'a> {
817813 false ,
818814 false ,
819815 ty:: trait_def:: TraitSpecializationKind :: None ,
820- self . def_path_table . def_path_hash ( item_id) ,
816+ self . def_path_hash ( item_id) ,
821817 ) ,
822818 _ => bug ! ( "def-index does not refer to trait or trait alias" ) ,
823819 }
@@ -1509,12 +1505,14 @@ impl<'a, 'tcx> CrateMetadataRef<'a> {
15091505
15101506 #[ inline]
15111507 fn def_key ( & self , index : DefIndex ) -> DefKey {
1512- let mut key = self . def_path_table . def_key ( index) ;
1513- if self . is_proc_macro ( index) {
1514- let name = self . raw_proc_macro ( index) . name ( ) ;
1515- key. disambiguated_data . data = DefPathData :: MacroNs ( Symbol :: intern ( name) ) ;
1516- }
1517- key
1508+ * self . def_key_cache . lock ( ) . entry ( index) . or_insert_with ( || {
1509+ let mut key = self . root . tables . def_keys . get ( self , index) . unwrap ( ) . decode ( self ) ;
1510+ if self . is_proc_macro ( index) {
1511+ let name = self . raw_proc_macro ( index) . name ( ) ;
1512+ key. disambiguated_data . data = DefPathData :: MacroNs ( Symbol :: intern ( name) ) ;
1513+ }
1514+ key
1515+ } )
15181516 }
15191517
15201518 // Returns the path leading to the thing with this `id`.
@@ -1723,9 +1721,6 @@ impl CrateMetadata {
17231721 private_dep : bool ,
17241722 host_hash : Option < Svh > ,
17251723 ) -> CrateMetadata {
1726- let def_path_table = record_time ( & sess. perf_stats . decode_def_path_tables_time , || {
1727- root. def_path_table . decode ( ( & blob, sess) )
1728- } ) ;
17291724 let trait_impls = root
17301725 . impls
17311726 . decode ( ( & blob, sess) )
@@ -1737,7 +1732,6 @@ impl CrateMetadata {
17371732 CrateMetadata {
17381733 blob,
17391734 root,
1740- def_path_table,
17411735 trait_impls,
17421736 raw_proc_macros,
17431737 source_map_import_info : OnceCell :: new ( ) ,
@@ -1752,6 +1746,8 @@ impl CrateMetadata {
17521746 host_hash,
17531747 extern_crate : Lock :: new ( None ) ,
17541748 hygiene_context : Default :: default ( ) ,
1749+ def_key_cache : Default :: default ( ) ,
1750+ def_path_hash_cache : Default :: default ( ) ,
17551751 }
17561752 }
17571753
@@ -1828,6 +1824,10 @@ impl CrateMetadata {
18281824 self . root . hash
18291825 }
18301826
1827+ fn num_def_ids ( & self ) -> usize {
1828+ self . root . tables . def_keys . size ( )
1829+ }
1830+
18311831 fn local_def_id ( & self , index : DefIndex ) -> DefId {
18321832 DefId { krate : self . cnum , index }
18331833 }
@@ -1843,10 +1843,34 @@ impl CrateMetadata {
18431843
18441844 None
18451845 }
1846+ }
1847+
1848+ impl < ' a , ' tcx > CrateMetadataRef < ' a > {
1849+ fn def_path_hash_unlocked (
1850+ & self ,
1851+ index : DefIndex ,
1852+ def_path_hashes : & mut FxHashMap < DefIndex , DefPathHash > ,
1853+ ) -> DefPathHash {
1854+ * def_path_hashes. entry ( index) . or_insert_with ( || {
1855+ self . root . tables . def_path_hashes . get ( self , index) . unwrap ( ) . decode ( self )
1856+ } )
1857+ }
18461858
18471859 #[ inline]
18481860 fn def_path_hash ( & self , index : DefIndex ) -> DefPathHash {
1849- self . def_path_table . def_path_hash ( index)
1861+ let mut def_path_hashes = self . def_path_hash_cache . lock ( ) ;
1862+ self . def_path_hash_unlocked ( index, & mut def_path_hashes)
1863+ }
1864+
1865+ fn all_def_path_hashes_and_def_ids ( & self ) -> Vec < ( DefPathHash , DefId ) > {
1866+ let mut result = Vec :: new ( ) ;
1867+ let mut def_path_hashes = self . def_path_hash_cache . lock ( ) ;
1868+ for index in 0 ..self . num_def_ids ( ) {
1869+ let index = DefIndex :: from_usize ( index) ;
1870+ let def_path_hash = self . def_path_hash_unlocked ( index, & mut def_path_hashes) ;
1871+ result. push ( ( def_path_hash, self . local_def_id ( index) ) ) ;
1872+ }
1873+ result
18501874 }
18511875
18521876 /// Get the `DepNodeIndex` corresponding this crate. The result of this
0 commit comments