Skip to content

Commit 5ab3b6b

Browse files
authored
Rollup merge of #60467 - nnethercote:less-symbol-interning, r=davidtwco
Avoid repeated interning of static strings. `file_metadata_raw` interns the strings `"<unknown>"` and `""` very frequently. This commit avoids that, which reduces the number of symbols interned significantly and reduces instruction counts by up to 0.5% on some workloads.
2 parents d0057da + 6ef39e6 commit 5ab3b6b

File tree

2 files changed

+14
-10
lines changed

2 files changed

+14
-10
lines changed

src/librustc_codegen_llvm/debuginfo/metadata.rs

+13-9
Original file line numberDiff line numberDiff line change
@@ -784,26 +784,30 @@ pub fn file_metadata(cx: &CodegenCx<'ll, '_>,
784784
file_name,
785785
defining_crate);
786786

787-
let directory = if defining_crate == LOCAL_CRATE {
788-
&cx.sess().working_dir.0
787+
let file_name = &file_name.to_string();
788+
let file_name_symbol = Symbol::intern(file_name);
789+
if defining_crate == LOCAL_CRATE {
790+
let directory = &cx.sess().working_dir.0.to_string_lossy();
791+
file_metadata_raw(cx, file_name, Some(file_name_symbol),
792+
directory, Some(Symbol::intern(directory)))
789793
} else {
790794
// If the path comes from an upstream crate we assume it has been made
791795
// independent of the compiler's working directory one way or another.
792-
Path::new("")
793-
};
794-
795-
file_metadata_raw(cx, &file_name.to_string(), &directory.to_string_lossy())
796+
file_metadata_raw(cx, file_name, Some(file_name_symbol), "", None)
797+
}
796798
}
797799

798800
pub fn unknown_file_metadata(cx: &CodegenCx<'ll, '_>) -> &'ll DIFile {
799-
file_metadata_raw(cx, "<unknown>", "")
801+
file_metadata_raw(cx, "<unknown>", None, "", None)
800802
}
801803

802804
fn file_metadata_raw(cx: &CodegenCx<'ll, '_>,
803805
file_name: &str,
804-
directory: &str)
806+
file_name_symbol: Option<Symbol>,
807+
directory: &str,
808+
directory_symbol: Option<Symbol>)
805809
-> &'ll DIFile {
806-
let key = (Symbol::intern(file_name), Symbol::intern(directory));
810+
let key = (file_name_symbol, directory_symbol);
807811

808812
if let Some(file_metadata) = debug_context(cx).created_files.borrow().get(&key) {
809813
return *file_metadata;

src/librustc_codegen_llvm/debuginfo/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ pub struct CrateDebugContext<'a, 'tcx> {
6363
llcontext: &'a llvm::Context,
6464
llmod: &'a llvm::Module,
6565
builder: &'a mut DIBuilder<'a>,
66-
created_files: RefCell<FxHashMap<(Symbol, Symbol), &'a DIFile>>,
66+
created_files: RefCell<FxHashMap<(Option<Symbol>, Option<Symbol>), &'a DIFile>>,
6767
created_enum_disr_types: RefCell<FxHashMap<(DefId, layout::Primitive), &'a DIType>>,
6868

6969
type_map: RefCell<TypeMap<'a, 'tcx>>,

0 commit comments

Comments
 (0)