14
14
15
15
use super :: query:: DepGraphQuery ;
16
16
use super :: { DepKind , DepNode , DepNodeIndex } ;
17
+ use hashbrown:: raw:: RawTable ;
17
18
use rustc_data_structures:: fingerprint:: Fingerprint ;
18
- use rustc_data_structures:: fx:: FxHashMap ;
19
+ use rustc_data_structures:: fx:: { FxHashMap , FxHasher } ;
19
20
use rustc_data_structures:: memmap:: Mmap ;
20
21
use rustc_data_structures:: owning_ref:: OwningRef ;
21
22
use rustc_data_structures:: profiling:: SelfProfilerRef ;
@@ -25,6 +26,7 @@ use rustc_serialize::opaque::{FileEncodeResult, FileEncoder, IntEncodedWithFixed
25
26
use rustc_serialize:: { Decodable , Encodable } ;
26
27
use smallvec:: SmallVec ;
27
28
use std:: convert:: TryInto ;
29
+ use std:: hash:: { Hash , Hasher } ;
28
30
29
31
// The maximum value of `SerializedDepNodeIndex` leaves the upper two bits
30
32
// unused so that we can store multiple index types in `CompressedHybridIndex`,
@@ -69,7 +71,13 @@ pub struct SerializedDepGraph<K: DepKind> {
69
71
/// The set of all DepNodes in the graph and their position in the mmap.
70
72
nodes : Option < OwningRef < Mmap , [ ( DepNode < K > , u32 ) ] > > ,
71
73
/// 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 ( )
73
81
}
74
82
75
83
impl < K : DepKind > Default for SerializedDepGraph < K > {
@@ -91,7 +99,10 @@ impl<K: DepKind> SerializedDepGraph<K> {
91
99
92
100
#[ inline]
93
101
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) )
95
106
}
96
107
97
108
#[ inline]
@@ -112,7 +123,7 @@ impl<K: DepKind> SerializedDepGraph<K> {
112
123
113
124
#[ inline]
114
125
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) ?;
116
127
Some ( self . fingerprint_by_index ( index) )
117
128
}
118
129
@@ -131,7 +142,7 @@ impl<K: DepKind> SerializedDepGraph<K> {
131
142
}
132
143
133
144
pub fn node_count ( & self ) -> usize {
134
- self . index . len ( )
145
+ if let Some ( ref nodes ) = self . nodes { nodes . len ( ) } else { 0 }
135
146
}
136
147
137
148
#[ instrument( level = "debug" , skip( mmap) ) ]
@@ -152,11 +163,14 @@ impl<K: DepKind> SerializedDepGraph<K> {
152
163
unsafe { d. mmap_slice_at :: < ( DepNode < K > , u32 ) > ( nodes_position, node_count) }
153
164
} ) ;
154
165
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
+ }
160
174
161
175
SerializedDepGraph { nodes : Some ( nodes) , index }
162
176
}
0 commit comments