Skip to content

Commit 1757d6b

Browse files
authored
Rollup merge of #87583 - tmiasko:compression-cache, r=wesleywiser
Refactor compression cache in v0 symbol mangler * Remove redundant option around compression caches (they are always present). * Flatten compression caches into symbol mangler to avoid dynamic memory allocation. * Implement printer for `&mut SymbolMangler` instead of `SymbolMangler` to avoid passing now slightly larger symbol mangler by value.
2 parents 3bc6c28 + 0eabbf8 commit 1757d6b

File tree

1 file changed

+32
-45
lines changed
  • compiler/rustc_symbol_mangling/src

1 file changed

+32
-45
lines changed

compiler/rustc_symbol_mangling/src/v0.rs

+32-45
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,12 @@ pub(super) fn mangle(
2323
let substs = tcx.normalize_erasing_regions(ty::ParamEnv::reveal_all(), instance.substs);
2424

2525
let prefix = "_R";
26-
let mut cx = SymbolMangler {
26+
let mut cx = &mut SymbolMangler {
2727
tcx,
28-
compress: Some(Box::new(CompressionCaches {
29-
start_offset: prefix.len(),
30-
31-
paths: FxHashMap::default(),
32-
types: FxHashMap::default(),
33-
consts: FxHashMap::default(),
34-
})),
28+
start_offset: prefix.len(),
29+
paths: FxHashMap::default(),
30+
types: FxHashMap::default(),
31+
consts: FxHashMap::default(),
3532
binders: vec![],
3633
out: String::from(prefix),
3734
};
@@ -52,17 +49,7 @@ pub(super) fn mangle(
5249
if let Some(instantiating_crate) = instantiating_crate {
5350
cx = cx.print_def_path(instantiating_crate.as_def_id(), &[]).unwrap();
5451
}
55-
cx.out
56-
}
57-
58-
struct CompressionCaches<'tcx> {
59-
// The length of the prefix in `out` (e.g. 2 for `_R`).
60-
start_offset: usize,
61-
62-
// The values are start positions in `out`, in bytes.
63-
paths: FxHashMap<(DefId, &'tcx [GenericArg<'tcx>]), usize>,
64-
types: FxHashMap<Ty<'tcx>, usize>,
65-
consts: FxHashMap<&'tcx ty::Const<'tcx>, usize>,
52+
std::mem::take(&mut cx.out)
6653
}
6754

6855
struct BinderLevel {
@@ -81,9 +68,15 @@ struct BinderLevel {
8168

8269
struct SymbolMangler<'tcx> {
8370
tcx: TyCtxt<'tcx>,
84-
compress: Option<Box<CompressionCaches<'tcx>>>,
8571
binders: Vec<BinderLevel>,
8672
out: String,
73+
74+
/// The length of the prefix in `out` (e.g. 2 for `_R`).
75+
start_offset: usize,
76+
/// The values are start positions in `out`, in bytes.
77+
paths: FxHashMap<(DefId, &'tcx [GenericArg<'tcx>]), usize>,
78+
types: FxHashMap<Ty<'tcx>, usize>,
79+
consts: FxHashMap<&'tcx ty::Const<'tcx>, usize>,
8780
}
8881

8982
impl SymbolMangler<'tcx> {
@@ -160,13 +153,13 @@ impl SymbolMangler<'tcx> {
160153
self.push(ident);
161154
}
162155

163-
fn path_append_ns(
164-
mut self,
165-
print_prefix: impl FnOnce(Self) -> Result<Self, !>,
156+
fn path_append_ns<'a>(
157+
mut self: &'a mut Self,
158+
print_prefix: impl FnOnce(&'a mut Self) -> Result<&'a mut Self, !>,
166159
ns: char,
167160
disambiguator: u64,
168161
name: &str,
169-
) -> Result<Self, !> {
162+
) -> Result<&'a mut Self, !> {
170163
self.push("N");
171164
self.out.push(ns);
172165
self = print_prefix(self)?;
@@ -175,17 +168,17 @@ impl SymbolMangler<'tcx> {
175168
Ok(self)
176169
}
177170

178-
fn print_backref(mut self, i: usize) -> Result<Self, !> {
171+
fn print_backref(&mut self, i: usize) -> Result<&mut Self, !> {
179172
self.push("B");
180-
self.push_integer_62((i - self.compress.as_ref().unwrap().start_offset) as u64);
173+
self.push_integer_62((i - self.start_offset) as u64);
181174
Ok(self)
182175
}
183176

184-
fn in_binder<T>(
185-
mut self,
177+
fn in_binder<'a, T>(
178+
mut self: &'a mut Self,
186179
value: &ty::Binder<'tcx, T>,
187-
print_value: impl FnOnce(Self, &T) -> Result<Self, !>,
188-
) -> Result<Self, !>
180+
print_value: impl FnOnce(&'a mut Self, &T) -> Result<&'a mut Self, !>,
181+
) -> Result<&'a mut Self, !>
189182
where
190183
T: TypeFoldable<'tcx>,
191184
{
@@ -218,7 +211,7 @@ impl SymbolMangler<'tcx> {
218211
}
219212
}
220213

221-
impl Printer<'tcx> for SymbolMangler<'tcx> {
214+
impl Printer<'tcx> for &mut SymbolMangler<'tcx> {
222215
type Error = !;
223216

224217
type Path = Self;
@@ -236,7 +229,7 @@ impl Printer<'tcx> for SymbolMangler<'tcx> {
236229
def_id: DefId,
237230
substs: &'tcx [GenericArg<'tcx>],
238231
) -> Result<Self::Path, Self::Error> {
239-
if let Some(&i) = self.compress.as_ref().and_then(|c| c.paths.get(&(def_id, substs))) {
232+
if let Some(&i) = self.paths.get(&(def_id, substs)) {
240233
return self.print_backref(i);
241234
}
242235
let start = self.out.len();
@@ -246,9 +239,7 @@ impl Printer<'tcx> for SymbolMangler<'tcx> {
246239
// Only cache paths that do not refer to an enclosing
247240
// binder (which would change depending on context).
248241
if !substs.iter().any(|k| k.has_escaping_bound_vars()) {
249-
if let Some(c) = &mut self.compress {
250-
c.paths.insert((def_id, substs), start);
251-
}
242+
self.paths.insert((def_id, substs), start);
252243
}
253244
Ok(self)
254245
}
@@ -312,7 +303,7 @@ impl Printer<'tcx> for SymbolMangler<'tcx> {
312303
Ok(self)
313304
}
314305

315-
fn print_region(mut self, region: ty::Region<'_>) -> Result<Self::Region, Self::Error> {
306+
fn print_region(self, region: ty::Region<'_>) -> Result<Self::Region, Self::Error> {
316307
let i = match *region {
317308
// Erased lifetimes use the index 0, for a
318309
// shorter mangling of `L_`.
@@ -367,7 +358,7 @@ impl Printer<'tcx> for SymbolMangler<'tcx> {
367358
return Ok(self);
368359
}
369360

370-
if let Some(&i) = self.compress.as_ref().and_then(|c| c.types.get(&ty)) {
361+
if let Some(&i) = self.types.get(&ty) {
371362
return self.print_backref(i);
372363
}
373364
let start = self.out.len();
@@ -476,9 +467,7 @@ impl Printer<'tcx> for SymbolMangler<'tcx> {
476467
// Only cache types that do not refer to an enclosing
477468
// binder (which would change depending on context).
478469
if !ty.has_escaping_bound_vars() {
479-
if let Some(c) = &mut self.compress {
480-
c.types.insert(ty, start);
481-
}
470+
self.types.insert(ty, start);
482471
}
483472
Ok(self)
484473
}
@@ -545,7 +534,7 @@ impl Printer<'tcx> for SymbolMangler<'tcx> {
545534
}
546535

547536
fn print_const(mut self, ct: &'tcx ty::Const<'tcx>) -> Result<Self::Const, Self::Error> {
548-
if let Some(&i) = self.compress.as_ref().and_then(|c| c.consts.get(&ct)) {
537+
if let Some(&i) = self.consts.get(&ct) {
549538
return self.print_backref(i);
550539
}
551540
let start = self.out.len();
@@ -583,14 +572,12 @@ impl Printer<'tcx> for SymbolMangler<'tcx> {
583572
// Only cache consts that do not refer to an enclosing
584573
// binder (which would change depending on context).
585574
if !ct.has_escaping_bound_vars() {
586-
if let Some(c) = &mut self.compress {
587-
c.consts.insert(ct, start);
588-
}
575+
self.consts.insert(ct, start);
589576
}
590577
Ok(self)
591578
}
592579

593-
fn path_crate(mut self, cnum: CrateNum) -> Result<Self::Path, Self::Error> {
580+
fn path_crate(self, cnum: CrateNum) -> Result<Self::Path, Self::Error> {
594581
self.push("C");
595582
let stable_crate_id = self.tcx.def_path_hash(cnum.as_def_id()).stable_crate_id();
596583
self.push_disambiguator(stable_crate_id.to_u64());

0 commit comments

Comments
 (0)