diff options
| author | bors <bors@rust-lang.org> | 2021-11-23 13:56:00 +0000 |
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2021-11-23 13:56:00 +0000 |
| commit | 311fa1f14dd8ffbbe83b229a94b17f7f1ecaf33b (patch) | |
| tree | 8c499d38adbac35b3b987b6968f932795fb6e890 | |
| parent | 22c2d9ddbf356bcdb718e88ca6ee3665e1e42690 (diff) | |
| parent | 3228603cce1bcb81a48171cf5e2083ee0cda7fd9 (diff) | |
| download | rust-311fa1f14dd8ffbbe83b229a94b17f7f1ecaf33b.tar.gz rust-311fa1f14dd8ffbbe83b229a94b17f7f1ecaf33b.zip | |
Auto merge of #89881 - Mark-Simulacrum:fieldless-fast, r=davidtwco
Avoid generating empty closures for fieldless enum variants For many enums, this avoids generating lots of tiny stubs that need to be codegen'd and then inlined and removed by LLVM. perf shows this to be a fairly small, but significant, win on rustc bootstrap time -- with minimal impact on runtime performance (which is at times even positive).
| -rw-r--r-- | compiler/rustc_macros/src/serialize.rs | 25 | ||||
| -rw-r--r-- | compiler/rustc_serialize/src/json.rs | 14 | ||||
| -rw-r--r-- | compiler/rustc_serialize/src/serialize.rs | 14 |
3 files changed, 46 insertions, 7 deletions
diff --git a/compiler/rustc_macros/src/serialize.rs b/compiler/rustc_macros/src/serialize.rs index 7bc669f2b00..66e6b571beb 100644 --- a/compiler/rustc_macros/src/serialize.rs +++ b/compiler/rustc_macros/src/serialize.rs @@ -247,13 +247,24 @@ fn encodable_body( }) .collect(); - let result = quote! { ::rustc_serialize::Encoder::emit_enum_variant( - __encoder, - #variant_name, - #variant_idx, - #field_idx, - |__encoder| { ::std::result::Result::Ok({ #encode_fields }) } - ) }; + let result = if field_idx != 0 { + quote! { + ::rustc_serialize::Encoder::emit_enum_variant( + __encoder, + #variant_name, + #variant_idx, + #field_idx, + |__encoder| { ::std::result::Result::Ok({ #encode_fields }) } + ) + } + } else { + quote! { + ::rustc_serialize::Encoder::emit_fieldless_enum_variant::<#variant_idx>( + __encoder, + #variant_name, + ) + } + }; variant_idx += 1; result }); diff --git a/compiler/rustc_serialize/src/json.rs b/compiler/rustc_serialize/src/json.rs index e5369b4bbfd..df78e1bcbf6 100644 --- a/compiler/rustc_serialize/src/json.rs +++ b/compiler/rustc_serialize/src/json.rs @@ -589,6 +589,13 @@ impl<'a> crate::Encoder for Encoder<'a> { } } + fn emit_fieldless_enum_variant<const ID: usize>( + &mut self, + name: &str, + ) -> Result<(), Self::Error> { + escape_str(self.writer, name) + } + fn emit_enum_variant_arg<F>(&mut self, first: bool, f: F) -> EncodeResult where F: FnOnce(&mut Encoder<'a>) -> EncodeResult, @@ -885,6 +892,13 @@ impl<'a> crate::Encoder for PrettyEncoder<'a> { } } + fn emit_fieldless_enum_variant<const ID: usize>( + &mut self, + name: &str, + ) -> Result<(), Self::Error> { + escape_str(self.writer, name) + } + fn emit_enum_variant_arg<F>(&mut self, first: bool, f: F) -> EncodeResult where F: FnOnce(&mut PrettyEncoder<'a>) -> EncodeResult, diff --git a/compiler/rustc_serialize/src/serialize.rs b/compiler/rustc_serialize/src/serialize.rs index e32e4493726..96a2231b590 100644 --- a/compiler/rustc_serialize/src/serialize.rs +++ b/compiler/rustc_serialize/src/serialize.rs @@ -58,6 +58,20 @@ pub trait Encoder { f(self) } + // We put the field index in a const generic to allow the emit_usize to be + // compiled into a more efficient form. In practice, the variant index is + // known at compile-time, and that knowledge allows much more efficient + // codegen than we'd otherwise get. LLVM isn't always able to make the + // optimization that would otherwise be necessary here, likely due to the + // multiple levels of inlining and const-prop that are needed. + #[inline] + fn emit_fieldless_enum_variant<const ID: usize>( + &mut self, + _v_name: &str, + ) -> Result<(), Self::Error> { + self.emit_usize(ID) + } + #[inline] fn emit_enum_variant_arg<F>(&mut self, _first: bool, f: F) -> Result<(), Self::Error> where |
