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.
This commit is contained in:
commit
1757d6b34d
1 changed files with 32 additions and 45 deletions
|
@ -23,15 +23,12 @@ pub(super) fn mangle(
|
||||||
let substs = tcx.normalize_erasing_regions(ty::ParamEnv::reveal_all(), instance.substs);
|
let substs = tcx.normalize_erasing_regions(ty::ParamEnv::reveal_all(), instance.substs);
|
||||||
|
|
||||||
let prefix = "_R";
|
let prefix = "_R";
|
||||||
let mut cx = SymbolMangler {
|
let mut cx = &mut SymbolMangler {
|
||||||
tcx,
|
tcx,
|
||||||
compress: Some(Box::new(CompressionCaches {
|
|
||||||
start_offset: prefix.len(),
|
start_offset: prefix.len(),
|
||||||
|
|
||||||
paths: FxHashMap::default(),
|
paths: FxHashMap::default(),
|
||||||
types: FxHashMap::default(),
|
types: FxHashMap::default(),
|
||||||
consts: FxHashMap::default(),
|
consts: FxHashMap::default(),
|
||||||
})),
|
|
||||||
binders: vec![],
|
binders: vec![],
|
||||||
out: String::from(prefix),
|
out: String::from(prefix),
|
||||||
};
|
};
|
||||||
|
@ -52,17 +49,7 @@ pub(super) fn mangle(
|
||||||
if let Some(instantiating_crate) = instantiating_crate {
|
if let Some(instantiating_crate) = instantiating_crate {
|
||||||
cx = cx.print_def_path(instantiating_crate.as_def_id(), &[]).unwrap();
|
cx = cx.print_def_path(instantiating_crate.as_def_id(), &[]).unwrap();
|
||||||
}
|
}
|
||||||
cx.out
|
std::mem::take(&mut cx.out)
|
||||||
}
|
|
||||||
|
|
||||||
struct CompressionCaches<'tcx> {
|
|
||||||
// The length of the prefix in `out` (e.g. 2 for `_R`).
|
|
||||||
start_offset: usize,
|
|
||||||
|
|
||||||
// The values are start positions in `out`, in bytes.
|
|
||||||
paths: FxHashMap<(DefId, &'tcx [GenericArg<'tcx>]), usize>,
|
|
||||||
types: FxHashMap<Ty<'tcx>, usize>,
|
|
||||||
consts: FxHashMap<&'tcx ty::Const<'tcx>, usize>,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
struct BinderLevel {
|
struct BinderLevel {
|
||||||
|
@ -81,9 +68,15 @@ struct BinderLevel {
|
||||||
|
|
||||||
struct SymbolMangler<'tcx> {
|
struct SymbolMangler<'tcx> {
|
||||||
tcx: TyCtxt<'tcx>,
|
tcx: TyCtxt<'tcx>,
|
||||||
compress: Option<Box<CompressionCaches<'tcx>>>,
|
|
||||||
binders: Vec<BinderLevel>,
|
binders: Vec<BinderLevel>,
|
||||||
out: String,
|
out: String,
|
||||||
|
|
||||||
|
/// The length of the prefix in `out` (e.g. 2 for `_R`).
|
||||||
|
start_offset: usize,
|
||||||
|
/// The values are start positions in `out`, in bytes.
|
||||||
|
paths: FxHashMap<(DefId, &'tcx [GenericArg<'tcx>]), usize>,
|
||||||
|
types: FxHashMap<Ty<'tcx>, usize>,
|
||||||
|
consts: FxHashMap<&'tcx ty::Const<'tcx>, usize>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl SymbolMangler<'tcx> {
|
impl SymbolMangler<'tcx> {
|
||||||
|
@ -160,13 +153,13 @@ impl SymbolMangler<'tcx> {
|
||||||
self.push(ident);
|
self.push(ident);
|
||||||
}
|
}
|
||||||
|
|
||||||
fn path_append_ns(
|
fn path_append_ns<'a>(
|
||||||
mut self,
|
mut self: &'a mut Self,
|
||||||
print_prefix: impl FnOnce(Self) -> Result<Self, !>,
|
print_prefix: impl FnOnce(&'a mut Self) -> Result<&'a mut Self, !>,
|
||||||
ns: char,
|
ns: char,
|
||||||
disambiguator: u64,
|
disambiguator: u64,
|
||||||
name: &str,
|
name: &str,
|
||||||
) -> Result<Self, !> {
|
) -> Result<&'a mut Self, !> {
|
||||||
self.push("N");
|
self.push("N");
|
||||||
self.out.push(ns);
|
self.out.push(ns);
|
||||||
self = print_prefix(self)?;
|
self = print_prefix(self)?;
|
||||||
|
@ -175,17 +168,17 @@ impl SymbolMangler<'tcx> {
|
||||||
Ok(self)
|
Ok(self)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn print_backref(mut self, i: usize) -> Result<Self, !> {
|
fn print_backref(&mut self, i: usize) -> Result<&mut Self, !> {
|
||||||
self.push("B");
|
self.push("B");
|
||||||
self.push_integer_62((i - self.compress.as_ref().unwrap().start_offset) as u64);
|
self.push_integer_62((i - self.start_offset) as u64);
|
||||||
Ok(self)
|
Ok(self)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn in_binder<T>(
|
fn in_binder<'a, T>(
|
||||||
mut self,
|
mut self: &'a mut Self,
|
||||||
value: &ty::Binder<'tcx, T>,
|
value: &ty::Binder<'tcx, T>,
|
||||||
print_value: impl FnOnce(Self, &T) -> Result<Self, !>,
|
print_value: impl FnOnce(&'a mut Self, &T) -> Result<&'a mut Self, !>,
|
||||||
) -> Result<Self, !>
|
) -> Result<&'a mut Self, !>
|
||||||
where
|
where
|
||||||
T: TypeFoldable<'tcx>,
|
T: TypeFoldable<'tcx>,
|
||||||
{
|
{
|
||||||
|
@ -218,7 +211,7 @@ impl SymbolMangler<'tcx> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Printer<'tcx> for SymbolMangler<'tcx> {
|
impl Printer<'tcx> for &mut SymbolMangler<'tcx> {
|
||||||
type Error = !;
|
type Error = !;
|
||||||
|
|
||||||
type Path = Self;
|
type Path = Self;
|
||||||
|
@ -236,7 +229,7 @@ impl Printer<'tcx> for SymbolMangler<'tcx> {
|
||||||
def_id: DefId,
|
def_id: DefId,
|
||||||
substs: &'tcx [GenericArg<'tcx>],
|
substs: &'tcx [GenericArg<'tcx>],
|
||||||
) -> Result<Self::Path, Self::Error> {
|
) -> Result<Self::Path, Self::Error> {
|
||||||
if let Some(&i) = self.compress.as_ref().and_then(|c| c.paths.get(&(def_id, substs))) {
|
if let Some(&i) = self.paths.get(&(def_id, substs)) {
|
||||||
return self.print_backref(i);
|
return self.print_backref(i);
|
||||||
}
|
}
|
||||||
let start = self.out.len();
|
let start = self.out.len();
|
||||||
|
@ -246,9 +239,7 @@ impl Printer<'tcx> for SymbolMangler<'tcx> {
|
||||||
// Only cache paths that do not refer to an enclosing
|
// Only cache paths that do not refer to an enclosing
|
||||||
// binder (which would change depending on context).
|
// binder (which would change depending on context).
|
||||||
if !substs.iter().any(|k| k.has_escaping_bound_vars()) {
|
if !substs.iter().any(|k| k.has_escaping_bound_vars()) {
|
||||||
if let Some(c) = &mut self.compress {
|
self.paths.insert((def_id, substs), start);
|
||||||
c.paths.insert((def_id, substs), start);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
Ok(self)
|
Ok(self)
|
||||||
}
|
}
|
||||||
|
@ -312,7 +303,7 @@ impl Printer<'tcx> for SymbolMangler<'tcx> {
|
||||||
Ok(self)
|
Ok(self)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn print_region(mut self, region: ty::Region<'_>) -> Result<Self::Region, Self::Error> {
|
fn print_region(self, region: ty::Region<'_>) -> Result<Self::Region, Self::Error> {
|
||||||
let i = match *region {
|
let i = match *region {
|
||||||
// Erased lifetimes use the index 0, for a
|
// Erased lifetimes use the index 0, for a
|
||||||
// shorter mangling of `L_`.
|
// shorter mangling of `L_`.
|
||||||
|
@ -367,7 +358,7 @@ impl Printer<'tcx> for SymbolMangler<'tcx> {
|
||||||
return Ok(self);
|
return Ok(self);
|
||||||
}
|
}
|
||||||
|
|
||||||
if let Some(&i) = self.compress.as_ref().and_then(|c| c.types.get(&ty)) {
|
if let Some(&i) = self.types.get(&ty) {
|
||||||
return self.print_backref(i);
|
return self.print_backref(i);
|
||||||
}
|
}
|
||||||
let start = self.out.len();
|
let start = self.out.len();
|
||||||
|
@ -476,9 +467,7 @@ impl Printer<'tcx> for SymbolMangler<'tcx> {
|
||||||
// Only cache types that do not refer to an enclosing
|
// Only cache types that do not refer to an enclosing
|
||||||
// binder (which would change depending on context).
|
// binder (which would change depending on context).
|
||||||
if !ty.has_escaping_bound_vars() {
|
if !ty.has_escaping_bound_vars() {
|
||||||
if let Some(c) = &mut self.compress {
|
self.types.insert(ty, start);
|
||||||
c.types.insert(ty, start);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
Ok(self)
|
Ok(self)
|
||||||
}
|
}
|
||||||
|
@ -545,7 +534,7 @@ impl Printer<'tcx> for SymbolMangler<'tcx> {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn print_const(mut self, ct: &'tcx ty::Const<'tcx>) -> Result<Self::Const, Self::Error> {
|
fn print_const(mut self, ct: &'tcx ty::Const<'tcx>) -> Result<Self::Const, Self::Error> {
|
||||||
if let Some(&i) = self.compress.as_ref().and_then(|c| c.consts.get(&ct)) {
|
if let Some(&i) = self.consts.get(&ct) {
|
||||||
return self.print_backref(i);
|
return self.print_backref(i);
|
||||||
}
|
}
|
||||||
let start = self.out.len();
|
let start = self.out.len();
|
||||||
|
@ -583,14 +572,12 @@ impl Printer<'tcx> for SymbolMangler<'tcx> {
|
||||||
// Only cache consts that do not refer to an enclosing
|
// Only cache consts that do not refer to an enclosing
|
||||||
// binder (which would change depending on context).
|
// binder (which would change depending on context).
|
||||||
if !ct.has_escaping_bound_vars() {
|
if !ct.has_escaping_bound_vars() {
|
||||||
if let Some(c) = &mut self.compress {
|
self.consts.insert(ct, start);
|
||||||
c.consts.insert(ct, start);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
Ok(self)
|
Ok(self)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn path_crate(mut self, cnum: CrateNum) -> Result<Self::Path, Self::Error> {
|
fn path_crate(self, cnum: CrateNum) -> Result<Self::Path, Self::Error> {
|
||||||
self.push("C");
|
self.push("C");
|
||||||
let stable_crate_id = self.tcx.def_path_hash(cnum.as_def_id()).stable_crate_id();
|
let stable_crate_id = self.tcx.def_path_hash(cnum.as_def_id()).stable_crate_id();
|
||||||
self.push_disambiguator(stable_crate_id.to_u64());
|
self.push_disambiguator(stable_crate_id.to_u64());
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue