From 8307072edf74f68f89e736d242d32b17eeb385d7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tomasz=20Mi=C4=85sko?= Date: Tue, 27 Jul 2021 00:00:00 +0000 Subject: [PATCH 1/3] Remove redundant option around compression caches Compression caches are always present. Remove unnecessary option. --- compiler/rustc_symbol_mangling/src/v0.rs | 26 +++++++++--------------- 1 file changed, 10 insertions(+), 16 deletions(-) diff --git a/compiler/rustc_symbol_mangling/src/v0.rs b/compiler/rustc_symbol_mangling/src/v0.rs index 14442806fc0b7..8359bba3079e5 100644 --- a/compiler/rustc_symbol_mangling/src/v0.rs +++ b/compiler/rustc_symbol_mangling/src/v0.rs @@ -25,13 +25,13 @@ pub(super) fn mangle( let prefix = "_R"; let mut cx = SymbolMangler { tcx, - compress: Some(Box::new(CompressionCaches { + compress: Box::new(CompressionCaches { start_offset: prefix.len(), paths: FxHashMap::default(), types: FxHashMap::default(), consts: FxHashMap::default(), - })), + }), binders: vec![], out: String::from(prefix), }; @@ -81,7 +81,7 @@ struct BinderLevel { struct SymbolMangler<'tcx> { tcx: TyCtxt<'tcx>, - compress: Option>>, + compress: Box>, binders: Vec, out: String, } @@ -177,7 +177,7 @@ impl SymbolMangler<'tcx> { fn print_backref(mut self, i: usize) -> Result { self.push("B"); - self.push_integer_62((i - self.compress.as_ref().unwrap().start_offset) as u64); + self.push_integer_62((i - self.compress.start_offset) as u64); Ok(self) } @@ -236,7 +236,7 @@ impl Printer<'tcx> for SymbolMangler<'tcx> { def_id: DefId, substs: &'tcx [GenericArg<'tcx>], ) -> Result { - if let Some(&i) = self.compress.as_ref().and_then(|c| c.paths.get(&(def_id, substs))) { + if let Some(&i) = self.compress.paths.get(&(def_id, substs)) { return self.print_backref(i); } let start = self.out.len(); @@ -246,9 +246,7 @@ impl Printer<'tcx> for SymbolMangler<'tcx> { // Only cache paths that do not refer to an enclosing // binder (which would change depending on context). if !substs.iter().any(|k| k.has_escaping_bound_vars()) { - if let Some(c) = &mut self.compress { - c.paths.insert((def_id, substs), start); - } + self.compress.paths.insert((def_id, substs), start); } Ok(self) } @@ -367,7 +365,7 @@ impl Printer<'tcx> for SymbolMangler<'tcx> { return Ok(self); } - if let Some(&i) = self.compress.as_ref().and_then(|c| c.types.get(&ty)) { + if let Some(&i) = self.compress.types.get(&ty) { return self.print_backref(i); } let start = self.out.len(); @@ -476,9 +474,7 @@ impl Printer<'tcx> for SymbolMangler<'tcx> { // Only cache types that do not refer to an enclosing // binder (which would change depending on context). if !ty.has_escaping_bound_vars() { - if let Some(c) = &mut self.compress { - c.types.insert(ty, start); - } + self.compress.types.insert(ty, start); } Ok(self) } @@ -545,7 +541,7 @@ impl Printer<'tcx> for SymbolMangler<'tcx> { } fn print_const(mut self, ct: &'tcx ty::Const<'tcx>) -> Result { - if let Some(&i) = self.compress.as_ref().and_then(|c| c.consts.get(&ct)) { + if let Some(&i) = self.compress.consts.get(&ct) { return self.print_backref(i); } let start = self.out.len(); @@ -583,9 +579,7 @@ impl Printer<'tcx> for SymbolMangler<'tcx> { // Only cache consts that do not refer to an enclosing // binder (which would change depending on context). if !ct.has_escaping_bound_vars() { - if let Some(c) = &mut self.compress { - c.consts.insert(ct, start); - } + self.compress.consts.insert(ct, start); } Ok(self) } From 0ce8001a473e049a74f424d765b90f83f5dc76c4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tomasz=20Mi=C4=85sko?= Date: Thu, 29 Jul 2021 00:00:00 +0000 Subject: [PATCH 2/3] Flatten compression caches into symbol mangler The compression caches currently don't have any dedicated functionality that would benefit from being separated. Incorporating caches directly into the symbol manger also avoids dynamic memory allocation. The symbol mangler, which is often passed by value, is now slightly larger. This aspect will be addressed by a follow-up commit. --- compiler/rustc_symbol_mangling/src/v0.rs | 43 ++++++++++-------------- 1 file changed, 18 insertions(+), 25 deletions(-) diff --git a/compiler/rustc_symbol_mangling/src/v0.rs b/compiler/rustc_symbol_mangling/src/v0.rs index 8359bba3079e5..6a1360da4ee9e 100644 --- a/compiler/rustc_symbol_mangling/src/v0.rs +++ b/compiler/rustc_symbol_mangling/src/v0.rs @@ -25,13 +25,10 @@ pub(super) fn mangle( let prefix = "_R"; let mut cx = SymbolMangler { tcx, - compress: Box::new(CompressionCaches { - start_offset: prefix.len(), - - paths: FxHashMap::default(), - types: FxHashMap::default(), - consts: FxHashMap::default(), - }), + start_offset: prefix.len(), + paths: FxHashMap::default(), + types: FxHashMap::default(), + consts: FxHashMap::default(), binders: vec![], out: String::from(prefix), }; @@ -55,16 +52,6 @@ pub(super) fn mangle( 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, usize>, - consts: FxHashMap<&'tcx ty::Const<'tcx>, usize>, -} - struct BinderLevel { /// The range of distances from the root of what's /// being printed, to the lifetimes in a binder. @@ -81,9 +68,15 @@ struct BinderLevel { struct SymbolMangler<'tcx> { tcx: TyCtxt<'tcx>, - compress: Box>, binders: Vec, 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, usize>, + consts: FxHashMap<&'tcx ty::Const<'tcx>, usize>, } impl SymbolMangler<'tcx> { @@ -177,7 +170,7 @@ impl SymbolMangler<'tcx> { fn print_backref(mut self, i: usize) -> Result { self.push("B"); - self.push_integer_62((i - self.compress.start_offset) as u64); + self.push_integer_62((i - self.start_offset) as u64); Ok(self) } @@ -236,7 +229,7 @@ impl Printer<'tcx> for SymbolMangler<'tcx> { def_id: DefId, substs: &'tcx [GenericArg<'tcx>], ) -> Result { - if let Some(&i) = self.compress.paths.get(&(def_id, substs)) { + if let Some(&i) = self.paths.get(&(def_id, substs)) { return self.print_backref(i); } let start = self.out.len(); @@ -246,7 +239,7 @@ impl Printer<'tcx> for SymbolMangler<'tcx> { // Only cache paths that do not refer to an enclosing // binder (which would change depending on context). if !substs.iter().any(|k| k.has_escaping_bound_vars()) { - self.compress.paths.insert((def_id, substs), start); + self.paths.insert((def_id, substs), start); } Ok(self) } @@ -365,7 +358,7 @@ impl Printer<'tcx> for SymbolMangler<'tcx> { return Ok(self); } - if let Some(&i) = self.compress.types.get(&ty) { + if let Some(&i) = self.types.get(&ty) { return self.print_backref(i); } let start = self.out.len(); @@ -474,7 +467,7 @@ impl Printer<'tcx> for SymbolMangler<'tcx> { // Only cache types that do not refer to an enclosing // binder (which would change depending on context). if !ty.has_escaping_bound_vars() { - self.compress.types.insert(ty, start); + self.types.insert(ty, start); } Ok(self) } @@ -541,7 +534,7 @@ impl Printer<'tcx> for SymbolMangler<'tcx> { } fn print_const(mut self, ct: &'tcx ty::Const<'tcx>) -> Result { - if let Some(&i) = self.compress.consts.get(&ct) { + if let Some(&i) = self.consts.get(&ct) { return self.print_backref(i); } let start = self.out.len(); @@ -579,7 +572,7 @@ impl Printer<'tcx> for SymbolMangler<'tcx> { // Only cache consts that do not refer to an enclosing // binder (which would change depending on context). if !ct.has_escaping_bound_vars() { - self.compress.consts.insert(ct, start); + self.consts.insert(ct, start); } Ok(self) } From 0eabbf84babbf9302285a4cb2ea54a9d67f13945 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tomasz=20Mi=C4=85sko?= Date: Thu, 29 Jul 2021 00:00:00 +0000 Subject: [PATCH 3/3] Implement `Printer` for `&mut SymbolMangler` to avoid passing the symbol mangler by value. --- compiler/rustc_symbol_mangling/src/v0.rs | 28 ++++++++++++------------ 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/compiler/rustc_symbol_mangling/src/v0.rs b/compiler/rustc_symbol_mangling/src/v0.rs index 6a1360da4ee9e..c4c1ec8ce4e0a 100644 --- a/compiler/rustc_symbol_mangling/src/v0.rs +++ b/compiler/rustc_symbol_mangling/src/v0.rs @@ -23,7 +23,7 @@ pub(super) fn mangle( let substs = tcx.normalize_erasing_regions(ty::ParamEnv::reveal_all(), instance.substs); let prefix = "_R"; - let mut cx = SymbolMangler { + let mut cx = &mut SymbolMangler { tcx, start_offset: prefix.len(), paths: FxHashMap::default(), @@ -49,7 +49,7 @@ pub(super) fn mangle( if let Some(instantiating_crate) = instantiating_crate { cx = cx.print_def_path(instantiating_crate.as_def_id(), &[]).unwrap(); } - cx.out + std::mem::take(&mut cx.out) } struct BinderLevel { @@ -153,13 +153,13 @@ impl SymbolMangler<'tcx> { self.push(ident); } - fn path_append_ns( - mut self, - print_prefix: impl FnOnce(Self) -> Result, + fn path_append_ns<'a>( + mut self: &'a mut Self, + print_prefix: impl FnOnce(&'a mut Self) -> Result<&'a mut Self, !>, ns: char, disambiguator: u64, name: &str, - ) -> Result { + ) -> Result<&'a mut Self, !> { self.push("N"); self.out.push(ns); self = print_prefix(self)?; @@ -168,17 +168,17 @@ impl SymbolMangler<'tcx> { Ok(self) } - fn print_backref(mut self, i: usize) -> Result { + fn print_backref(&mut self, i: usize) -> Result<&mut Self, !> { self.push("B"); self.push_integer_62((i - self.start_offset) as u64); Ok(self) } - fn in_binder( - mut self, + fn in_binder<'a, T>( + mut self: &'a mut Self, value: &ty::Binder<'tcx, T>, - print_value: impl FnOnce(Self, &T) -> Result, - ) -> Result + print_value: impl FnOnce(&'a mut Self, &T) -> Result<&'a mut Self, !>, + ) -> Result<&'a mut Self, !> where T: TypeFoldable<'tcx>, { @@ -211,7 +211,7 @@ impl SymbolMangler<'tcx> { } } -impl Printer<'tcx> for SymbolMangler<'tcx> { +impl Printer<'tcx> for &mut SymbolMangler<'tcx> { type Error = !; type Path = Self; @@ -303,7 +303,7 @@ impl Printer<'tcx> for SymbolMangler<'tcx> { Ok(self) } - fn print_region(mut self, region: ty::Region<'_>) -> Result { + fn print_region(self, region: ty::Region<'_>) -> Result { let i = match *region { // Erased lifetimes use the index 0, for a // shorter mangling of `L_`. @@ -577,7 +577,7 @@ impl Printer<'tcx> for SymbolMangler<'tcx> { Ok(self) } - fn path_crate(mut self, cnum: CrateNum) -> Result { + fn path_crate(self, cnum: CrateNum) -> Result { self.push("C"); let stable_crate_id = self.tcx.def_path_hash(cnum.as_def_id()).stable_crate_id(); self.push_disambiguator(stable_crate_id.to_u64());