From 5caf311885cbc7ac85dd724690ea3b10953a9da9 Mon Sep 17 00:00:00 2001 From: panstromek Date: Fri, 9 Oct 2026 14:26:32 +0200 Subject: [PATCH 1/2] try to remove all debug code to measure the cost of it --- compiler/rustc_session/src/options.rs | 2 +- tests/ui/derives/deriving-all-codegen.stdout | 75 ++++++-------------- 2 files changed, 22 insertions(+), 55 deletions(-) diff --git a/compiler/rustc_session/src/options.rs b/compiler/rustc_session/src/options.rs index a29309c71e879..5a879d40fe782 100644 --- a/compiler/rustc_session/src/options.rs +++ b/compiler/rustc_session/src/options.rs @@ -2539,7 +2539,7 @@ options! { flatten_format_args: bool = (true, parse_bool, [TRACKED], "flatten nested format_args!() and literals into a simplified format_args!() call \ (default: yes)"), - fmt_debug: FmtDebug = (FmtDebug::Full, parse_fmt_debug, [TRACKED], + fmt_debug: FmtDebug = (FmtDebug::None, parse_fmt_debug, [TRACKED], "how detailed `#[derive(Debug)]` should be. `full` prints types recursively, \ `shallow` prints only type names, `none` prints nothing and disables `{:?}`. (default: `full`)"), force_intrinsic_fallback: bool = (false, parse_bool, [TRACKED], diff --git a/tests/ui/derives/deriving-all-codegen.stdout b/tests/ui/derives/deriving-all-codegen.stdout index 163658d2106dd..0ffc158ff63fc 100644 --- a/tests/ui/derives/deriving-all-codegen.stdout +++ b/tests/ui/derives/deriving-all-codegen.stdout @@ -40,7 +40,7 @@ impl ::core::marker::Copy for Empty { } impl ::core::fmt::Debug for Empty { #[inline] fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { - ::core::fmt::Formatter::write_str(f, "Empty") + ::core::result::Result::Ok(()) } } #[automatically_derived] @@ -101,8 +101,7 @@ impl ::core::marker::Copy for Point { } impl ::core::fmt::Debug for Point { #[inline] fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { - ::core::fmt::Formatter::debug_struct_field2_finish(f, "Point", "x", - &self.x, "y", &&self.y) + ::core::result::Result::Ok(()) } } #[automatically_derived] @@ -185,8 +184,7 @@ impl ::core::marker::Copy for PackedPoint { } impl ::core::fmt::Debug for PackedPoint { #[inline] fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { - ::core::fmt::Formatter::debug_struct_field2_finish(f, "PackedPoint", - "x", &{ self.x }, "y", &&{ self.y }) + ::core::result::Result::Ok(()) } } #[automatically_derived] @@ -263,8 +261,7 @@ impl ::core::marker::Copy for TupleSingleField { } impl ::core::fmt::Debug for TupleSingleField { #[inline] fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { - ::core::fmt::Formatter::debug_tuple_field1_finish(f, - "TupleSingleField", &&self.0) + ::core::result::Result::Ok(()) } } #[automatically_derived] @@ -338,8 +335,7 @@ impl ::core::marker::Copy for SingleField { } impl ::core::fmt::Debug for SingleField { #[inline] fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { - ::core::fmt::Formatter::debug_struct_field1_finish(f, "SingleField", - "foo", &&self.foo) + ::core::result::Result::Ok(()) } } #[automatically_derived] @@ -422,13 +418,7 @@ impl ::core::marker::Copy for Big { } impl ::core::fmt::Debug for Big { #[inline] fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { - let names: &'static _ = - &["b1", "b2", "b3", "b4", "b5", "b6", "b7", "b8"]; - let values: &[&dyn ::core::fmt::Debug] = - &[&self.b1, &self.b2, &self.b3, &self.b4, &self.b5, &self.b6, - &self.b7, &&self.b8]; - ::core::fmt::Formatter::debug_struct_fields_finish(f, "Big", names, - values) + ::core::result::Result::Ok(()) } } #[automatically_derived] @@ -672,8 +662,7 @@ struct Unsized([u32]); impl ::core::fmt::Debug for Unsized { #[inline] fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { - ::core::fmt::Formatter::debug_tuple_field1_finish(f, "Unsized", - &&self.0) + ::core::result::Result::Ok(()) } } #[automatically_derived] @@ -751,8 +740,7 @@ impl ::core::fmt::Debug for Generic where T::A: ::core::fmt::Debug { #[inline] fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { - ::core::fmt::Formatter::debug_struct_field3_finish(f, "Generic", "t", - &self.t, "ta", &self.ta, "u", &&self.u) + ::core::result::Result::Ok(()) } } #[automatically_derived] @@ -871,8 +859,7 @@ impl ::core::fmt::Result { - ::core::fmt::Formatter::debug_tuple_field3_finish(f, "PackedGeneric", - &{ self.0 }, &{ self.1 }, &&{ self.2 }) + ::core::result::Result::Ok(()) } } #[automatically_derived] @@ -1038,9 +1025,7 @@ impl ::core::fmt::Debug for Enum1 { #[inline] fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { match self { - Self::Single { x: __self_0 } => - ::core::fmt::Formatter::debug_struct_field1_finish(f, - "Single", "x", &__self_0), + Self::Single { x: __self_0 } => ::core::result::Result::Ok(()), } } } @@ -1109,7 +1094,7 @@ impl ::core::clone::Clone for Fieldless1 { impl ::core::fmt::Debug for Fieldless1 { #[inline] fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { - ::core::fmt::Formatter::write_str(f, "A") + ::core::result::Result::Ok(()) } } #[automatically_derived] @@ -1169,12 +1154,7 @@ impl ::core::marker::Copy for Fieldless { } impl ::core::fmt::Debug for Fieldless { #[inline] fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { - ::core::fmt::Formatter::write_str(f, - match self { - Fieldless::A => "A", - Fieldless::B => "B", - Fieldless::C => "C", - }) + ::core::result::Result::Ok(()) } } #[automatically_derived] @@ -1251,14 +1231,11 @@ impl ::core::fmt::Debug for Mixed { #[inline] fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { match self { - Self::P => ::core::fmt::Formatter::write_str(f, "P"), - Self::Q => ::core::fmt::Formatter::write_str(f, "Q"), - Self::R(__self_0) => - ::core::fmt::Formatter::debug_tuple_field1_finish(f, "R", - &__self_0), + Self::P => ::core::result::Result::Ok(()), + Self::Q => ::core::result::Result::Ok(()), + Self::R(__self_0) => ::core::result::Result::Ok(()), Self::S { d1: __self_0, d2: __self_1 } => - ::core::fmt::Formatter::debug_struct_field2_finish(f, "S", - "d1", __self_0, "d2", &__self_1), + ::core::result::Result::Ok(()), } } } @@ -1431,15 +1408,9 @@ impl ::core::fmt::Debug for Fielded { #[inline] fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { match self { - Self::X(__self_0) => - ::core::fmt::Formatter::debug_tuple_field1_finish(f, "X", - &__self_0), - Self::Y(__self_0) => - ::core::fmt::Formatter::debug_tuple_field1_finish(f, "Y", - &__self_0), - Self::Z(__self_0) => - ::core::fmt::Formatter::debug_tuple_field1_finish(f, "Z", - &__self_0), + Self::X(__self_0) => ::core::result::Result::Ok(()), + Self::Y(__self_0) => ::core::result::Result::Ok(()), + Self::Z(__self_0) => ::core::result::Result::Ok(()), } } } @@ -1537,12 +1508,8 @@ impl ::core::fmt::Debug for #[inline] fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { match self { - Self::One(__self_0) => - ::core::fmt::Formatter::debug_tuple_field1_finish(f, "One", - &__self_0), - Self::Two(__self_0) => - ::core::fmt::Formatter::debug_tuple_field1_finish(f, "Two", - &__self_0), + Self::One(__self_0) => ::core::result::Result::Ok(()), + Self::Two(__self_0) => ::core::result::Result::Ok(()), } } } From 13085bd7fe179990b4ccc910e1bad8354ad662de Mon Sep 17 00:00:00 2001 From: panstromek Date: Fri, 9 Oct 2026 14:55:27 +0200 Subject: [PATCH 2/2] avoid generating pointless matching --- .../src/deriving/debug.rs | 2 +- .../src/deriving/generic/mod.rs | 13 +++++++++++ tests/ui/derives/deriving-all-codegen.stdout | 23 ++++--------------- 3 files changed, 18 insertions(+), 20 deletions(-) diff --git a/compiler/rustc_builtin_macros/src/deriving/debug.rs b/compiler/rustc_builtin_macros/src/deriving/debug.rs index d8355d733a7f3..615fefc3c8572 100644 --- a/compiler/rustc_builtin_macros/src/deriving/debug.rs +++ b/compiler/rustc_builtin_macros/src/deriving/debug.rs @@ -50,7 +50,7 @@ pub(crate) fn expand_deriving_debug( safety: Safety::Default, document: true, }; - trait_def.expand(cx, item, push) + trait_def.expand_ext_inner(cx, item, push, false, true) } fn formatter_ident(cx: &ExtCtxt<'_>, span: Span) -> Box { diff --git a/compiler/rustc_builtin_macros/src/deriving/generic/mod.rs b/compiler/rustc_builtin_macros/src/deriving/generic/mod.rs index fbbadce3a8816..fa3511b864e60 100644 --- a/compiler/rustc_builtin_macros/src/deriving/generic/mod.rs +++ b/compiler/rustc_builtin_macros/src/deriving/generic/mod.rs @@ -460,11 +460,22 @@ impl<'a> TraitDef<'a> { } pub(crate) fn expand_ext( + self, + cx: &ExtCtxt<'_>, + item: &'a ast::Item, + push: &mut dyn FnMut(Box), + from_scratch: bool + ) { + self.expand_ext_inner(cx, item, push, from_scratch, false); + } + + pub(crate) fn expand_ext_inner( self, cx: &ExtCtxt<'_>, item: &'a ast::Item, push: &mut dyn FnMut(Box), from_scratch: bool, + empty: bool ) { let span = self.span; let is_packed = matches!( @@ -509,6 +520,8 @@ impl<'a> TraitDef<'a> { let methods = self.methods.iter().filter_map(|method_def| { let body = if from_scratch || method_def.is_static() { method_def.call_substructure_method(cx, span, StaticEnum(enum_def), *ident) + } else if empty && enum_def.variants.len() > 0 { + method_def.call_substructure_method(cx, span, EnumDiscr(None), *ident) } else { method_def.expand_enum_method_body(cx, span, enum_def, *ident) }; diff --git a/tests/ui/derives/deriving-all-codegen.stdout b/tests/ui/derives/deriving-all-codegen.stdout index 0ffc158ff63fc..3598911e3bfa3 100644 --- a/tests/ui/derives/deriving-all-codegen.stdout +++ b/tests/ui/derives/deriving-all-codegen.stdout @@ -1024,9 +1024,7 @@ impl ::core::clone::Clone for Enum1 { impl ::core::fmt::Debug for Enum1 { #[inline] fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { - match self { - Self::Single { x: __self_0 } => ::core::result::Result::Ok(()), - } + ::core::result::Result::Ok(()) } } #[automatically_derived] @@ -1230,13 +1228,7 @@ impl ::core::marker::Copy for Mixed { } impl ::core::fmt::Debug for Mixed { #[inline] fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { - match self { - Self::P => ::core::result::Result::Ok(()), - Self::Q => ::core::result::Result::Ok(()), - Self::R(__self_0) => ::core::result::Result::Ok(()), - Self::S { d1: __self_0, d2: __self_1 } => - ::core::result::Result::Ok(()), - } + ::core::result::Result::Ok(()) } } #[automatically_derived] @@ -1407,11 +1399,7 @@ impl ::core::clone::Clone for Fielded { impl ::core::fmt::Debug for Fielded { #[inline] fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { - match self { - Self::X(__self_0) => ::core::result::Result::Ok(()), - Self::Y(__self_0) => ::core::result::Result::Ok(()), - Self::Z(__self_0) => ::core::result::Result::Ok(()), - } + ::core::result::Result::Ok(()) } } #[automatically_derived] @@ -1507,10 +1495,7 @@ impl ::core::fmt::Debug for EnumGeneric { #[inline] fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { - match self { - Self::One(__self_0) => ::core::result::Result::Ok(()), - Self::Two(__self_0) => ::core::result::Result::Ok(()), - } + ::core::result::Result::Ok(()) } } #[automatically_derived]