Skip to content

Commit 6644921

Browse files
committed
coverage: Encode function mappings without re-sorting them
The main change here is that `VirtualFileMapping` now uses an internal hashmap to de-duplicate incoming global file IDs. That removes the need for `encode_mappings_for_function` to re-sort its mappings by filename in order to de-duplicate them. (We still de-duplicate runs of identical filenames to save work, but this is not load-bearing for correctness, so a sort is not necessary.)
1 parent dba8e49 commit 6644921

File tree

2 files changed

+14
-12
lines changed

2 files changed

+14
-12
lines changed

compiler/rustc_codegen_llvm/src/coverageinfo/mapgen.rs

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -218,11 +218,15 @@ rustc_index::newtype_index! {
218218
#[derive(Default)]
219219
struct VirtualFileMapping {
220220
local_to_global: IndexVec<LocalFileId, u32>,
221+
global_to_local: FxIndexMap<u32, LocalFileId>,
221222
}
222223

223224
impl VirtualFileMapping {
224-
fn push_global_id(&mut self, global_file_id: u32) -> LocalFileId {
225-
self.local_to_global.push(global_file_id)
225+
fn local_id_for_global(&mut self, global_file_id: u32) -> LocalFileId {
226+
*self
227+
.global_to_local
228+
.entry(global_file_id)
229+
.or_insert_with(|| self.local_to_global.push(global_file_id))
226230
}
227231

228232
fn into_vec(self) -> Vec<u32> {
@@ -239,7 +243,7 @@ fn encode_mappings_for_function(
239243
global_file_table: &GlobalFileTable,
240244
function_coverage: &FunctionCoverage<'_>,
241245
) -> Vec<u8> {
242-
let mut counter_regions = function_coverage.counter_regions().collect::<Vec<_>>();
246+
let counter_regions = function_coverage.counter_regions();
243247
if counter_regions.is_empty() {
244248
return Vec::new();
245249
}
@@ -249,25 +253,23 @@ fn encode_mappings_for_function(
249253
let mut virtual_file_mapping = VirtualFileMapping::default();
250254
let mut mapping_regions = Vec::with_capacity(counter_regions.len());
251255

252-
// Sort and group the list of (counter, region) mapping pairs by filename.
253-
// (Preserve any further ordering imposed by `FunctionCoverage`.)
256+
// Group mappings into runs with the same filename, preserving the order
257+
// yielded by `FunctionCoverage`.
254258
// Prepare file IDs for each filename, and prepare the mapping data so that
255259
// we can pass it through FFI to LLVM.
256-
counter_regions.sort_by_key(|(_counter, region)| region.file_name);
257-
for counter_regions_for_file in
258-
counter_regions.group_by(|(_, a), (_, b)| a.file_name == b.file_name)
260+
for (file_name, counter_regions_for_file) in
261+
&counter_regions.group_by(|(_counter, region)| region.file_name)
259262
{
260263
// Look up the global file ID for this filename.
261-
let file_name = counter_regions_for_file[0].1.file_name;
262264
let global_file_id = global_file_table.global_file_id_for_file_name(file_name);
263265

264266
// Associate that global file ID with a local file ID for this function.
265-
let local_file_id = virtual_file_mapping.push_global_id(global_file_id);
267+
let local_file_id = virtual_file_mapping.local_id_for_global(global_file_id);
266268
debug!(" file id: {local_file_id:?} => global {global_file_id} = '{file_name:?}'");
267269

268270
// For each counter/region pair in this function+file, convert it to a
269271
// form suitable for FFI.
270-
for &(counter, region) in counter_regions_for_file {
272+
for (counter, region) in counter_regions_for_file {
271273
let CodeRegion { file_name: _, start_line, start_col, end_line, end_col } = *region;
272274

273275
debug!("Adding counter {counter:?} to map for {region:?}");

compiler/rustc_codegen_llvm/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,13 @@
88
#![cfg_attr(not(bootstrap), feature(rustdoc_internals))]
99
#![cfg_attr(not(bootstrap), doc(rust_logo))]
1010
#![doc(html_root_url = "https://doc.rust-lang.org/nightly/nightly-rustc/")]
11+
#![feature(exact_size_is_empty)]
1112
#![feature(extern_types)]
1213
#![feature(hash_raw_entry)]
1314
#![feature(iter_intersperse)]
1415
#![feature(let_chains)]
1516
#![feature(min_specialization)]
1617
#![feature(never_type)]
17-
#![feature(slice_group_by)]
1818
#![feature(impl_trait_in_assoc_type)]
1919
#![recursion_limit = "256"]
2020
#![allow(rustc::potential_query_instability)]

0 commit comments

Comments
 (0)