coverage: Separate creation of edge counters from building their sum

This commit is contained in:
Zalathar 2024-09-11 17:16:14 +10:00
parent 6c65d4f47f
commit a7a3595668

View file

@ -315,20 +315,20 @@ impl<'a> MakeBcbCounters<'a> {
// For each out-edge other than the one that was chosen to get an expression, // For each out-edge other than the one that was chosen to get an expression,
// ensure that it has a counter (existing counter/expression or a new counter), // ensure that it has a counter (existing counter/expression or a new counter),
// and accumulate the corresponding counters into a single sum expression. // and accumulate the corresponding counters into a single sum expression.
let sum_of_all_other_out_edges: BcbCounter = { let other_out_edge_counters = successors
let _span = debug_span!("sum_of_all_other_out_edges", ?expression_to_bcb).entered(); .iter()
successors .copied()
.iter() // Skip the chosen edge, since we'll calculate its count from this sum.
.copied() .filter(|&to_bcb| to_bcb != expression_to_bcb)
// Skip the chosen edge, since we'll calculate its count from this sum. .map(|to_bcb| self.get_or_make_edge_counter(from_bcb, to_bcb))
.filter(|&to_bcb| to_bcb != expression_to_bcb) .collect::<Vec<_>>();
.fold(None, |accum, to_bcb| { let sum_of_all_other_out_edges: BcbCounter = other_out_edge_counters
let _span = debug_span!("to_bcb", ?accum, ?to_bcb).entered(); .iter()
let edge_counter = self.get_or_make_edge_counter(from_bcb, to_bcb); .copied()
Some(self.coverage_counters.make_sum_expression(accum, edge_counter)) .fold(None, |accum, edge_counter| {
}) Some(self.coverage_counters.make_sum_expression(accum, edge_counter))
.expect("there must be at least one other out-edge") })
}; .expect("there must be at least one other out-edge");
// Now create an expression for the chosen edge, by taking the counter // Now create an expression for the chosen edge, by taking the counter
// for its source node and subtracting the sum of its sibling out-edges. // for its source node and subtracting the sum of its sibling out-edges.
@ -375,20 +375,18 @@ impl<'a> MakeBcbCounters<'a> {
// A BCB with multiple incoming edges can compute its count by ensuring that counters // A BCB with multiple incoming edges can compute its count by ensuring that counters
// exist for each of those edges, and then adding them up to get a total count. // exist for each of those edges, and then adding them up to get a total count.
let sum_of_in_edges: BcbCounter = { let in_edge_counters = self.basic_coverage_blocks.predecessors[bcb]
let _span = debug_span!("sum_of_in_edges", ?bcb).entered(); .iter()
// We avoid calling `self.bcb_predecessors` here so that we can .copied()
// call methods on `&mut self` inside the fold. .map(|from_bcb| self.get_or_make_edge_counter(from_bcb, bcb))
self.basic_coverage_blocks.predecessors[bcb] .collect::<Vec<_>>();
.iter() let sum_of_in_edges: BcbCounter = in_edge_counters
.copied() .iter()
.fold(None, |accum, from_bcb| { .copied()
let _span = debug_span!("from_bcb", ?accum, ?from_bcb).entered(); .fold(None, |accum, edge_counter| {
let edge_counter = self.get_or_make_edge_counter(from_bcb, bcb); Some(self.coverage_counters.make_sum_expression(accum, edge_counter))
Some(self.coverage_counters.make_sum_expression(accum, edge_counter)) })
}) .expect("there must be at least one in-edge");
.expect("there must be at least one in-edge")
};
debug!("{bcb:?} gets a new counter (sum of predecessor counters): {sum_of_in_edges:?}"); debug!("{bcb:?} gets a new counter (sum of predecessor counters): {sum_of_in_edges:?}");
self.coverage_counters.set_bcb_counter(bcb, sum_of_in_edges) self.coverage_counters.set_bcb_counter(bcb, sum_of_in_edges)