Skip to content

Commit 1dcff2d

Browse files
committed
Auto merge of rust-lang#98638 - bjorn3:less_string_interning, r=tmiasko
Use less string interning This removes string interning in a couple of places where doing so won't result in perf improvements. I also switched one place to use pre-interned symbols.
2 parents 0f97e02 + f688f4d commit 1dcff2d

File tree

14 files changed

+103
-96
lines changed

14 files changed

+103
-96
lines changed

compiler/rustc_codegen_gcc/src/common.rs

+9-7
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ use rustc_middle::mir::Mutability;
1212
use rustc_middle::ty::ScalarInt;
1313
use rustc_middle::ty::layout::{TyAndLayout, LayoutOf};
1414
use rustc_middle::mir::interpret::{ConstAllocation, GlobalAlloc, Scalar};
15-
use rustc_span::Symbol;
1615
use rustc_target::abi::{self, HasDataLayout, Pointer, Size};
1716

1817
use crate::consts::const_alloc_to_gcc;
@@ -125,12 +124,15 @@ impl<'gcc, 'tcx> ConstMethods<'tcx> for CodegenCx<'gcc, 'tcx> {
125124
self.context.new_rvalue_from_double(typ, val)
126125
}
127126

128-
fn const_str(&self, s: Symbol) -> (RValue<'gcc>, RValue<'gcc>) {
129-
let s_str = s.as_str();
130-
let str_global = *self.const_str_cache.borrow_mut().entry(s).or_insert_with(|| {
131-
self.global_string(s_str)
132-
});
133-
let len = s_str.len();
127+
fn const_str(&self, s: &str) -> (RValue<'gcc>, RValue<'gcc>) {
128+
let str_global = *self
129+
.const_str_cache
130+
.borrow_mut()
131+
.raw_entry_mut()
132+
.from_key(s)
133+
.or_insert_with(|| (s.to_owned(), self.global_string(s)))
134+
.1;
135+
let len = s.len();
134136
let cs = self.const_ptrcast(str_global.get_address(None),
135137
self.type_ptr_to(self.layout_of(self.tcx.types.str_).gcc_type(self, true)),
136138
);

compiler/rustc_codegen_gcc/src/context.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use rustc_middle::mir::mono::CodegenUnit;
1313
use rustc_middle::ty::{self, Instance, ParamEnv, PolyExistentialTraitRef, Ty, TyCtxt};
1414
use rustc_middle::ty::layout::{FnAbiError, FnAbiOfHelpers, FnAbiRequest, HasParamEnv, HasTyCtxt, LayoutError, TyAndLayout, LayoutOfHelpers};
1515
use rustc_session::Session;
16-
use rustc_span::{Span, Symbol};
16+
use rustc_span::Span;
1717
use rustc_target::abi::{call::FnAbi, HasDataLayout, PointeeInfo, Size, TargetDataLayout, VariantIdx};
1818
use rustc_target::spec::{HasTargetSpec, Target, TlsModel};
1919

@@ -101,7 +101,7 @@ pub struct CodegenCx<'gcc, 'tcx> {
101101
pub global_lvalues: RefCell<FxHashMap<RValue<'gcc>, LValue<'gcc>>>,
102102

103103
/// Cache of constant strings,
104-
pub const_str_cache: RefCell<FxHashMap<Symbol, LValue<'gcc>>>,
104+
pub const_str_cache: RefCell<FxHashMap<String, LValue<'gcc>>>,
105105

106106
/// Cache of globals.
107107
pub globals: RefCell<FxHashMap<String, RValue<'gcc>>>,

compiler/rustc_codegen_gcc/src/lib.rs

+8-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,14 @@
66
* TODO(antoyo): remove the patches.
77
*/
88

9-
#![feature(rustc_private, decl_macro, associated_type_bounds, never_type, trusted_len)]
9+
#![feature(
10+
rustc_private,
11+
decl_macro,
12+
associated_type_bounds,
13+
never_type,
14+
trusted_len,
15+
hash_raw_entry
16+
)]
1017
#![allow(broken_intra_doc_links)]
1118
#![recursion_limit="256"]
1219
#![warn(rust_2018_idioms)]

compiler/rustc_codegen_llvm/src/common.rs

+21-17
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ use rustc_middle::bug;
1414
use rustc_middle::mir::interpret::{ConstAllocation, GlobalAlloc, Scalar};
1515
use rustc_middle::ty::layout::{LayoutOf, TyAndLayout};
1616
use rustc_middle::ty::ScalarInt;
17-
use rustc_span::symbol::Symbol;
1817
use rustc_target::abi::{self, AddressSpace, HasDataLayout, Pointer, Size};
1918

2019
use libc::{c_char, c_uint};
@@ -180,22 +179,27 @@ impl<'ll, 'tcx> ConstMethods<'tcx> for CodegenCx<'ll, 'tcx> {
180179
unsafe { llvm::LLVMConstReal(t, val) }
181180
}
182181

183-
fn const_str(&self, s: Symbol) -> (&'ll Value, &'ll Value) {
184-
let s_str = s.as_str();
185-
let str_global = *self.const_str_cache.borrow_mut().entry(s).or_insert_with(|| {
186-
let sc = self.const_bytes(s_str.as_bytes());
187-
let sym = self.generate_local_symbol_name("str");
188-
let g = self.define_global(&sym, self.val_ty(sc)).unwrap_or_else(|| {
189-
bug!("symbol `{}` is already defined", sym);
190-
});
191-
unsafe {
192-
llvm::LLVMSetInitializer(g, sc);
193-
llvm::LLVMSetGlobalConstant(g, True);
194-
llvm::LLVMRustSetLinkage(g, llvm::Linkage::InternalLinkage);
195-
}
196-
g
197-
});
198-
let len = s_str.len();
182+
fn const_str(&self, s: &str) -> (&'ll Value, &'ll Value) {
183+
let str_global = *self
184+
.const_str_cache
185+
.borrow_mut()
186+
.raw_entry_mut()
187+
.from_key(s)
188+
.or_insert_with(|| {
189+
let sc = self.const_bytes(s.as_bytes());
190+
let sym = self.generate_local_symbol_name("str");
191+
let g = self.define_global(&sym, self.val_ty(sc)).unwrap_or_else(|| {
192+
bug!("symbol `{}` is already defined", sym);
193+
});
194+
unsafe {
195+
llvm::LLVMSetInitializer(g, sc);
196+
llvm::LLVMSetGlobalConstant(g, True);
197+
llvm::LLVMRustSetLinkage(g, llvm::Linkage::InternalLinkage);
198+
}
199+
(s.to_owned(), g)
200+
})
201+
.1;
202+
let len = s.len();
199203
let cs = consts::ptrcast(
200204
str_global,
201205
self.type_ptr_to(self.layout_of(self.tcx.types.str_).llvm_type(self)),

compiler/rustc_codegen_llvm/src/context.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ use rustc_session::config::{BranchProtection, CFGuard, CFProtection};
2626
use rustc_session::config::{CrateType, DebugInfo, PAuthKey, PacRet};
2727
use rustc_session::Session;
2828
use rustc_span::source_map::Span;
29-
use rustc_span::symbol::Symbol;
3029
use rustc_target::abi::{
3130
call::FnAbi, HasDataLayout, PointeeInfo, Size, TargetDataLayout, VariantIdx,
3231
};
@@ -56,7 +55,7 @@ pub struct CodegenCx<'ll, 'tcx> {
5655
pub vtables:
5756
RefCell<FxHashMap<(Ty<'tcx>, Option<ty::PolyExistentialTraitRef<'tcx>>), &'ll Value>>,
5857
/// Cache of constant strings,
59-
pub const_str_cache: RefCell<FxHashMap<Symbol, &'ll Value>>,
58+
pub const_str_cache: RefCell<FxHashMap<String, &'ll Value>>,
6059

6160
/// Reverse-direction for const ptrs cast from globals.
6261
///

compiler/rustc_codegen_llvm/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
//! This API is completely unstable and subject to change.
66
77
#![doc(html_root_url = "https://doc.rust-lang.org/nightly/nightly-rustc/")]
8+
#![feature(hash_raw_entry)]
89
#![feature(let_chains)]
910
#![feature(let_else)]
1011
#![feature(extern_types)]

compiler/rustc_codegen_ssa/src/back/archive.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
use rustc_data_structures::temp_dir::MaybeTempDir;
22
use rustc_session::cstore::DllImport;
33
use rustc_session::Session;
4-
use rustc_span::symbol::Symbol;
54

65
use std::io;
76
use std::path::{Path, PathBuf};
87

98
pub(super) fn find_library(
10-
name: Symbol,
9+
name: &str,
1110
verbatim: bool,
1211
search_paths: &[PathBuf],
1312
sess: &Session,

compiler/rustc_codegen_ssa/src/back/command.rs

-6
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ use std::io;
77
use std::mem;
88
use std::process::{self, Output};
99

10-
use rustc_span::symbol::Symbol;
1110
use rustc_target::spec::LldFlavor;
1211

1312
#[derive(Clone)]
@@ -47,11 +46,6 @@ impl Command {
4746
self
4847
}
4948

50-
pub fn sym_arg(&mut self, arg: Symbol) -> &mut Command {
51-
self.arg(arg.as_str());
52-
self
53-
}
54-
5549
pub fn args<I>(&mut self, args: I) -> &mut Command
5650
where
5751
I: IntoIterator<Item: AsRef<OsStr>>,

compiler/rustc_codegen_ssa/src/back/link.rs

+6-3
Original file line numberDiff line numberDiff line change
@@ -358,7 +358,7 @@ fn link_rlib<'a, B: ArchiveBuilder<'a>>(
358358
}
359359
if let Some(name) = lib.name {
360360
let location =
361-
find_library(name, lib.verbatim.unwrap_or(false), &lib_search_paths, sess);
361+
find_library(name.as_str(), lib.verbatim.unwrap_or(false), &lib_search_paths, sess);
362362
ab.add_archive(&location, |_| false).unwrap_or_else(|e| {
363363
sess.fatal(&format!(
364364
"failed to add native library {}: {}",
@@ -1122,7 +1122,7 @@ fn link_sanitizer_runtime(sess: &Session, linker: &mut dyn Linker, name: &str) {
11221122
let path = find_sanitizer_runtime(&sess, &filename);
11231123
let rpath = path.to_str().expect("non-utf8 component in path");
11241124
linker.args(&["-Wl,-rpath", "-Xlinker", rpath]);
1125-
linker.link_dylib(Symbol::intern(&filename), false, true);
1125+
linker.link_dylib(&filename, false, true);
11261126
} else {
11271127
let filename = format!("librustc{}_rt.{}.a", channel, name);
11281128
let path = find_sanitizer_runtime(&sess, &filename).join(&filename);
@@ -2204,6 +2204,7 @@ fn add_local_native_libraries(
22042204
let Some(name) = lib.name else {
22052205
continue;
22062206
};
2207+
let name = name.as_str();
22072208

22082209
// Skip if this library is the same as the last.
22092210
last = if (lib.name, lib.kind, lib.verbatim) == last {
@@ -2367,6 +2368,7 @@ fn add_upstream_rust_crates<'a, B: ArchiveBuilder<'a>>(
23672368
let Some(name) = lib.name else {
23682369
continue;
23692370
};
2371+
let name = name.as_str();
23702372
if !relevant_lib(sess, lib) {
23712373
continue;
23722374
}
@@ -2524,7 +2526,7 @@ fn add_upstream_rust_crates<'a, B: ArchiveBuilder<'a>>(
25242526
}
25252527
let filestem = cratepath.file_stem().unwrap().to_str().unwrap();
25262528
cmd.link_rust_dylib(
2527-
Symbol::intern(&unlib(&sess.target, filestem)),
2529+
&unlib(&sess.target, filestem),
25282530
parent.unwrap_or_else(|| Path::new("")),
25292531
);
25302532
}
@@ -2556,6 +2558,7 @@ fn add_upstream_native_libraries(
25562558
let Some(name) = lib.name else {
25572559
continue;
25582560
};
2561+
let name = name.as_str();
25592562
if !relevant_lib(sess, &lib) {
25602563
continue;
25612564
}

0 commit comments

Comments
 (0)