Skip to content

Try not applying #[inline] to big structs #118031

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

Closed
Closed
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
21 changes: 20 additions & 1 deletion compiler/rustc_builtin_macros/src/deriving/debug.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ use crate::deriving::generic::*;
use crate::deriving::path_std;

use ast::EnumDef;
use rustc_ast::ItemKind;
use rustc_ast::VariantData;
use rustc_ast::{self as ast, MetaItem};
use rustc_expand::base::{Annotatable, ExtCtxt};
use rustc_span::symbol::{sym, Ident, Symbol};
Expand All @@ -20,6 +22,23 @@ pub fn expand_deriving_debug(
// &mut ::std::fmt::Formatter
let fmtr = Ref(Box::new(Path(path_std!(fmt::Formatter))), ast::Mutability::Mut);

// We default to applying #[inline]
let mut attributes = thin_vec![cx.attr_word(sym::inline, span)];
if let Annotatable::Item(item) = item {
match &item.kind {
ItemKind::Struct(VariantData::Struct(fields, _) | VariantData::Tuple(fields, _), _) => {
// Except that we drop it for structs with more than 5 fields, because with less
// than 5 fields we implement Debug with a single function call, which makes
// Debug::fmt a trivial wrapper that always should be optimized away. But with more
// than 5 fields, Debug::fmt has a complicated body.
if fields.len() > 5 {
attributes = ThinVec::new();
}
}
_ => {}
}
}

let trait_def = TraitDef {
span,
path: path_std!(fmt::Debug),
Expand All @@ -33,7 +52,7 @@ pub fn expand_deriving_debug(
explicit_self: true,
nonself_args: vec![(fmtr, sym::f)],
ret_ty: Path(path_std!(fmt::Result)),
attributes: thin_vec![cx.attr_word(sym::inline, span)],
attributes,
fieldless_variants_strategy:
FieldlessVariantsStrategy::SpecializeIfAllVariantsFieldless,
combine_substructure: combine_substructure(Box::new(|a, b, c| {
Expand Down