@@ -254,8 +254,10 @@ impl<'a, K: DepKind + Decodable<MemDecoder<'a>>> Decodable<MemDecoder<'a>>
254254 // end of the array. This padding ensure it doesn't.
255255 edge_list_data. extend ( & [ 0u8 ; DEP_NODE_PAD ] ) ;
256256
257- let mut index: Vec < _ > =
258- iter:: repeat ( FxHashMap :: default ( ) ) . take ( K :: MAX as usize + 1 ) . collect ( ) ;
257+ // Read the number of each dep kind and use it to create an hash map with a suitable size.
258+ let mut index: Vec < _ > = ( 0 ..( K :: MAX as usize + 1 ) )
259+ . map ( |_| FxHashMap :: with_capacity_and_hasher ( d. read_u32 ( ) as usize , Default :: default ( ) ) )
260+ . collect ( ) ;
259261
260262 for ( idx, node) in nodes. iter_enumerated ( ) {
261263 index[ node. kind . to_u16 ( ) as usize ] . insert ( node. hash , idx) ;
@@ -426,6 +428,9 @@ struct EncoderState<K: DepKind> {
426428 total_node_count : usize ,
427429 total_edge_count : usize ,
428430 stats : Option < FxHashMap < K , Stat < K > > > ,
431+
432+ /// Stores the number of times we've encoded each dep kind.
433+ kind_stats : Vec < u32 > ,
429434}
430435
431436impl < K : DepKind > EncoderState < K > {
@@ -435,6 +440,7 @@ impl<K: DepKind> EncoderState<K> {
435440 total_edge_count : 0 ,
436441 total_node_count : 0 ,
437442 stats : record_stats. then ( FxHashMap :: default) ,
443+ kind_stats : iter:: repeat ( 0 ) . take ( K :: MAX as usize + 1 ) . collect ( ) ,
438444 }
439445 }
440446
@@ -445,6 +451,7 @@ impl<K: DepKind> EncoderState<K> {
445451 ) -> DepNodeIndex {
446452 let index = DepNodeIndex :: new ( self . total_node_count ) ;
447453 self . total_node_count += 1 ;
454+ self . kind_stats [ node. node . kind . to_u16 ( ) as usize ] += 1 ;
448455
449456 let edge_count = node. edges . len ( ) ;
450457 self . total_edge_count += edge_count;
@@ -470,11 +477,16 @@ impl<K: DepKind> EncoderState<K> {
470477 }
471478
472479 fn finish ( self , profiler : & SelfProfilerRef ) -> FileEncodeResult {
473- let Self { mut encoder, total_node_count, total_edge_count, stats : _ } = self ;
480+ let Self { mut encoder, total_node_count, total_edge_count, stats : _, kind_stats } = self ;
474481
475482 let node_count = total_node_count. try_into ( ) . unwrap ( ) ;
476483 let edge_count = total_edge_count. try_into ( ) . unwrap ( ) ;
477484
485+ // Encode the number of each dep kind encountered
486+ for count in kind_stats. iter ( ) {
487+ count. encode ( & mut encoder) ;
488+ }
489+
478490 debug ! ( ?node_count, ?edge_count) ;
479491 debug ! ( "position: {:?}" , encoder. position( ) ) ;
480492 IntEncodedWithFixedSize ( node_count) . encode ( & mut encoder) ;
0 commit comments