From 5b0324fce09e1b08d4ebb51e90d1e37d206bef3b Mon Sep 17 00:00:00 2001 From: Nicholas Nethercote Date: Fri, 26 May 2023 06:55:06 +1000 Subject: [PATCH] Inline derived `hash` function. Because most of the other derived functions are inlined: `clone`, `default`, `eq`, `partial_cmp`, `cmp`. The exception is `fmt`, but it tends to not be on hot paths as much. --- .../rustc_builtin_macros/src/deriving/hash.rs | 5 +++-- tests/ui/deriving/deriving-all-codegen.stdout | 15 +++++++++++++++ 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/compiler/rustc_builtin_macros/src/deriving/hash.rs b/compiler/rustc_builtin_macros/src/deriving/hash.rs index 4eee573db42..6fea50feb2a 100644 --- a/compiler/rustc_builtin_macros/src/deriving/hash.rs +++ b/compiler/rustc_builtin_macros/src/deriving/hash.rs @@ -1,7 +1,7 @@ use crate::deriving::generic::ty::*; use crate::deriving::generic::*; use crate::deriving::{path_std, pathvec_std}; -use rustc_ast::{AttrVec, MetaItem, Mutability}; +use rustc_ast::{MetaItem, Mutability}; use rustc_expand::base::{Annotatable, ExtCtxt}; use rustc_span::symbol::sym; use rustc_span::Span; @@ -20,6 +20,7 @@ pub fn expand_deriving_hash( let typaram = sym::__H; let arg = Path::new_local(typaram); + let attrs = thin_vec![cx.attr_word(sym::inline, span)]; let hash_trait_def = TraitDef { span, path, @@ -33,7 +34,7 @@ pub fn expand_deriving_hash( explicit_self: true, nonself_args: vec![(Ref(Box::new(Path(arg)), Mutability::Mut), sym::state)], ret_ty: Unit, - attributes: AttrVec::new(), + attributes: attrs, fieldless_variants_strategy: FieldlessVariantsStrategy::Unify, combine_substructure: combine_substructure(Box::new(|a, b, c| { hash_substructure(a, b, c) diff --git a/tests/ui/deriving/deriving-all-codegen.stdout b/tests/ui/deriving/deriving-all-codegen.stdout index 5bca83e87f8..d6a2c80cc06 100644 --- a/tests/ui/deriving/deriving-all-codegen.stdout +++ b/tests/ui/deriving/deriving-all-codegen.stdout @@ -44,6 +44,7 @@ impl ::core::default::Default for Empty { } #[automatically_derived] impl ::core::hash::Hash for Empty { + #[inline] fn hash<__H: ::core::hash::Hasher>(&self, state: &mut __H) -> () {} } #[automatically_derived] @@ -113,6 +114,7 @@ impl ::core::default::Default for Point { } #[automatically_derived] impl ::core::hash::Hash for Point { + #[inline] fn hash<__H: ::core::hash::Hasher>(&self, state: &mut __H) -> () { ::core::hash::Hash::hash(&self.x, state); ::core::hash::Hash::hash(&self.y, state) @@ -198,6 +200,7 @@ impl ::core::default::Default for PackedPoint { } #[automatically_derived] impl ::core::hash::Hash for PackedPoint { + #[inline] fn hash<__H: ::core::hash::Hasher>(&self, state: &mut __H) -> () { ::core::hash::Hash::hash(&{ self.x }, state); ::core::hash::Hash::hash(&{ self.y }, state) @@ -301,6 +304,7 @@ impl ::core::default::Default for Big { } #[automatically_derived] impl ::core::hash::Hash for Big { + #[inline] fn hash<__H: ::core::hash::Hasher>(&self, state: &mut __H) -> () { ::core::hash::Hash::hash(&self.b1, state); ::core::hash::Hash::hash(&self.b2, state); @@ -478,6 +482,7 @@ impl ::core::fmt::Debug for Unsized { } #[automatically_derived] impl ::core::hash::Hash for Unsized { + #[inline] fn hash<__H: ::core::hash::Hasher>(&self, state: &mut __H) -> () { ::core::hash::Hash::hash(&self.0, state) } @@ -529,6 +534,7 @@ impl ::core::fmt::Debug for PackedUnsizedU8 { } #[automatically_derived] impl ::core::hash::Hash for PackedUnsizedU8 { + #[inline] fn hash<__H: ::core::hash::Hasher>(&self, state: &mut __H) -> () { ::core::hash::Hash::hash(&self.0, state) } @@ -584,6 +590,7 @@ impl #[automatically_derived] impl ::core::hash::Hash for Generic where T::A: ::core::hash::Hash { + #[inline] fn hash<__H: ::core::hash::Hasher>(&self, state: &mut __H) -> () { ::core::hash::Hash::hash(&self.t, state); ::core::hash::Hash::hash(&self.ta, state); @@ -701,6 +708,7 @@ impl ::core::hash::Hash for PackedGeneric where T::A: ::core::hash::Hash + ::core::marker::Copy { + #[inline] fn hash<__H: ::core::hash::Hasher>(&self, state: &mut __H) -> () { ::core::hash::Hash::hash(&{ self.0 }, state); ::core::hash::Hash::hash(&{ self.1 }, state); @@ -795,6 +803,7 @@ impl ::core::fmt::Debug for Enum0 { } #[automatically_derived] impl ::core::hash::Hash for Enum0 { + #[inline] fn hash<__H: ::core::hash::Hasher>(&self, state: &mut __H) -> () { unsafe { ::core::intrinsics::unreachable() } } @@ -861,6 +870,7 @@ impl ::core::fmt::Debug for Enum1 { } #[automatically_derived] impl ::core::hash::Hash for Enum1 { + #[inline] fn hash<__H: ::core::hash::Hasher>(&self, state: &mut __H) -> () { match self { Enum1::Single { x: __self_0 } => @@ -937,6 +947,7 @@ impl ::core::default::Default for Fieldless1 { } #[automatically_derived] impl ::core::hash::Hash for Fieldless1 { + #[inline] fn hash<__H: ::core::hash::Hasher>(&self, state: &mut __H) -> () {} } #[automatically_derived] @@ -1004,6 +1015,7 @@ impl ::core::default::Default for Fieldless { } #[automatically_derived] impl ::core::hash::Hash for Fieldless { + #[inline] fn hash<__H: ::core::hash::Hasher>(&self, state: &mut __H) -> () { let __self_tag = ::core::intrinsics::discriminant_value(self); ::core::hash::Hash::hash(&__self_tag, state) @@ -1095,6 +1107,7 @@ impl ::core::default::Default for Mixed { } #[automatically_derived] impl ::core::hash::Hash for Mixed { + #[inline] fn hash<__H: ::core::hash::Hasher>(&self, state: &mut __H) -> () { let __self_tag = ::core::intrinsics::discriminant_value(self); ::core::hash::Hash::hash(&__self_tag, state); @@ -1224,6 +1237,7 @@ impl ::core::fmt::Debug for Fielded { } #[automatically_derived] impl ::core::hash::Hash for Fielded { + #[inline] fn hash<__H: ::core::hash::Hasher>(&self, state: &mut __H) -> () { let __self_tag = ::core::intrinsics::discriminant_value(self); ::core::hash::Hash::hash(&__self_tag, state); @@ -1345,6 +1359,7 @@ impl ::core::fmt::Debug for #[automatically_derived] impl ::core::hash::Hash for EnumGeneric { + #[inline] fn hash<__H: ::core::hash::Hasher>(&self, state: &mut __H) -> () { let __self_tag = ::core::intrinsics::discriminant_value(self); ::core::hash::Hash::hash(&__self_tag, state);