Skip to content

Avoid generating empty closures for fieldless enum variants #89881

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 23, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 18 additions & 7 deletions compiler/rustc_macros/src/serialize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
});
Expand Down
14 changes: 14 additions & 0 deletions compiler/rustc_serialize/src/json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
14 changes: 14 additions & 0 deletions compiler/rustc_serialize/src/serialize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down