1414
1515use super :: query:: DepGraphQuery ;
1616use super :: { DepKind , DepNode , DepNodeIndex } ;
17+ use hashbrown:: raw:: RawTable ;
1718use rustc_data_structures:: fingerprint:: Fingerprint ;
18- use rustc_data_structures:: fx:: FxHashMap ;
19+ use rustc_data_structures:: fx:: { FxHashMap , FxHasher } ;
1920use rustc_data_structures:: memmap:: Mmap ;
2021use rustc_data_structures:: owning_ref:: OwningRef ;
2122use rustc_data_structures:: profiling:: SelfProfilerRef ;
@@ -25,6 +26,7 @@ use rustc_serialize::opaque::{FileEncodeResult, FileEncoder, IntEncodedWithFixed
2526use rustc_serialize:: { Decodable , Encodable } ;
2627use smallvec:: SmallVec ;
2728use std:: convert:: TryInto ;
29+ use std:: hash:: { Hash , Hasher } ;
2830
2931// The maximum value of `SerializedDepNodeIndex` leaves the upper two bits
3032// unused so that we can store multiple index types in `CompressedHybridIndex`,
@@ -69,7 +71,13 @@ pub struct SerializedDepGraph<K: DepKind> {
6971 /// The set of all DepNodes in the graph and their position in the mmap.
7072 nodes : Option < OwningRef < Mmap , [ ( DepNode < K > , u32 ) ] > > ,
7173 /// Reciprocal map to `nodes`.
72- index : FxHashMap < DepNode < K > , SerializedDepNodeIndex > ,
74+ index : RawTable < u32 > ,
75+ }
76+
77+ fn hash_node < K : DepKind > ( key : & DepNode < K > ) -> u64 {
78+ let mut h = FxHasher :: default ( ) ;
79+ key. hash ( & mut h) ;
80+ h. finish ( )
7381}
7482
7583impl < K : DepKind > Default for SerializedDepGraph < K > {
@@ -91,7 +99,10 @@ impl<K: DepKind> SerializedDepGraph<K> {
9199
92100 #[ inline]
93101 pub fn node_to_index_opt ( & self , dep_node : & DepNode < K > ) -> Option < SerializedDepNodeIndex > {
94- self . index . get ( dep_node) . cloned ( )
102+ let nodes = self . nodes . as_ref ( ) ?;
103+ let hash = hash_node ( dep_node) ;
104+ let index = self . index . get ( hash, |& i| * dep_node == nodes[ i as usize ] . 0 ) ?;
105+ Some ( SerializedDepNodeIndex :: from_u32 ( * index) )
95106 }
96107
97108 #[ inline]
@@ -112,7 +123,7 @@ impl<K: DepKind> SerializedDepGraph<K> {
112123
113124 #[ inline]
114125 pub fn fingerprint_of ( & self , dep_node : & DepNode < K > ) -> Option < Fingerprint > {
115- let index = self . index . get ( dep_node) . cloned ( ) ?;
126+ let index = self . node_to_index_opt ( dep_node) ?;
116127 Some ( self . fingerprint_by_index ( index) )
117128 }
118129
@@ -131,7 +142,7 @@ impl<K: DepKind> SerializedDepGraph<K> {
131142 }
132143
133144 pub fn node_count ( & self ) -> usize {
134- self . index . len ( )
145+ if let Some ( ref nodes ) = self . nodes { nodes . len ( ) } else { 0 }
135146 }
136147
137148 #[ instrument( level = "debug" , skip( mmap) ) ]
@@ -152,11 +163,14 @@ impl<K: DepKind> SerializedDepGraph<K> {
152163 unsafe { d. mmap_slice_at :: < ( DepNode < K > , u32 ) > ( nodes_position, node_count) }
153164 } ) ;
154165
155- let index: FxHashMap < _ , _ > = nodes
156- . iter ( )
157- . enumerate ( )
158- . map ( |( idx, & ( dep_node, _) ) | ( dep_node, SerializedDepNodeIndex :: from_usize ( idx) ) )
159- . collect ( ) ;
166+ let mut index = RawTable :: with_capacity ( node_count) ;
167+ let mut known_hashes = Vec :: with_capacity ( node_count) ;
168+ for ( idx, ( dep_node, _) ) in nodes. iter ( ) . enumerate ( ) {
169+ let idx = idx. try_into ( ) . unwrap ( ) ;
170+ let hash = hash_node ( dep_node) ;
171+ known_hashes. push ( hash) ;
172+ index. insert ( hash, idx, |& i| known_hashes[ i as usize ] ) ;
173+ }
160174
161175 SerializedDepGraph { nodes : Some ( nodes) , index }
162176 }
0 commit comments