Skip to content

Commit 5b0324f

Browse files
committed
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.
1 parent 8b4b208 commit 5b0324f

File tree

2 files changed

+18
-2
lines changed

2 files changed

+18
-2
lines changed

compiler/rustc_builtin_macros/src/deriving/hash.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use crate::deriving::generic::ty::*;
22
use crate::deriving::generic::*;
33
use crate::deriving::{path_std, pathvec_std};
4-
use rustc_ast::{AttrVec, MetaItem, Mutability};
4+
use rustc_ast::{MetaItem, Mutability};
55
use rustc_expand::base::{Annotatable, ExtCtxt};
66
use rustc_span::symbol::sym;
77
use rustc_span::Span;
@@ -20,6 +20,7 @@ pub fn expand_deriving_hash(
2020
let typaram = sym::__H;
2121

2222
let arg = Path::new_local(typaram);
23+
let attrs = thin_vec![cx.attr_word(sym::inline, span)];
2324
let hash_trait_def = TraitDef {
2425
span,
2526
path,
@@ -33,7 +34,7 @@ pub fn expand_deriving_hash(
3334
explicit_self: true,
3435
nonself_args: vec![(Ref(Box::new(Path(arg)), Mutability::Mut), sym::state)],
3536
ret_ty: Unit,
36-
attributes: AttrVec::new(),
37+
attributes: attrs,
3738
fieldless_variants_strategy: FieldlessVariantsStrategy::Unify,
3839
combine_substructure: combine_substructure(Box::new(|a, b, c| {
3940
hash_substructure(a, b, c)

tests/ui/deriving/deriving-all-codegen.stdout

+15
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ impl ::core::default::Default for Empty {
4444
}
4545
#[automatically_derived]
4646
impl ::core::hash::Hash for Empty {
47+
#[inline]
4748
fn hash<__H: ::core::hash::Hasher>(&self, state: &mut __H) -> () {}
4849
}
4950
#[automatically_derived]
@@ -113,6 +114,7 @@ impl ::core::default::Default for Point {
113114
}
114115
#[automatically_derived]
115116
impl ::core::hash::Hash for Point {
117+
#[inline]
116118
fn hash<__H: ::core::hash::Hasher>(&self, state: &mut __H) -> () {
117119
::core::hash::Hash::hash(&self.x, state);
118120
::core::hash::Hash::hash(&self.y, state)
@@ -198,6 +200,7 @@ impl ::core::default::Default for PackedPoint {
198200
}
199201
#[automatically_derived]
200202
impl ::core::hash::Hash for PackedPoint {
203+
#[inline]
201204
fn hash<__H: ::core::hash::Hasher>(&self, state: &mut __H) -> () {
202205
::core::hash::Hash::hash(&{ self.x }, state);
203206
::core::hash::Hash::hash(&{ self.y }, state)
@@ -301,6 +304,7 @@ impl ::core::default::Default for Big {
301304
}
302305
#[automatically_derived]
303306
impl ::core::hash::Hash for Big {
307+
#[inline]
304308
fn hash<__H: ::core::hash::Hasher>(&self, state: &mut __H) -> () {
305309
::core::hash::Hash::hash(&self.b1, state);
306310
::core::hash::Hash::hash(&self.b2, state);
@@ -478,6 +482,7 @@ impl ::core::fmt::Debug for Unsized {
478482
}
479483
#[automatically_derived]
480484
impl ::core::hash::Hash for Unsized {
485+
#[inline]
481486
fn hash<__H: ::core::hash::Hasher>(&self, state: &mut __H) -> () {
482487
::core::hash::Hash::hash(&self.0, state)
483488
}
@@ -529,6 +534,7 @@ impl ::core::fmt::Debug for PackedUnsizedU8 {
529534
}
530535
#[automatically_derived]
531536
impl ::core::hash::Hash for PackedUnsizedU8 {
537+
#[inline]
532538
fn hash<__H: ::core::hash::Hasher>(&self, state: &mut __H) -> () {
533539
::core::hash::Hash::hash(&self.0, state)
534540
}
@@ -584,6 +590,7 @@ impl<T: ::core::default::Default + Trait, U: ::core::default::Default>
584590
#[automatically_derived]
585591
impl<T: ::core::hash::Hash + Trait, U: ::core::hash::Hash> ::core::hash::Hash
586592
for Generic<T, U> where T::A: ::core::hash::Hash {
593+
#[inline]
587594
fn hash<__H: ::core::hash::Hasher>(&self, state: &mut __H) -> () {
588595
::core::hash::Hash::hash(&self.t, state);
589596
::core::hash::Hash::hash(&self.ta, state);
@@ -701,6 +708,7 @@ impl<T: ::core::hash::Hash + ::core::marker::Copy + Trait,
701708
U: ::core::hash::Hash + ::core::marker::Copy> ::core::hash::Hash for
702709
PackedGeneric<T, U> where T::A: ::core::hash::Hash + ::core::marker::Copy
703710
{
711+
#[inline]
704712
fn hash<__H: ::core::hash::Hasher>(&self, state: &mut __H) -> () {
705713
::core::hash::Hash::hash(&{ self.0 }, state);
706714
::core::hash::Hash::hash(&{ self.1 }, state);
@@ -795,6 +803,7 @@ impl ::core::fmt::Debug for Enum0 {
795803
}
796804
#[automatically_derived]
797805
impl ::core::hash::Hash for Enum0 {
806+
#[inline]
798807
fn hash<__H: ::core::hash::Hasher>(&self, state: &mut __H) -> () {
799808
unsafe { ::core::intrinsics::unreachable() }
800809
}
@@ -861,6 +870,7 @@ impl ::core::fmt::Debug for Enum1 {
861870
}
862871
#[automatically_derived]
863872
impl ::core::hash::Hash for Enum1 {
873+
#[inline]
864874
fn hash<__H: ::core::hash::Hasher>(&self, state: &mut __H) -> () {
865875
match self {
866876
Enum1::Single { x: __self_0 } =>
@@ -937,6 +947,7 @@ impl ::core::default::Default for Fieldless1 {
937947
}
938948
#[automatically_derived]
939949
impl ::core::hash::Hash for Fieldless1 {
950+
#[inline]
940951
fn hash<__H: ::core::hash::Hasher>(&self, state: &mut __H) -> () {}
941952
}
942953
#[automatically_derived]
@@ -1004,6 +1015,7 @@ impl ::core::default::Default for Fieldless {
10041015
}
10051016
#[automatically_derived]
10061017
impl ::core::hash::Hash for Fieldless {
1018+
#[inline]
10071019
fn hash<__H: ::core::hash::Hasher>(&self, state: &mut __H) -> () {
10081020
let __self_tag = ::core::intrinsics::discriminant_value(self);
10091021
::core::hash::Hash::hash(&__self_tag, state)
@@ -1095,6 +1107,7 @@ impl ::core::default::Default for Mixed {
10951107
}
10961108
#[automatically_derived]
10971109
impl ::core::hash::Hash for Mixed {
1110+
#[inline]
10981111
fn hash<__H: ::core::hash::Hasher>(&self, state: &mut __H) -> () {
10991112
let __self_tag = ::core::intrinsics::discriminant_value(self);
11001113
::core::hash::Hash::hash(&__self_tag, state);
@@ -1224,6 +1237,7 @@ impl ::core::fmt::Debug for Fielded {
12241237
}
12251238
#[automatically_derived]
12261239
impl ::core::hash::Hash for Fielded {
1240+
#[inline]
12271241
fn hash<__H: ::core::hash::Hasher>(&self, state: &mut __H) -> () {
12281242
let __self_tag = ::core::intrinsics::discriminant_value(self);
12291243
::core::hash::Hash::hash(&__self_tag, state);
@@ -1345,6 +1359,7 @@ impl<T: ::core::fmt::Debug, U: ::core::fmt::Debug> ::core::fmt::Debug for
13451359
#[automatically_derived]
13461360
impl<T: ::core::hash::Hash, U: ::core::hash::Hash> ::core::hash::Hash for
13471361
EnumGeneric<T, U> {
1362+
#[inline]
13481363
fn hash<__H: ::core::hash::Hasher>(&self, state: &mut __H) -> () {
13491364
let __self_tag = ::core::intrinsics::discriminant_value(self);
13501365
::core::hash::Hash::hash(&__self_tag, state);

0 commit comments

Comments
 (0)