@@ -218,11 +218,15 @@ rustc_index::newtype_index! {
218218#[ derive( Default ) ]
219219struct VirtualFileMapping {
220220 local_to_global : IndexVec < LocalFileId , u32 > ,
221+ global_to_local : FxIndexMap < u32 , LocalFileId > ,
221222}
222223
223224impl 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:?}" ) ;
0 commit comments