@@ -18,7 +18,7 @@ use hir;
1818use hir:: def_id:: { CrateNum , DefId , DefIndex , LOCAL_CRATE , DefIndexAddressSpace ,
1919 CRATE_DEF_INDEX } ;
2020use ich:: Fingerprint ;
21- use rustc_data_structures:: fx:: FxHashMap ;
21+ use rustc_data_structures:: fx:: { FxHashMap , FxHashSet } ;
2222use rustc_data_structures:: indexed_vec:: IndexVec ;
2323use rustc_data_structures:: stable_hasher:: StableHasher ;
2424use serialize:: { Encodable , Decodable , Encoder , Decoder } ;
@@ -36,7 +36,6 @@ use util::nodemap::NodeMap;
3636/// There is one DefPathTable for each crate.
3737pub struct DefPathTable {
3838 index_to_key : [ Vec < DefKey > ; 2 ] ,
39- key_to_index : FxHashMap < DefKey , DefIndex > ,
4039 def_path_hashes : [ Vec < DefPathHash > ; 2 ] ,
4140}
4241
@@ -47,7 +46,6 @@ impl Clone for DefPathTable {
4746 DefPathTable {
4847 index_to_key : [ self . index_to_key [ 0 ] . clone ( ) ,
4948 self . index_to_key [ 1 ] . clone ( ) ] ,
50- key_to_index : self . key_to_index . clone ( ) ,
5149 def_path_hashes : [ self . def_path_hashes [ 0 ] . clone ( ) ,
5250 self . def_path_hashes [ 1 ] . clone ( ) ] ,
5351 }
@@ -65,10 +63,9 @@ impl DefPathTable {
6563 let index_to_key = & mut self . index_to_key [ address_space. index ( ) ] ;
6664 let index = DefIndex :: new ( index_to_key. len ( ) + address_space. start ( ) ) ;
6765 debug ! ( "DefPathTable::insert() - {:?} <-> {:?}" , key, index) ;
68- index_to_key. push ( key. clone ( ) ) ;
66+ index_to_key. push ( key) ;
6967 index
7068 } ;
71- self . key_to_index . insert ( key, index) ;
7269 self . def_path_hashes [ address_space. index ( ) ] . push ( def_path_hash) ;
7370 debug_assert ! ( self . def_path_hashes[ address_space. index( ) ] . len( ) ==
7471 self . index_to_key[ address_space. index( ) ] . len( ) ) ;
@@ -87,47 +84,6 @@ impl DefPathTable {
8784 [ index. as_array_index ( ) ]
8885 }
8986
90- #[ inline( always) ]
91- pub fn def_index_for_def_key ( & self , key : & DefKey ) -> Option < DefIndex > {
92- self . key_to_index . get ( key) . cloned ( )
93- }
94-
95- #[ inline( always) ]
96- pub fn contains_key ( & self , key : & DefKey ) -> bool {
97- self . key_to_index . contains_key ( key)
98- }
99-
100- pub fn retrace_path ( & self ,
101- path_data : & [ DisambiguatedDefPathData ] )
102- -> Option < DefIndex > {
103- let root_key = DefKey {
104- parent : None ,
105- disambiguated_data : DisambiguatedDefPathData {
106- data : DefPathData :: CrateRoot ,
107- disambiguator : 0 ,
108- } ,
109- } ;
110-
111- let root_index = self . key_to_index
112- . get ( & root_key)
113- . expect ( "no root key?" )
114- . clone ( ) ;
115-
116- debug ! ( "retrace_path: root_index={:?}" , root_index) ;
117-
118- let mut index = root_index;
119- for data in path_data {
120- let key = DefKey { parent : Some ( index) , disambiguated_data : data. clone ( ) } ;
121- debug ! ( "retrace_path: key={:?}" , key) ;
122- match self . key_to_index . get ( & key) {
123- Some ( & i) => index = i,
124- None => return None ,
125- }
126- }
127-
128- Some ( index)
129- }
130-
13187 pub fn add_def_path_hashes_to ( & self ,
13288 cnum : CrateNum ,
13389 out : & mut FxHashMap < DefPathHash , DefId > ) {
@@ -149,7 +105,7 @@ impl DefPathTable {
149105 }
150106
151107 pub fn size ( & self ) -> usize {
152- self . key_to_index . len ( )
108+ self . index_to_key . iter ( ) . map ( |v| v . len ( ) ) . sum ( )
153109 }
154110}
155111
@@ -179,19 +135,8 @@ impl Decodable for DefPathTable {
179135 let index_to_key = [ index_to_key_lo, index_to_key_hi] ;
180136 let def_path_hashes = [ def_path_hashes_lo, def_path_hashes_hi] ;
181137
182- let mut key_to_index = FxHashMap ( ) ;
183-
184- for space in & [ DefIndexAddressSpace :: Low , DefIndexAddressSpace :: High ] {
185- key_to_index. extend ( index_to_key[ space. index ( ) ]
186- . iter ( )
187- . enumerate ( )
188- . map ( |( index, key) | ( key. clone ( ) ,
189- DefIndex :: new ( index + space. start ( ) ) ) ) )
190- }
191-
192138 Ok ( DefPathTable {
193139 index_to_key,
194- key_to_index,
195140 def_path_hashes,
196141 } )
197142 }
@@ -208,6 +153,7 @@ pub struct Definitions {
208153 pub ( super ) node_to_hir_id : IndexVec < ast:: NodeId , hir:: HirId > ,
209154 macro_def_scopes : FxHashMap < Mark , DefId > ,
210155 expansions : FxHashMap < DefIndex , Mark > ,
156+ keys_created : FxHashSet < DefKey > ,
211157}
212158
213159// Unfortunately we have to provide a manual impl of Clone because of the
@@ -224,6 +170,7 @@ impl Clone for Definitions {
224170 node_to_hir_id : self . node_to_hir_id . clone ( ) ,
225171 macro_def_scopes : self . macro_def_scopes . clone ( ) ,
226172 expansions : self . expansions . clone ( ) ,
173+ keys_created : self . keys_created . clone ( ) ,
227174 }
228175 }
229176}
@@ -448,14 +395,14 @@ impl Definitions {
448395 Definitions {
449396 table : DefPathTable {
450397 index_to_key : [ vec ! [ ] , vec ! [ ] ] ,
451- key_to_index : FxHashMap ( ) ,
452398 def_path_hashes : [ vec ! [ ] , vec ! [ ] ] ,
453399 } ,
454400 node_to_def_index : NodeMap ( ) ,
455401 def_index_to_node : [ vec ! [ ] , vec ! [ ] ] ,
456402 node_to_hir_id : IndexVec :: new ( ) ,
457403 macro_def_scopes : FxHashMap ( ) ,
458404 expansions : FxHashMap ( ) ,
405+ keys_created : FxHashSet ( ) ,
459406 }
460407 }
461408
@@ -478,10 +425,6 @@ impl Definitions {
478425 self . table . def_path_hash ( index)
479426 }
480427
481- pub fn def_index_for_def_key ( & self , key : DefKey ) -> Option < DefIndex > {
482- self . table . def_index_for_def_key ( & key)
483- }
484-
485428 /// Returns the path from the crate root to `index`. The root
486429 /// nodes are not included in the path (i.e., this will be an
487430 /// empty vector for the crate root). For an inlined item, this
@@ -583,9 +526,10 @@ impl Definitions {
583526 }
584527 } ;
585528
586- while self . table . contains_key ( & key) {
529+ while self . keys_created . contains ( & key) {
587530 key. disambiguated_data . disambiguator += 1 ;
588531 }
532+ self . keys_created . insert ( key. clone ( ) ) ;
589533
590534 let parent_hash = self . table . def_path_hash ( parent) ;
591535 let def_path_hash = key. compute_stable_hash ( parent_hash) ;
@@ -710,6 +654,8 @@ macro_rules! define_global_metadata_kind {
710654 $( $variant) ,*
711655 }
712656
657+ const GLOBAL_MD_ADDRESS_SPACE : DefIndexAddressSpace = DefIndexAddressSpace :: High ;
658+
713659 impl GlobalMetaDataKind {
714660 fn allocate_def_indices( definitions: & mut Definitions ) {
715661 $( {
@@ -718,7 +664,7 @@ macro_rules! define_global_metadata_kind {
718664 CRATE_DEF_INDEX ,
719665 ast:: DUMMY_NODE_ID ,
720666 DefPathData :: GlobalMetaData ( instance. name( ) ) ,
721- DefIndexAddressSpace :: High ,
667+ GLOBAL_MD_ADDRESS_SPACE ,
722668 Mark :: root( )
723669 ) ;
724670
@@ -736,7 +682,14 @@ macro_rules! define_global_metadata_kind {
736682 }
737683 } ;
738684
739- def_path_table. key_to_index[ & def_key]
685+ // These DefKeys are all right after the root,
686+ // so a linear search is fine.
687+ let index = def_path_table. index_to_key[ GLOBAL_MD_ADDRESS_SPACE . index( ) ]
688+ . iter( )
689+ . position( |k| * k == def_key)
690+ . unwrap( ) ;
691+
692+ DefIndex :: from_array_index( index, GLOBAL_MD_ADDRESS_SPACE )
740693 }
741694
742695 fn name( & self ) -> Symbol {
0 commit comments