From 6ef39e69eac42e1cc86ec8eeb1b85c6b3a8784fa Mon Sep 17 00:00:00 2001
From: Nicholas Nethercote <nnethercote@mozilla.com>
Date: Thu, 2 May 2019 20:34:26 +1000
Subject: [PATCH] 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.
---
 .../debuginfo/metadata.rs                     | 22 +++++++++++--------
 src/librustc_codegen_llvm/debuginfo/mod.rs    |  2 +-
 2 files changed, 14 insertions(+), 10 deletions(-)

diff --git a/src/librustc_codegen_llvm/debuginfo/metadata.rs b/src/librustc_codegen_llvm/debuginfo/metadata.rs
index 765992fd2b70e..0d67865d71708 100644
--- a/src/librustc_codegen_llvm/debuginfo/metadata.rs
+++ b/src/librustc_codegen_llvm/debuginfo/metadata.rs
@@ -784,26 +784,30 @@ pub fn file_metadata(cx: &CodegenCx<'ll, '_>,
            file_name,
            defining_crate);
 
-    let directory = if defining_crate == LOCAL_CRATE {
-        &cx.sess().working_dir.0
+    let file_name = &file_name.to_string();
+    let file_name_symbol = Symbol::intern(file_name);
+    if defining_crate == LOCAL_CRATE {
+        let directory = &cx.sess().working_dir.0.to_string_lossy();
+        file_metadata_raw(cx, file_name, Some(file_name_symbol),
+                          directory, Some(Symbol::intern(directory)))
     } else {
         // If the path comes from an upstream crate we assume it has been made
         // independent of the compiler's working directory one way or another.
-        Path::new("")
-    };
-
-    file_metadata_raw(cx, &file_name.to_string(), &directory.to_string_lossy())
+        file_metadata_raw(cx, file_name, Some(file_name_symbol), "", None)
+    }
 }
 
 pub fn unknown_file_metadata(cx: &CodegenCx<'ll, '_>) -> &'ll DIFile {
-    file_metadata_raw(cx, "<unknown>", "")
+    file_metadata_raw(cx, "<unknown>", None, "", None)
 }
 
 fn file_metadata_raw(cx: &CodegenCx<'ll, '_>,
                      file_name: &str,
-                     directory: &str)
+                     file_name_symbol: Option<Symbol>,
+                     directory: &str,
+                     directory_symbol: Option<Symbol>)
                      -> &'ll DIFile {
-    let key = (Symbol::intern(file_name), Symbol::intern(directory));
+    let key = (file_name_symbol, directory_symbol);
 
     if let Some(file_metadata) = debug_context(cx).created_files.borrow().get(&key) {
         return *file_metadata;
diff --git a/src/librustc_codegen_llvm/debuginfo/mod.rs b/src/librustc_codegen_llvm/debuginfo/mod.rs
index 2154ac9b1d259..f3070a03b4ed5 100644
--- a/src/librustc_codegen_llvm/debuginfo/mod.rs
+++ b/src/librustc_codegen_llvm/debuginfo/mod.rs
@@ -63,7 +63,7 @@ pub struct CrateDebugContext<'a, 'tcx> {
     llcontext: &'a llvm::Context,
     llmod: &'a llvm::Module,
     builder: &'a mut DIBuilder<'a>,
-    created_files: RefCell<FxHashMap<(Symbol, Symbol), &'a DIFile>>,
+    created_files: RefCell<FxHashMap<(Option<Symbol>, Option<Symbol>), &'a DIFile>>,
     created_enum_disr_types: RefCell<FxHashMap<(DefId, layout::Primitive), &'a DIType>>,
 
     type_map: RefCell<TypeMap<'a, 'tcx>>,