From 5e8f67bbc9017cc02338e959ca49f5355ff22f07 Mon Sep 17 00:00:00 2001 From: Mark Rousskov Date: Tue, 9 Jan 2024 18:40:00 -0500 Subject: [PATCH 1/2] u8 tags for smaller enums 100% of the serialized enums during libcore compilation fit into the smaller tag, and this eliminates hitting the leb128 code for coding/decoding when we can statically guarantee that's not required. 30% of all leb128 integers serialized in libcore (12981183 total) come from the usize's removed here. --- compiler/rustc_macros/src/serialize.rs | 30 ++++++++++++++++++++------ 1 file changed, 24 insertions(+), 6 deletions(-) diff --git a/compiler/rustc_macros/src/serialize.rs b/compiler/rustc_macros/src/serialize.rs index 9ca7ce09ba6c7..98b53945b9115 100644 --- a/compiler/rustc_macros/src/serialize.rs +++ b/compiler/rustc_macros/src/serialize.rs @@ -76,8 +76,17 @@ fn decodable_body( ty_name, variants.len() ); + let tag = if variants.len() < u8::MAX as usize { + quote! { + ::rustc_serialize::Decoder::read_u8(__decoder) as usize + } + } else { + quote! { + ::rustc_serialize::Decoder::read_usize(__decoder) + } + }; quote! { - match ::rustc_serialize::Decoder::read_usize(__decoder) { + match #tag { #match_inner n => panic!(#message, n), } @@ -206,11 +215,20 @@ fn encodable_body( variant_idx += 1; result }); - quote! { - let disc = match *self { - #encode_inner - }; - ::rustc_serialize::Encoder::emit_usize(__encoder, disc); + if variant_idx < u8::MAX as usize { + quote! { + let disc = match *self { + #encode_inner + }; + ::rustc_serialize::Encoder::emit_u8(__encoder, disc as u8); + } + } else { + quote! { + let disc = match *self { + #encode_inner + }; + ::rustc_serialize::Encoder::emit_usize(__encoder, disc); + } } }; From 1d2005be71431f9a870fd347516ff2317c1d37a3 Mon Sep 17 00:00:00 2001 From: Mark Rousskov Date: Tue, 9 Jan 2024 19:51:18 -0500 Subject: [PATCH 2/2] Remove more needless leb128 coding for enum variants This removes emit_enum_variant and the emit_usize calls that resulted in. In libcore this eliminates 17% of leb128, taking us from 8964488 to 7383842 leb128's serialized. --- compiler/rustc_serialize/src/serialize.rs | 33 +++++++++++------------ compiler/rustc_span/src/lib.rs | 23 ++++++++-------- compiler/rustc_target/src/spec/mod.rs | 17 +++++++----- 3 files changed, 37 insertions(+), 36 deletions(-) diff --git a/compiler/rustc_serialize/src/serialize.rs b/compiler/rustc_serialize/src/serialize.rs index 63bd3457eb97c..287e317b10f33 100644 --- a/compiler/rustc_serialize/src/serialize.rs +++ b/compiler/rustc_serialize/src/serialize.rs @@ -70,14 +70,6 @@ pub trait Encoder { } fn emit_raw_bytes(&mut self, s: &[u8]); - - fn emit_enum_variant(&mut self, v_id: usize, f: F) - where - F: FnOnce(&mut Self), - { - self.emit_usize(v_id); - f(self); - } } // Note: all the methods in this trait are infallible, which may be surprising. @@ -132,10 +124,6 @@ pub trait Decoder { fn read_raw_bytes(&mut self, len: usize) -> &[u8]; - // Although there is an `emit_enum_variant` method in `Encoder`, the code - // patterns in decoding are different enough to encoding that there is no - // need for a corresponding `read_enum_variant` method here. - fn peek_byte(&self) -> u8; fn position(&self) -> usize; } @@ -372,15 +360,18 @@ impl<'a, D: Decoder> Decodable for Cow<'a, str> { impl> Encodable for Option { fn encode(&self, s: &mut S) { match *self { - None => s.emit_enum_variant(0, |_| {}), - Some(ref v) => s.emit_enum_variant(1, |s| v.encode(s)), + None => s.emit_u8(0), + Some(ref v) => { + s.emit_u8(1); + v.encode(s); + } } } } impl> Decodable for Option { fn decode(d: &mut D) -> Option { - match d.read_usize() { + match d.read_u8() { 0 => None, 1 => Some(Decodable::decode(d)), _ => panic!("Encountered invalid discriminant while decoding `Option`."), @@ -391,15 +382,21 @@ impl> Decodable for Option { impl, T2: Encodable> Encodable for Result { fn encode(&self, s: &mut S) { match *self { - Ok(ref v) => s.emit_enum_variant(0, |s| v.encode(s)), - Err(ref v) => s.emit_enum_variant(1, |s| v.encode(s)), + Ok(ref v) => { + s.emit_u8(0); + v.encode(s); + } + Err(ref v) => { + s.emit_u8(1); + v.encode(s); + } } } } impl, T2: Decodable> Decodable for Result { fn decode(d: &mut D) -> Result { - match d.read_usize() { + match d.read_u8() { 0 => Ok(T1::decode(d)), 1 => Err(T2::decode(d)), _ => panic!("Encountered invalid discriminant while decoding `Result`."), diff --git a/compiler/rustc_span/src/lib.rs b/compiler/rustc_span/src/lib.rs index 4235293823c29..65702f76fdac7 100644 --- a/compiler/rustc_span/src/lib.rs +++ b/compiler/rustc_span/src/lib.rs @@ -203,18 +203,19 @@ impl Hash for RealFileName { impl Encodable for RealFileName { fn encode(&self, encoder: &mut S) { match *self { - RealFileName::LocalPath(ref local_path) => encoder.emit_enum_variant(0, |encoder| { + RealFileName::LocalPath(ref local_path) => { + encoder.emit_u8(0); local_path.encode(encoder); - }), - - RealFileName::Remapped { ref local_path, ref virtual_name } => encoder - .emit_enum_variant(1, |encoder| { - // For privacy and build reproducibility, we must not embed host-dependant path - // in artifacts if they have been remapped by --remap-path-prefix - assert!(local_path.is_none()); - local_path.encode(encoder); - virtual_name.encode(encoder); - }), + } + + RealFileName::Remapped { ref local_path, ref virtual_name } => { + encoder.emit_u8(1); + // For privacy and build reproducibility, we must not embed host-dependant path + // in artifacts if they have been remapped by --remap-path-prefix + assert!(local_path.is_none()); + local_path.encode(encoder); + virtual_name.encode(encoder); + } } } } diff --git a/compiler/rustc_target/src/spec/mod.rs b/compiler/rustc_target/src/spec/mod.rs index 8e26327196a1a..da8706ea715b3 100644 --- a/compiler/rustc_target/src/spec/mod.rs +++ b/compiler/rustc_target/src/spec/mod.rs @@ -3396,19 +3396,22 @@ impl Hash for TargetTriple { impl Encodable for TargetTriple { fn encode(&self, s: &mut S) { match self { - TargetTriple::TargetTriple(triple) => s.emit_enum_variant(0, |s| s.emit_str(triple)), - TargetTriple::TargetJson { path_for_rustdoc: _, triple, contents } => s - .emit_enum_variant(1, |s| { - s.emit_str(triple); - s.emit_str(contents) - }), + TargetTriple::TargetTriple(triple) => { + s.emit_u8(0); + s.emit_str(triple); + } + TargetTriple::TargetJson { path_for_rustdoc: _, triple, contents } => { + s.emit_u8(1); + s.emit_str(triple); + s.emit_str(contents); + } } } } impl Decodable for TargetTriple { fn decode(d: &mut D) -> Self { - match d.read_usize() { + match d.read_u8() { 0 => TargetTriple::TargetTriple(d.read_str().to_owned()), 1 => TargetTriple::TargetJson { path_for_rustdoc: PathBuf::new(),