@@ -4,7 +4,7 @@ use std::fmt::{self, Debug};
4
4
use either:: Either ;
5
5
use itertools:: Itertools ;
6
6
use rustc_data_structures:: captures:: Captures ;
7
- use rustc_data_structures:: fx:: FxHashMap ;
7
+ use rustc_data_structures:: fx:: { FxHashMap , FxIndexMap } ;
8
8
use rustc_data_structures:: graph:: DirectedGraph ;
9
9
use rustc_index:: IndexVec ;
10
10
use rustc_index:: bit_set:: DenseBitSet ;
@@ -58,7 +58,8 @@ struct BcbExpression {
58
58
pub ( super ) struct CoverageCounters {
59
59
/// List of places where a counter-increment statement should be injected
60
60
/// 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 ,
62
63
63
64
/// Coverage counters/expressions that are associated with individual BCBs.
64
65
node_counters : IndexVec < BasicCoverageBlock , Option < BcbCounter > > ,
@@ -114,16 +115,21 @@ impl CoverageCounters {
114
115
115
116
fn with_num_bcbs ( num_bcbs : usize ) -> Self {
116
117
Self {
117
- counter_increment_sites : IndexVec :: new ( ) ,
118
+ phys_counter_for_node : FxIndexMap :: default ( ) ,
119
+ next_counter_id : CounterId :: ZERO ,
118
120
node_counters : IndexVec :: from_elem_n ( None , num_bcbs) ,
119
121
expressions : IndexVec :: new ( ) ,
120
122
expressions_memo : FxHashMap :: default ( ) ,
121
123
}
122
124
}
123
125
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
+ } ) ;
127
133
BcbCounter :: Counter { id }
128
134
}
129
135
@@ -152,7 +158,9 @@ impl CoverageCounters {
152
158
}
153
159
154
160
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
156
164
}
157
165
158
166
fn set_node_counter ( & mut self , bcb : BasicCoverageBlock , counter : BcbCounter ) -> BcbCounter {
@@ -174,7 +182,7 @@ impl CoverageCounters {
174
182
pub ( super ) fn counter_increment_sites (
175
183
& self ,
176
184
) -> 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) )
178
186
}
179
187
180
188
/// Returns an iterator over the subset of BCB nodes that have been associated
@@ -212,16 +220,11 @@ impl CoverageCounters {
212
220
struct Transcriber {
213
221
old : NodeCounters < BasicCoverageBlock > ,
214
222
new : CoverageCounters ,
215
- phys_counter_for_node : FxHashMap < BasicCoverageBlock , BcbCounter > ,
216
223
}
217
224
218
225
impl Transcriber {
219
226
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) }
225
228
}
226
229
227
230
fn transcribe_counters (
@@ -250,7 +253,7 @@ impl Transcriber {
250
253
neg. sort ( ) ;
251
254
252
255
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 < _ > > ( )
254
257
} ;
255
258
let mut pos = new_counters_for_sites ( pos) ;
256
259
let mut neg = new_counters_for_sites ( neg) ;
@@ -265,8 +268,4 @@ impl Transcriber {
265
268
266
269
self . new
267
270
}
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
- }
272
271
}
0 commit comments