@@ -34,14 +34,11 @@ use super::prev::PreviousDepGraph;
34
34
pub struct DepGraph {
35
35
data : Option < Rc < DepGraphData > > ,
36
36
37
- // At the moment we are using DepNode as key here. In the future it might
38
- // be possible to use an IndexVec<DepNodeIndex, _> here. At the moment there
39
- // are a few problems with that:
40
- // - Some fingerprints are needed even if incr. comp. is disabled -- yet
41
- // we need to have a dep-graph to generate DepNodeIndices.
42
- // - The architecture is still in flux and it's not clear what how to best
43
- // implement things.
44
- fingerprints : Rc < RefCell < FxHashMap < DepNode , Fingerprint > > >
37
+ // A vector mapping depnodes from the current graph to their associated
38
+ // result value fingerprints. Do not rely on the length of this vector
39
+ // being the same as the number of nodes in the graph. The vector can
40
+ // contain an arbitrary number of zero-entries at the end.
41
+ fingerprints : Rc < RefCell < IndexVec < DepNodeIndex , Fingerprint > > >
45
42
}
46
43
47
44
@@ -97,6 +94,11 @@ struct DepGraphData {
97
94
impl DepGraph {
98
95
99
96
pub fn new ( prev_graph : PreviousDepGraph ) -> DepGraph {
97
+ // Pre-allocate the fingerprints array. We over-allocate a little so
98
+ // that we hopefully don't have to re-allocate during this compilation
99
+ // session.
100
+ let fingerprints = IndexVec :: from_elem_n ( Fingerprint :: ZERO ,
101
+ ( prev_graph. node_count ( ) * 115 ) / 100 ) ;
100
102
DepGraph {
101
103
data : Some ( Rc :: new ( DepGraphData {
102
104
previous_work_products : RefCell :: new ( FxHashMap ( ) ) ,
@@ -107,14 +109,14 @@ impl DepGraph {
107
109
colors : RefCell :: new ( FxHashMap ( ) ) ,
108
110
loaded_from_cache : RefCell :: new ( FxHashMap ( ) ) ,
109
111
} ) ) ,
110
- fingerprints : Rc :: new ( RefCell :: new ( FxHashMap ( ) ) ) ,
112
+ fingerprints : Rc :: new ( RefCell :: new ( fingerprints ) ) ,
111
113
}
112
114
}
113
115
114
116
pub fn new_disabled ( ) -> DepGraph {
115
117
DepGraph {
116
118
data : None ,
117
- fingerprints : Rc :: new ( RefCell :: new ( FxHashMap ( ) ) ) ,
119
+ fingerprints : Rc :: new ( RefCell :: new ( IndexVec :: new ( ) ) ) ,
118
120
}
119
121
}
120
122
@@ -231,12 +233,16 @@ impl DepGraph {
231
233
232
234
// Store the current fingerprint
233
235
{
234
- let old_value = self . fingerprints
235
- . borrow_mut ( )
236
- . insert ( key, current_fingerprint) ;
237
- debug_assert ! ( old_value. is_none( ) ,
236
+ let mut fingerprints = self . fingerprints . borrow_mut ( ) ;
237
+
238
+ if dep_node_index. index ( ) >= fingerprints. len ( ) {
239
+ fingerprints. resize ( dep_node_index. index ( ) + 1 , Fingerprint :: ZERO ) ;
240
+ }
241
+
242
+ debug_assert ! ( fingerprints[ dep_node_index] == Fingerprint :: ZERO ,
238
243
"DepGraph::with_task() - Duplicate fingerprint \
239
244
insertion for {:?}", key) ;
245
+ fingerprints[ dep_node_index] = current_fingerprint;
240
246
}
241
247
242
248
// Determine the color of the new DepNode.
@@ -262,13 +268,15 @@ impl DepGraph {
262
268
let result = task ( cx, arg) ;
263
269
let mut stable_hasher = StableHasher :: new ( ) ;
264
270
result. hash_stable ( & mut hcx, & mut stable_hasher) ;
265
- let old_value = self . fingerprints
266
- . borrow_mut ( )
267
- . insert ( key, stable_hasher. finish ( ) ) ;
268
- debug_assert ! ( old_value. is_none( ) ,
269
- "DepGraph::with_task() - Duplicate fingerprint \
270
- insertion for {:?}", key) ;
271
- ( result, DepNodeIndex :: INVALID )
271
+ let fingerprint = stable_hasher. finish ( ) ;
272
+
273
+ let mut fingerprints = self . fingerprints . borrow_mut ( ) ;
274
+ let dep_node_index = DepNodeIndex :: new ( fingerprints. len ( ) ) ;
275
+ fingerprints. push ( fingerprint) ;
276
+ debug_assert ! ( fingerprints[ dep_node_index] == fingerprint,
277
+ "DepGraph::with_task() - Assigned fingerprint to \
278
+ unexpected index for {:?}", key) ;
279
+ ( result, dep_node_index)
272
280
} else {
273
281
( task ( cx, arg) , DepNodeIndex :: INVALID )
274
282
}
@@ -328,11 +336,29 @@ impl DepGraph {
328
336
}
329
337
330
338
#[ inline]
331
- pub fn fingerprint_of ( & self , dep_node : & DepNode ) -> Fingerprint {
332
- match self . fingerprints . borrow ( ) . get ( dep_node) {
339
+ pub fn dep_node_index_of ( & self , dep_node : & DepNode ) -> DepNodeIndex {
340
+ self . data
341
+ . as_ref ( )
342
+ . unwrap ( )
343
+ . current
344
+ . borrow_mut ( )
345
+ . node_to_node_index
346
+ . get ( dep_node)
347
+ . cloned ( )
348
+ . unwrap ( )
349
+ }
350
+
351
+ #[ inline]
352
+ pub fn fingerprint_of ( & self , dep_node_index : DepNodeIndex ) -> Fingerprint {
353
+ match self . fingerprints . borrow ( ) . get ( dep_node_index) {
333
354
Some ( & fingerprint) => fingerprint,
334
355
None => {
335
- bug ! ( "Could not find current fingerprint for {:?}" , dep_node)
356
+ if let Some ( ref data) = self . data {
357
+ let dep_node = data. current . borrow ( ) . nodes [ dep_node_index] ;
358
+ bug ! ( "Could not find current fingerprint for {:?}" , dep_node)
359
+ } else {
360
+ bug ! ( "Could not find current fingerprint for {:?}" , dep_node_index)
361
+ }
336
362
}
337
363
}
338
364
}
@@ -420,14 +446,17 @@ impl DepGraph {
420
446
}
421
447
422
448
pub fn serialize ( & self ) -> SerializedDepGraph {
423
- let fingerprints = self . fingerprints . borrow ( ) ;
449
+ let mut fingerprints = self . fingerprints . borrow_mut ( ) ;
424
450
let current_dep_graph = self . data . as_ref ( ) . unwrap ( ) . current . borrow ( ) ;
425
451
426
- let nodes: IndexVec < _ , _ > = current_dep_graph. nodes . iter ( ) . map ( |dep_node| {
427
- let fingerprint = fingerprints. get ( dep_node)
428
- . cloned ( )
429
- . unwrap_or ( Fingerprint :: zero ( ) ) ;
430
- ( * dep_node, fingerprint)
452
+ // Make sure we don't run out of bounds below.
453
+ if current_dep_graph. nodes . len ( ) > fingerprints. len ( ) {
454
+ fingerprints. resize ( current_dep_graph. nodes . len ( ) , Fingerprint :: ZERO ) ;
455
+ }
456
+
457
+ let nodes: IndexVec < _ , ( DepNode , Fingerprint ) > =
458
+ current_dep_graph. nodes . iter_enumerated ( ) . map ( |( idx, & dep_node) | {
459
+ ( dep_node, fingerprints[ idx] )
431
460
} ) . collect ( ) ;
432
461
433
462
let total_edge_count: usize = current_dep_graph. edges . iter ( )
@@ -610,13 +639,20 @@ impl DepGraph {
610
639
611
640
// ... copying the fingerprint from the previous graph too, so we don't
612
641
// have to recompute it ...
613
- let fingerprint = data. previous . fingerprint_by_index ( prev_dep_node_index) ;
614
- let old_fingerprint = self . fingerprints
615
- . borrow_mut ( )
616
- . insert ( * dep_node, fingerprint) ;
617
- debug_assert ! ( old_fingerprint. is_none( ) ,
618
- "DepGraph::try_mark_green() - Duplicate fingerprint \
619
- insertion for {:?}", dep_node) ;
642
+ {
643
+ let fingerprint = data. previous . fingerprint_by_index ( prev_dep_node_index) ;
644
+ let mut fingerprints = self . fingerprints . borrow_mut ( ) ;
645
+
646
+ if dep_node_index. index ( ) >= fingerprints. len ( ) {
647
+ fingerprints. resize ( dep_node_index. index ( ) + 1 , Fingerprint :: ZERO ) ;
648
+ }
649
+
650
+ debug_assert ! ( fingerprints[ dep_node_index] == Fingerprint :: ZERO ,
651
+ "DepGraph::try_mark_green() - Duplicate fingerprint \
652
+ insertion for {:?}", dep_node) ;
653
+
654
+ fingerprints[ dep_node_index] = fingerprint;
655
+ }
620
656
621
657
// ... emitting any stored diagnostic ...
622
658
{
0 commit comments