Skip to content

Commit 64ebdc2

Browse files
committed
Avoid intermediate Vec allocations in coverage mapping creation
1 parent 39cba67 commit 64ebdc2

File tree

5 files changed

+19
-18
lines changed

5 files changed

+19
-18
lines changed

compiler/rustc_codegen_llvm/src/coverageinfo/map_data.rs

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -134,12 +134,12 @@ impl<'tcx> FunctionCoverage<'tcx> {
134134
self.source_hash
135135
}
136136

137-
/// Generate an array of CounterExpressions, and an iterator over all `Counter`s and their
138-
/// associated `Regions` (from which the LLVM-specific `CoverageMapGenerator` will create
139-
/// `CounterMappingRegion`s.
137+
/// Generate an array of CounterExpressions, and an array of all `Counter`s and their
138+
/// associated `Regions` sorted by `Region`, from which the LLVM-specific
139+
/// `CoverageMapGenerator` will create `CounterMappingRegion`s.
140140
pub fn get_expressions_and_counter_regions(
141141
&self,
142-
) -> (Vec<CounterExpression>, impl Iterator<Item = (Counter, &CodeRegion)>) {
142+
) -> (Vec<CounterExpression>, Vec<(Counter, &CodeRegion)>) {
143143
assert!(
144144
self.source_hash != 0 || !self.is_used,
145145
"No counters provided the source_hash for used function: {:?}",
@@ -150,9 +150,12 @@ impl<'tcx> FunctionCoverage<'tcx> {
150150
let (counter_expressions, expression_regions) = self.expressions_with_regions();
151151
let unreachable_regions = self.unreachable_regions();
152152

153-
let counter_regions =
154-
counter_regions.chain(expression_regions.into_iter().chain(unreachable_regions));
155-
(counter_expressions, counter_regions)
153+
let mut collected_counter_regions = expression_regions;
154+
collected_counter_regions.extend(counter_regions);
155+
collected_counter_regions.extend(unreachable_regions);
156+
collected_counter_regions.sort_unstable_by_key(|(_counter, region)| *region);
157+
158+
(counter_expressions, collected_counter_regions)
156159
}
157160

158161
fn counter_regions(&self) -> impl Iterator<Item = (Counter, &CodeRegion)> {
@@ -165,7 +168,7 @@ impl<'tcx> FunctionCoverage<'tcx> {
165168

166169
fn expressions_with_regions(
167170
&self,
168-
) -> (Vec<CounterExpression>, impl Iterator<Item = (Counter, &CodeRegion)>) {
171+
) -> (Vec<CounterExpression>, Vec<(Counter, &CodeRegion)>) {
169172
let mut counter_expressions = Vec::with_capacity(self.expressions.len());
170173
let mut expression_regions = Vec::with_capacity(self.expressions.len());
171174
let mut new_indexes = IndexVec::from_elem_n(None, self.expressions.len());
@@ -302,7 +305,7 @@ impl<'tcx> FunctionCoverage<'tcx> {
302305
);
303306
}
304307
}
305-
(counter_expressions, expression_regions.into_iter())
308+
(counter_expressions, expression_regions)
306309
}
307310

308311
fn unreachable_regions(&self) -> impl Iterator<Item = (Counter, &CodeRegion)> {

compiler/rustc_codegen_llvm/src/coverageinfo/mapgen.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -140,14 +140,13 @@ impl CoverageMapGenerator {
140140
/// Using the `expressions` and `counter_regions` collected for the current function, generate
141141
/// the `mapping_regions` and `virtual_file_mapping`, and capture any new filenames. Then use
142142
/// LLVM APIs to encode the `virtual_file_mapping`, `expressions`, and `mapping_regions` into
143-
/// the given `coverage_mapping` byte buffer, compliant with the LLVM Coverage Mapping format.
144-
fn write_coverage_mapping<'a>(
143+
/// the given `coverage_mapping_buffer` byte buffer, compliant with the LLVM Coverage Mapping format.
144+
fn write_coverage_mapping(
145145
&mut self,
146146
expressions: Vec<CounterExpression>,
147-
counter_regions: impl Iterator<Item = (Counter, &'a CodeRegion)>,
147+
counter_regions: Vec<(Counter, &CodeRegion)>,
148148
coverage_mapping_buffer: &RustString,
149149
) {
150-
let mut counter_regions = counter_regions.collect::<Vec<_>>();
151150
if counter_regions.is_empty() {
152151
return;
153152
}
@@ -162,7 +161,6 @@ impl CoverageMapGenerator {
162161
// `file_id` (indexing files referenced by the current function), and construct the
163162
// function-specific `virtual_file_mapping` from `file_id` to its index in the module's
164163
// `filenames` array.
165-
counter_regions.sort_unstable_by_key(|(_counter, region)| *region);
166164
for (counter, region) in counter_regions {
167165
let CodeRegion { file_name, start_line, start_col, end_line, end_col } = *region;
168166
let same_file = current_file_name.is_some_and(|p| p == file_name);

compiler/rustc_mir_transform/src/coverage/debug.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,15 +44,15 @@
4444
//! points, which can be enabled via environment variable:
4545
//!
4646
//! ```shell
47-
//! RUSTC_LOG=rustc_mir_transform::transform::coverage=debug
47+
//! RUSTC_LOG=rustc_mir_transform::coverage=debug
4848
//! ```
4949
//!
5050
//! Other module paths with coverage-related debug logs may also be of interest, particularly for
5151
//! debugging the coverage map data, injected as global variables in the LLVM IR (during rustc's
5252
//! code generation pass). For example:
5353
//!
5454
//! ```shell
55-
//! RUSTC_LOG=rustc_mir_transform::transform::coverage,rustc_codegen_ssa::coverageinfo,rustc_codegen_llvm::coverageinfo=debug
55+
//! RUSTC_LOG=rustc_mir_transform::coverage,rustc_codegen_ssa::coverageinfo,rustc_codegen_llvm::coverageinfo=debug
5656
//! ```
5757
//!
5858
//! Coverage Debug Options

compiler/rustc_mir_transform/src/coverage/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,7 @@ impl<'a, 'tcx> Instrumentor<'a, 'tcx> {
218218
}
219219

220220
////////////////////////////////////////////////////
221-
// Remove the counter or edge counter from of each `CoverageSpan`s associated
221+
// Remove the counter or edge counter from each `CoverageSpan`s associated
222222
// `BasicCoverageBlock`, and inject a `Coverage` statement into the MIR.
223223
//
224224
// `Coverage` statements injected from `CoverageSpan`s will include the code regions

compiler/rustc_mir_transform/src/coverage/tests.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
//! pass.
33
//!
44
//! ```shell
5-
//! ./x.py test --keep-stage 1 compiler/rustc_mir --test-args '--show-output coverage'
5+
//! ./x.py test --keep-stage 1 compiler/rustc_mir_transform --test-args '--show-output coverage'
66
//! ```
77
//!
88
//! The tests construct a few "mock" objects, as needed, to support the `InstrumentCoverage`

0 commit comments

Comments
 (0)