Skip to content

Commit 1b16242

Browse files
committed
[HACK] rustc_codegen_utils: try to reduce overhead of symbol mangling checks.
1 parent 07800ed commit 1b16242

File tree

3 files changed

+53
-55
lines changed

3 files changed

+53
-55
lines changed

src/librustc_codegen_utils/symbol_names.rs

+6-7
Original file line numberDiff line numberDiff line change
@@ -293,10 +293,13 @@ fn compute_symbol_name<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, instance: Instance
293293
return tcx.item_name(def_id).to_string();
294294
}
295295

296-
compute_mangled_symbol_name(tcx, instance)
296+
dump::record(tcx, instance)
297297
}
298298

299-
fn compute_mangled_symbol_name(tcx: TyCtxt<'_, 'tcx, 'tcx>, instance: Instance<'tcx>) -> String {
299+
fn compute_old_mangled_symbol_name(
300+
tcx: TyCtxt<'_, 'tcx, 'tcx>,
301+
instance: Instance<'tcx>,
302+
) -> (String, u64) {
300303
let def_id = instance.def_id();
301304

302305
// We want to compute the "type" of this item. Unfortunately, some
@@ -340,11 +343,7 @@ fn compute_mangled_symbol_name(tcx: TyCtxt<'_, 'tcx, 'tcx>, instance: Instance<'
340343
sanitize(&mut buf.temp_buf, "{{vtable-shim}}");
341344
}
342345

343-
let mangled_symbol = buf.finish(hash);
344-
345-
return dump::record(tcx, instance, &mangled_symbol, hash);
346-
347-
// mangled_symbol
346+
(buf.finish(hash), hash)
348347
}
349348

350349
// Follow C++ namespace-mangling style, see

src/librustc_codegen_utils/symbol_names/dump.rs

+32-29
Original file line numberDiff line numberDiff line change
@@ -22,38 +22,15 @@ thread_local!(static OUT_DIR: Option<PathBuf> = {
2222
});
2323
thread_local!(static OUTPUT: RefCell<Option<File>> = RefCell::new(None));
2424

25-
pub fn record(
26-
tcx: TyCtxt<'_, 'tcx, 'tcx>,
27-
instance: Instance<'tcx>,
28-
old_mangling: &str,
29-
old_hash: u64,
30-
) -> String {
25+
pub fn record(tcx: TyCtxt<'_, 'tcx, 'tcx>, instance: Instance<'tcx>) -> String {
3126
let header = "old+generics,old,mw,mw+compression,new,new+compression";
3227

33-
// Always compute all forms of mangling.
3428
let def_id = instance.def_id();
3529
// FIXME(eddyb) this should ideally not be needed.
3630
let substs =
3731
tcx.normalize_erasing_regions(ty::ParamEnv::reveal_all(), instance.substs);
3832

39-
let old_mangling_plus_generics = {
40-
let mut symbol_path = old::SymbolPrinter {
41-
tcx,
42-
path: old::SymbolPath::new(),
43-
keep_within_component: false,
44-
}.print_def_path(def_id, substs).unwrap().path;
45-
46-
if instance.is_vtable_shim() {
47-
symbol_path.finalize_pending_component();
48-
old::sanitize(&mut symbol_path.temp_buf, "{{vtable-shim}}");
49-
}
50-
51-
symbol_path.finish(old_hash)
52-
};
53-
54-
let (mw_mangling, mw_mangling_plus_compression) = mw::mangle(tcx, instance)
55-
.unwrap_or((String::new(), String::new()));
56-
let (new_mangling, new_mangling_plus_compression) = new::mangle(tcx, instance);
33+
let new_mangling_plus_compression = new::mangle(tcx, instance, true);
5734

5835
OUTPUT.with(|out| {
5936
let mut out = out.borrow_mut();
@@ -93,6 +70,29 @@ pub fn record(
9370
}
9471

9572
if let Some(out) = out.as_mut() {
73+
let (old_mangling, old_hash) =
74+
old::compute_old_mangled_symbol_name(tcx, instance);
75+
76+
let old_mangling_plus_generics = {
77+
let mut symbol_path = old::SymbolPrinter {
78+
tcx,
79+
path: old::SymbolPath::new(),
80+
keep_within_component: false,
81+
}.print_def_path(def_id, substs).unwrap().path;
82+
83+
if instance.is_vtable_shim() {
84+
symbol_path.finalize_pending_component();
85+
old::sanitize(&mut symbol_path.temp_buf, "{{vtable-shim}}");
86+
}
87+
88+
symbol_path.finish(old_hash)
89+
};
90+
91+
let (mw_mangling, mw_mangling_plus_compression) = mw::mangle(tcx, instance)
92+
.unwrap_or((String::new(), String::new()));
93+
94+
let new_mangling = new::mangle(tcx, instance, false);
95+
9696
writeln!(out, "{},{},{},{},{},{}",
9797
old_mangling_plus_generics,
9898
old_mangling,
@@ -124,17 +124,20 @@ pub fn record(
124124
cx.print_def_path(def_id, substs).unwrap().out
125125
}
126126
};
127-
let expected_demangling_alt = make_expected_demangling(true);
127+
128+
// HACK(eddyb) some parts are commented out to reduce check overhead:
129+
130+
// let expected_demangling_alt = make_expected_demangling(true);
128131
let expected_demangling = make_expected_demangling(false);
129132

130-
for mangling in &[&new_mangling, &new_mangling_plus_compression] {
133+
for mangling in &[/*&new_mangling,*/ &new_mangling_plus_compression] {
131134
match rustc_demangle::try_demangle(mangling) {
132135
Ok(demangling) => {
133-
let demangling_alt = format!("{:#}", demangling);
136+
/*let demangling_alt = format!("{:#}", demangling);
134137
if demangling_alt.contains('?') {
135138
bug!("demangle(alt) printing failed for {:?}\n{:?}", mangling, demangling_alt);
136139
}
137-
assert_eq!(demangling_alt, expected_demangling_alt);
140+
assert_eq!(demangling_alt, expected_demangling_alt);*/
138141

139142
let demangling = format!("{}", demangling);
140143
if demangling.contains('?') {

src/librustc_codegen_utils/symbol_names/new.rs

+15-19
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ use std::ops::Range;
1616
pub(super) fn mangle(
1717
tcx: TyCtxt<'_, 'tcx, 'tcx>,
1818
instance: Instance<'tcx>,
19-
) -> (String, String) {
19+
compress: bool,
20+
) -> String {
2021
let def_id = instance.def_id();
2122
// FIXME(eddyb) this should ideally not be needed.
2223
let substs =
@@ -41,27 +42,22 @@ pub(super) fn mangle(
4142

4243
let prefix = "_R";
4344

44-
let uncompressed = print_symbol(SymbolMangler {
45+
print_symbol(SymbolMangler {
4546
tcx,
46-
compress: None,
47-
binders: vec![],
48-
out: String::from(prefix),
49-
});
50-
51-
let compressed = print_symbol(SymbolMangler {
52-
tcx,
53-
compress: Some(Box::new(CompressionCaches {
54-
start_offset: prefix.len(),
55-
56-
paths: FxHashMap::default(),
57-
types: FxHashMap::default(),
58-
consts: FxHashMap::default(),
59-
})),
47+
compress: if compress {
48+
Some(Box::new(CompressionCaches {
49+
start_offset: prefix.len(),
50+
51+
paths: FxHashMap::default(),
52+
types: FxHashMap::default(),
53+
consts: FxHashMap::default(),
54+
}))
55+
} else {
56+
None
57+
},
6058
binders: vec![],
6159
out: String::from(prefix),
62-
});
63-
64-
(uncompressed, compressed)
60+
})
6561
}
6662

6763
struct CompressionCaches<'tcx> {

0 commit comments

Comments
 (0)