Skip to content

Commit 13a502d

Browse files
committed
Changed String to Rc<str> and added extra cache check
1 parent fdd211a commit 13a502d

File tree

2 files changed

+13
-8
lines changed

2 files changed

+13
-8
lines changed

compiler/rustc_codegen_llvm/src/debuginfo/mod.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ use rustc_target::abi::{Primitive, Size};
3838
use libc::c_uint;
3939
use smallvec::SmallVec;
4040
use std::cell::RefCell;
41+
use std::rc::Rc;
4142
use std::iter;
4243
use tracing::debug;
4344

@@ -63,7 +64,7 @@ pub struct CrateDebugContext<'a, 'tcx> {
6364
builder: &'a mut DIBuilder<'a>,
6465
created_files: RefCell<FxHashMap<(Option<String>, Option<String>), &'a DIFile>>,
6566
created_enum_disr_types: RefCell<FxHashMap<(DefId, Primitive), &'a DIType>>,
66-
type_name_cache: RefCell<FxHashMap<(Ty<'tcx>, bool), String>>,
67+
type_name_cache: RefCell<FxHashMap<(Ty<'tcx>, bool), Rc<str>>>,
6768

6869
type_map: RefCell<TypeMap<'a, 'tcx>>,
6970
namespace_map: RefCell<DefIdMap<&'a DIScope>>,

compiler/rustc_codegen_ssa/src/debuginfo/type_names.rs

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ use rustc_target::abi::{Integer, TagEncoding, Variants};
2424
use smallvec::SmallVec;
2525

2626
use std::fmt::Write;
27+
use std::rc::Rc;
2728

2829
// Compute the name of the type as it should be stored in debuginfo. Does not do
2930
// any caching, i.e., calling the function twice with the same type will also do
@@ -33,14 +34,18 @@ pub fn compute_debuginfo_type_name<'tcx>(
3334
tcx: TyCtxt<'tcx>,
3435
t: Ty<'tcx>,
3536
qualified: bool,
36-
type_name_cache: &mut FxHashMap<(Ty<'tcx>, bool), String>,
37+
type_name_cache: &mut FxHashMap<(Ty<'tcx>, bool), Rc<str>>,
3738
) -> String {
3839
let _prof = tcx.prof.generic_activity("compute_debuginfo_type_name");
3940

41+
if let Some(type_name) = type_name_cache.get(&(t, qualified)) {
42+
return String::from(&type_name[..]);
43+
}
44+
4045
let mut result = String::with_capacity(64);
4146
let mut visited = FxHashSet::default();
4247
push_debuginfo_type_name(tcx, t, qualified, &mut result, &mut visited, type_name_cache);
43-
if type_name_cache.insert((t, qualified), result.clone()).is_some() {
48+
if type_name_cache.insert((t, qualified), Rc::from(&*result)).is_some() {
4449
bug!("type name is already in the type name cache!");
4550
}
4651
result
@@ -54,9 +59,8 @@ fn push_debuginfo_type_name<'tcx>(
5459
qualified: bool,
5560
output: &mut String,
5661
visited: &mut FxHashSet<Ty<'tcx>>,
57-
type_name_cache: &mut FxHashMap<(Ty<'tcx>, bool), String>,
62+
type_name_cache: &mut FxHashMap<(Ty<'tcx>, bool), Rc<str>>,
5863
) {
59-
// Check if we have seen this type and qualifier before.
6064
if let Some(type_name) = type_name_cache.get(&(t, qualified)) {
6165
output.push_str(&type_name[..]);
6266
return;
@@ -437,7 +441,7 @@ fn push_debuginfo_type_name<'tcx>(
437441
substs: SubstsRef<'tcx>,
438442
output: &mut String,
439443
visited: &mut FxHashSet<Ty<'tcx>>,
440-
type_name_cache: &mut FxHashMap<(Ty<'tcx>, bool), String>,
444+
type_name_cache: &mut FxHashMap<(Ty<'tcx>, bool), Rc<str>>,
441445
) {
442446
let layout = tcx.layout_of(tcx.param_env(def.did).and(ty)).expect("layout error");
443447

@@ -555,7 +559,7 @@ fn push_generic_params_internal<'tcx>(
555559
substs: SubstsRef<'tcx>,
556560
output: &mut String,
557561
visited: &mut FxHashSet<Ty<'tcx>>,
558-
type_name_cache: &mut FxHashMap<(Ty<'tcx>, bool), String>,
562+
type_name_cache: &mut FxHashMap<(Ty<'tcx>, bool), Rc<str>>,
559563
) -> bool {
560564
if substs.non_erasable_generics().next().is_none() {
561565
return false;
@@ -642,7 +646,7 @@ pub fn push_generic_params<'tcx>(
642646
tcx: TyCtxt<'tcx>,
643647
substs: SubstsRef<'tcx>,
644648
output: &mut String,
645-
type_name_cache: &mut FxHashMap<(Ty<'tcx>, bool), String>,
649+
type_name_cache: &mut FxHashMap<(Ty<'tcx>, bool), Rc<str>>,
646650
) {
647651
let _prof = tcx.prof.generic_activity("compute_debuginfo_type_name");
648652
let mut visited = FxHashSet::default();

0 commit comments

Comments
 (0)