@@ -34,14 +34,11 @@ use super::prev::PreviousDepGraph;
3434pub struct DepGraph {
3535 data : Option < Rc < DepGraphData > > ,
3636
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 > > >
4542}
4643
4744
@@ -97,6 +94,11 @@ struct DepGraphData {
9794impl DepGraph {
9895
9996 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 ) ;
100102 DepGraph {
101103 data : Some ( Rc :: new ( DepGraphData {
102104 previous_work_products : RefCell :: new ( FxHashMap ( ) ) ,
@@ -107,14 +109,14 @@ impl DepGraph {
107109 colors : RefCell :: new ( FxHashMap ( ) ) ,
108110 loaded_from_cache : RefCell :: new ( FxHashMap ( ) ) ,
109111 } ) ) ,
110- fingerprints : Rc :: new ( RefCell :: new ( FxHashMap ( ) ) ) ,
112+ fingerprints : Rc :: new ( RefCell :: new ( fingerprints ) ) ,
111113 }
112114 }
113115
114116 pub fn new_disabled ( ) -> DepGraph {
115117 DepGraph {
116118 data : None ,
117- fingerprints : Rc :: new ( RefCell :: new ( FxHashMap ( ) ) ) ,
119+ fingerprints : Rc :: new ( RefCell :: new ( IndexVec :: new ( ) ) ) ,
118120 }
119121 }
120122
@@ -231,12 +233,16 @@ impl DepGraph {
231233
232234 // Store the current fingerprint
233235 {
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 ,
238243 "DepGraph::with_task() - Duplicate fingerprint \
239244 insertion for {:?}", key) ;
245+ fingerprints[ dep_node_index] = current_fingerprint;
240246 }
241247
242248 // Determine the color of the new DepNode.
@@ -262,13 +268,15 @@ impl DepGraph {
262268 let result = task ( cx, arg) ;
263269 let mut stable_hasher = StableHasher :: new ( ) ;
264270 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)
272280 } else {
273281 ( task ( cx, arg) , DepNodeIndex :: INVALID )
274282 }
@@ -328,11 +336,29 @@ impl DepGraph {
328336 }
329337
330338 #[ 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) {
333354 Some ( & fingerprint) => fingerprint,
334355 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+ }
336362 }
337363 }
338364 }
@@ -420,14 +446,17 @@ impl DepGraph {
420446 }
421447
422448 pub fn serialize ( & self ) -> SerializedDepGraph {
423- let fingerprints = self . fingerprints . borrow ( ) ;
449+ let mut fingerprints = self . fingerprints . borrow_mut ( ) ;
424450 let current_dep_graph = self . data . as_ref ( ) . unwrap ( ) . current . borrow ( ) ;
425451
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] )
431460 } ) . collect ( ) ;
432461
433462 let total_edge_count: usize = current_dep_graph. edges . iter ( )
@@ -610,13 +639,20 @@ impl DepGraph {
610639
611640 // ... copying the fingerprint from the previous graph too, so we don't
612641 // 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+ }
620656
621657 // ... emitting any stored diagnostic ...
622658 {
0 commit comments