Skip to content

Commit f6484fa

Browse files
committed
Avoid unnecessary string interning for const_str
1 parent 94e9374 commit f6484fa

File tree

8 files changed

+45
-34
lines changed

8 files changed

+45
-34
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};
@@ -181,22 +180,27 @@ impl<'ll, 'tcx> ConstMethods<'tcx> for CodegenCx<'ll, 'tcx> {
181180
unsafe { llvm::LLVMConstReal(t, val) }
182181
}
183182

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

+2-3
Original file line numberDiff line numberDiff line change
@@ -481,8 +481,7 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
481481
(LangItem::PanicBoundsCheck, vec![index, len, location])
482482
}
483483
_ => {
484-
let msg_str = Symbol::intern(msg.description());
485-
let msg = bx.const_str(msg_str);
484+
let msg = bx.const_str(msg.description());
486485
// It's `pub fn panic(expr: &str)`, with the wide reference being passed
487486
// as two arguments, and `#[track_caller]` adds an implicit third argument.
488487
(LangItem::Panic, vec![msg.0, msg.1, location])
@@ -563,7 +562,7 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
563562
}
564563
})
565564
});
566-
let msg = bx.const_str(Symbol::intern(&msg_str));
565+
let msg = bx.const_str(&msg_str);
567566
let location = self.get_caller_location(bx, source_info).immediate();
568567

569568
// Obtain the panic entry point.

compiler/rustc_codegen_ssa/src/traits/consts.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ use super::BackendTypes;
22
use crate::mir::place::PlaceRef;
33
use rustc_middle::mir::interpret::{ConstAllocation, Scalar};
44
use rustc_middle::ty::layout::TyAndLayout;
5-
use rustc_span::Symbol;
65
use rustc_target::abi::{self, Size};
76

87
pub trait ConstMethods<'tcx>: BackendTypes {
@@ -21,7 +20,7 @@ pub trait ConstMethods<'tcx>: BackendTypes {
2120
fn const_u8(&self, i: u8) -> Self::Value;
2221
fn const_real(&self, t: Self::Type, val: f64) -> Self::Value;
2322

24-
fn const_str(&self, s: Symbol) -> (Self::Value, Self::Value);
23+
fn const_str(&self, s: &str) -> (Self::Value, Self::Value);
2524
fn const_struct(&self, elts: &[Self::Value], packed: bool) -> Self::Value;
2625

2726
fn const_to_opt_uint(&self, v: Self::Value) -> Option<u64>;

0 commit comments

Comments
 (0)