From 1f71f0f2b54981d4cc7a99201d180e787c13ca6d Mon Sep 17 00:00:00 2001 From: Josh Stone Date: Fri, 7 Aug 2020 20:42:29 -0700 Subject: [PATCH] rustc_codegen_llvm: use IndexSet in CoverageMapGenerator --- .../coverageinfo/mapgen.rs | 19 +++++-------------- src/librustc_codegen_llvm/coverageinfo/mod.rs | 7 +++++-- 2 files changed, 10 insertions(+), 16 deletions(-) diff --git a/src/librustc_codegen_llvm/coverageinfo/mapgen.rs b/src/librustc_codegen_llvm/coverageinfo/mapgen.rs index 9d2383abeed..b50b3b6d975 100644 --- a/src/librustc_codegen_llvm/coverageinfo/mapgen.rs +++ b/src/librustc_codegen_llvm/coverageinfo/mapgen.rs @@ -6,7 +6,7 @@ use llvm::coverageinfo::CounterMappingRegion; use log::debug; use rustc_codegen_ssa::coverageinfo::map::{Counter, CounterExpression, Region}; use rustc_codegen_ssa::traits::{BaseTypeMethods, ConstMethods}; -use rustc_data_structures::fx::FxHashMap; +use rustc_data_structures::fx::FxIndexSet; use rustc_llvm::RustString; use std::ffi::CString; @@ -76,13 +76,12 @@ pub fn finalize<'ll, 'tcx>(cx: &CodegenCx<'ll, 'tcx>) { } struct CoverageMapGenerator { - filenames: Vec, - filename_to_index: FxHashMap, + filenames: FxIndexSet, } impl CoverageMapGenerator { fn new() -> Self { - Self { filenames: Vec::new(), filename_to_index: FxHashMap::default() } + Self { filenames: FxIndexSet::default() } } /// Using the `expressions` and `counter_regions` collected for the current function, generate @@ -122,16 +121,8 @@ impl CoverageMapGenerator { let c_filename = CString::new(file_name).expect("null error converting filename to C string"); debug!(" file_id: {} = '{:?}'", current_file_id, c_filename); - let filenames_index = match self.filename_to_index.get(&c_filename) { - Some(index) => *index, - None => { - let index = self.filenames.len() as u32; - self.filenames.push(c_filename.clone()); - self.filename_to_index.insert(c_filename.clone(), index); - index - } - }; - virtual_file_mapping.push(filenames_index); + let (filenames_index, _) = self.filenames.insert_full(c_filename); + virtual_file_mapping.push(filenames_index as u32); } mapping_regions.push(CounterMappingRegion::code_region( counter, diff --git a/src/librustc_codegen_llvm/coverageinfo/mod.rs b/src/librustc_codegen_llvm/coverageinfo/mod.rs index 7b864e499d1..90831f0bcfb 100644 --- a/src/librustc_codegen_llvm/coverageinfo/mod.rs +++ b/src/librustc_codegen_llvm/coverageinfo/mod.rs @@ -97,8 +97,11 @@ impl CoverageInfoBuilderMethods<'tcx> for Builder<'a, 'll, 'tcx> { } } -pub(crate) fn write_filenames_section_to_buffer(filenames: &Vec, buffer: &RustString) { - let c_str_vec = filenames.iter().map(|cstring| cstring.as_ptr()).collect::>(); +pub(crate) fn write_filenames_section_to_buffer<'a>( + filenames: impl IntoIterator, + buffer: &RustString, +) { + let c_str_vec = filenames.into_iter().map(|cstring| cstring.as_ptr()).collect::>(); unsafe { llvm::LLVMRustCoverageWriteFilenamesSectionToBuffer( c_str_vec.as_ptr(),