about summary refs log tree commit diff
path: root/compiler/rustc_serialize/src
diff options
context:
space:
mode:
authorMark Rousskov <mark.simulacrum@gmail.com>2021-10-12 10:43:39 -0400
committerMark Rousskov <mark.simulacrum@gmail.com>2021-11-22 21:22:35 -0500
commit3228603cce1bcb81a48171cf5e2083ee0cda7fd9 (patch)
tree04eec4981c8631c3d091e7d488e0548048439355 /compiler/rustc_serialize/src
parent65f3f8b220f020e562c5dd848ff7319257a7ba45 (diff)
downloadrust-3228603cce1bcb81a48171cf5e2083ee0cda7fd9.tar.gz
rust-3228603cce1bcb81a48171cf5e2083ee0cda7fd9.zip
Avoid generating empty closures for fieldless enums
For many enums, this avoids generating lots of tiny stubs that need to be
codegen'd and then inlined and removed by LLVM.
Diffstat (limited to 'compiler/rustc_serialize/src')
-rw-r--r--compiler/rustc_serialize/src/json.rs14
-rw-r--r--compiler/rustc_serialize/src/serialize.rs14
2 files changed, 28 insertions, 0 deletions
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