@@ -4,7 +4,7 @@ use std::fmt::{self, Debug};
44use either:: Either ;
55use itertools:: Itertools ;
66use rustc_data_structures:: captures:: Captures ;
7- use rustc_data_structures:: fx:: FxHashMap ;
7+ use rustc_data_structures:: fx:: { FxHashMap , FxIndexMap } ;
88use rustc_data_structures:: graph:: DirectedGraph ;
99use rustc_index:: IndexVec ;
1010use rustc_index:: bit_set:: DenseBitSet ;
@@ -58,7 +58,8 @@ struct BcbExpression {
5858pub ( super ) struct CoverageCounters {
5959 /// List of places where a counter-increment statement should be injected
6060 /// into MIR, each with its corresponding counter ID.
61- counter_increment_sites : IndexVec < CounterId , BasicCoverageBlock > ,
61+ phys_counter_for_node : FxIndexMap < BasicCoverageBlock , CounterId > ,
62+ next_counter_id : CounterId ,
6263
6364 /// Coverage counters/expressions that are associated with individual BCBs.
6465 node_counters : IndexVec < BasicCoverageBlock , Option < BcbCounter > > ,
@@ -114,16 +115,21 @@ impl CoverageCounters {
114115
115116 fn with_num_bcbs ( num_bcbs : usize ) -> Self {
116117 Self {
117- counter_increment_sites : IndexVec :: new ( ) ,
118+ phys_counter_for_node : FxIndexMap :: default ( ) ,
119+ next_counter_id : CounterId :: ZERO ,
118120 node_counters : IndexVec :: from_elem_n ( None , num_bcbs) ,
119121 expressions : IndexVec :: new ( ) ,
120122 expressions_memo : FxHashMap :: default ( ) ,
121123 }
122124 }
123125
124- /// Creates a new physical counter for a BCB node.
125- fn make_phys_counter ( & mut self , bcb : BasicCoverageBlock ) -> BcbCounter {
126- let id = self . counter_increment_sites . push ( bcb) ;
126+ /// Returns the physical counter for the given node, creating it if necessary.
127+ fn ensure_phys_counter ( & mut self , bcb : BasicCoverageBlock ) -> BcbCounter {
128+ let id = * self . phys_counter_for_node . entry ( bcb) . or_insert_with ( || {
129+ let id = self . next_counter_id ;
130+ self . next_counter_id = id + 1 ;
131+ id
132+ } ) ;
127133 BcbCounter :: Counter { id }
128134 }
129135
@@ -152,7 +158,9 @@ impl CoverageCounters {
152158 }
153159
154160 pub ( super ) fn num_counters ( & self ) -> usize {
155- self . counter_increment_sites . len ( )
161+ let num_counters = self . phys_counter_for_node . len ( ) ;
162+ assert_eq ! ( num_counters, self . next_counter_id. as_usize( ) ) ;
163+ num_counters
156164 }
157165
158166 fn set_node_counter ( & mut self , bcb : BasicCoverageBlock , counter : BcbCounter ) -> BcbCounter {
@@ -174,7 +182,7 @@ impl CoverageCounters {
174182 pub ( super ) fn counter_increment_sites (
175183 & self ,
176184 ) -> impl Iterator < Item = ( CounterId , BasicCoverageBlock ) > + Captures < ' _ > {
177- self . counter_increment_sites . iter_enumerated ( ) . map ( |( id , & site ) | ( id, site) )
185+ self . phys_counter_for_node . iter ( ) . map ( |( & site , & id ) | ( id, site) )
178186 }
179187
180188 /// Returns an iterator over the subset of BCB nodes that have been associated
@@ -212,16 +220,11 @@ impl CoverageCounters {
212220struct Transcriber {
213221 old : NodeCounters < BasicCoverageBlock > ,
214222 new : CoverageCounters ,
215- phys_counter_for_node : FxHashMap < BasicCoverageBlock , BcbCounter > ,
216223}
217224
218225impl Transcriber {
219226 fn new ( num_nodes : usize , old : NodeCounters < BasicCoverageBlock > ) -> Self {
220- Self {
221- old,
222- new : CoverageCounters :: with_num_bcbs ( num_nodes) ,
223- phys_counter_for_node : FxHashMap :: default ( ) ,
224- }
227+ Self { old, new : CoverageCounters :: with_num_bcbs ( num_nodes) }
225228 }
226229
227230 fn transcribe_counters (
@@ -250,7 +253,7 @@ impl Transcriber {
250253 neg. sort ( ) ;
251254
252255 let mut new_counters_for_sites = |sites : Vec < BasicCoverageBlock > | {
253- sites. into_iter ( ) . map ( |node| self . ensure_phys_counter ( node) ) . collect :: < Vec < _ > > ( )
256+ sites. into_iter ( ) . map ( |node| self . new . ensure_phys_counter ( node) ) . collect :: < Vec < _ > > ( )
254257 } ;
255258 let mut pos = new_counters_for_sites ( pos) ;
256259 let mut neg = new_counters_for_sites ( neg) ;
@@ -265,8 +268,4 @@ impl Transcriber {
265268
266269 self . new
267270 }
268-
269- fn ensure_phys_counter ( & mut self , bcb : BasicCoverageBlock ) -> BcbCounter {
270- * self . phys_counter_for_node . entry ( bcb) . or_insert_with ( || self . new . make_phys_counter ( bcb) )
271- }
272271}
0 commit comments