Skip to content

Introduce a fast path that avoids the debug_tuple abstraction when deriving Debug for unit-like enum variants. #88832

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
Sep 17, 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
20 changes: 17 additions & 3 deletions compiler/rustc_builtin_macros/src/deriving/debug.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,15 +65,29 @@ fn show_substructure(cx: &mut ExtCtxt<'_>, span: Span, substr: &Substructure<'_>
// We want to make sure we have the ctxt set so that we can use unstable methods
let span = cx.with_def_site_ctxt(span);
let name = cx.expr_lit(span, ast::LitKind::Str(ident.name, ast::StrStyle::Cooked));
let fmt = substr.nonself_args[0].clone();

// Special fast path for unit variants. In the common case of an enum that is entirely unit
// variants (i.e. a C-like enum), this fast path allows LLVM to eliminate the entire switch in
// favor of a lookup table.
if let ast::VariantData::Unit(..) = vdata {
let fn_path_write_str = cx.std_path(&[sym::fmt, sym::Formatter, sym::write_str]);
let expr = cx.expr_call_global(span, fn_path_write_str, vec![fmt, name]);
let stmts = vec![cx.stmt_expr(expr)];
let block = cx.block(span, stmts);
return cx.expr_block(block);
}

let builder = Ident::new(sym::debug_trait_builder, span);
let builder_expr = cx.expr_ident(span, builder);

let fmt = substr.nonself_args[0].clone();

let mut stmts = Vec::with_capacity(fields.len() + 2);
let fn_path_finish;
match vdata {
ast::VariantData::Tuple(..) | ast::VariantData::Unit(..) => {
ast::VariantData::Unit(..) => {
cx.span_bug(span, "unit variants should have been handled above");
}
ast::VariantData::Tuple(..) => {
// tuple struct/"normal" variant
let fn_path_debug_tuple = cx.std_path(&[sym::fmt, sym::Formatter, sym::debug_tuple]);
let expr = cx.expr_call_global(span, fn_path_debug_tuple, vec![fmt, name]);
Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_span/src/symbol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1418,6 +1418,7 @@ symbols! {
wrapping_sub,
wreg,
write_bytes,
write_str,
x87_reg,
xer,
xmm_reg,
Expand Down