Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.

Commit 2cf506f

Browse files
committed
Maintain left.Equals(right) order for equality for compat
1 parent 110835b commit 2cf506f

File tree

11 files changed

+77
-32
lines changed

11 files changed

+77
-32
lines changed

src/System.Private.CoreLib/shared/System/Globalization/SortVersion.cs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,6 @@ public override int GetHashCode()
7777
return m_NlsVersion * 7 | m_SortId.GetHashCode();
7878
}
7979

80-
// Force inline as the true/false ternary takes it above ALWAYS_INLINE size even though the asm ends up smaller
8180
[MethodImpl(MethodImplOptions.AggressiveInlining)]
8281
public static bool operator ==(SortVersion left, SortVersion right)
8382
{
@@ -89,7 +88,13 @@ public override int GetHashCode()
8988
return (left is null) ? true : false;
9089
}
9190

92-
return right.Equals(left);
91+
// Try fast reference equality and opposite null check prior to calling the slower virtual Equals
92+
if ((object)left == (object)right)
93+
{
94+
return true;
95+
}
96+
97+
return (left is null) ? false : left.Equals(right);
9398
}
9499

95100
public static bool operator !=(SortVersion left, SortVersion right)

src/System.Private.CoreLib/shared/System/Reflection/Assembly.cs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,6 @@ public override string ToString()
148148
public override bool Equals(object o) => base.Equals(o);
149149
public override int GetHashCode() => base.GetHashCode();
150150

151-
// Force inline as the true/false ternary takes it above ALWAYS_INLINE size even though the asm ends up smaller
152151
[MethodImpl(MethodImplOptions.AggressiveInlining)]
153152
public static bool operator ==(Assembly left, Assembly right)
154153
{
@@ -160,8 +159,13 @@ public override string ToString()
160159
return (left is null) ? true : false;
161160
}
162161

163-
// Quick reference equality test prior to calling the virtual Equality
164-
return ReferenceEquals(right, left) ? true : right.Equals(left);
162+
// Try fast reference equality and opposite null check prior to calling the slower virtual Equals
163+
if ((object)left == (object)right)
164+
{
165+
return true;
166+
}
167+
168+
return (left is null) ? false : left.Equals(right);
165169
}
166170

167171
public static bool operator !=(Assembly left, Assembly right)

src/System.Private.CoreLib/shared/System/Reflection/ConstructorInfo.cs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ protected ConstructorInfo() { }
2222
public override bool Equals(object obj) => base.Equals(obj);
2323
public override int GetHashCode() => base.GetHashCode();
2424

25-
// Force inline as the true/false ternary takes it above ALWAYS_INLINE size even though the asm ends up smaller
2625
[MethodImpl(MethodImplOptions.AggressiveInlining)]
2726
public static bool operator ==(ConstructorInfo left, ConstructorInfo right)
2827
{
@@ -34,8 +33,13 @@ protected ConstructorInfo() { }
3433
return (left is null) ? true : false;
3534
}
3635

37-
// Quick reference equality test prior to calling the virtual Equality
38-
return ReferenceEquals(right, left) ? true : right.Equals(left);
36+
// Try fast reference equality and opposite null check prior to calling the slower virtual Equals
37+
if ((object)left == (object)right)
38+
{
39+
return true;
40+
}
41+
42+
return (left is null) ? false : left.Equals(right);
3943
}
4044

4145
public static bool operator !=(ConstructorInfo left, ConstructorInfo right) => !(left == right);

src/System.Private.CoreLib/shared/System/Reflection/EventInfo.cs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,6 @@ public virtual void RemoveEventHandler(object target, Delegate handler)
100100
public override bool Equals(object obj) => base.Equals(obj);
101101
public override int GetHashCode() => base.GetHashCode();
102102

103-
// Force inline as the true/false ternary takes it above ALWAYS_INLINE size even though the asm ends up smaller
104103
[MethodImpl(MethodImplOptions.AggressiveInlining)]
105104
public static bool operator ==(EventInfo left, EventInfo right)
106105
{
@@ -112,8 +111,13 @@ public virtual void RemoveEventHandler(object target, Delegate handler)
112111
return (left is null) ? true : false;
113112
}
114113

115-
// Quick reference equality test prior to calling the virtual Equality
116-
return ReferenceEquals(right, left) ? true : right.Equals(left);
114+
// Try fast reference equality and opposite null check prior to calling the slower virtual Equals
115+
if ((object)left == (object)right)
116+
{
117+
return true;
118+
}
119+
120+
return (left is null) ? false : left.Equals(right);
117121
}
118122

119123
public static bool operator !=(EventInfo left, EventInfo right) => !(left == right);

src/System.Private.CoreLib/shared/System/Reflection/FieldInfo.cs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@ protected FieldInfo() { }
4040
public override bool Equals(object obj) => base.Equals(obj);
4141
public override int GetHashCode() => base.GetHashCode();
4242

43-
// Force inline as the true/false ternary takes it above ALWAYS_INLINE size even though the asm ends up smaller
4443
[MethodImpl(MethodImplOptions.AggressiveInlining)]
4544
public static bool operator ==(FieldInfo left, FieldInfo right)
4645
{
@@ -52,8 +51,13 @@ protected FieldInfo() { }
5251
return (left is null) ? true : false;
5352
}
5453

55-
// Quick reference equality test prior to calling the virtual Equality
56-
return ReferenceEquals(right, left) ? true : right.Equals(left);
54+
// Try fast reference equality and opposite null check prior to calling the slower virtual Equals
55+
if ((object)left == (object)right)
56+
{
57+
return true;
58+
}
59+
60+
return (left is null) ? false : left.Equals(right);
5761
}
5862

5963
public static bool operator !=(FieldInfo left, FieldInfo right) => !(left == right);

src/System.Private.CoreLib/shared/System/Reflection/MemberInfo.cs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@ public virtual Module Module
4545
public override bool Equals(object obj) => base.Equals(obj);
4646
public override int GetHashCode() => base.GetHashCode();
4747

48-
// Force inline as the true/false ternary takes it above ALWAYS_INLINE size even though the asm ends up smaller
4948
[MethodImpl(MethodImplOptions.AggressiveInlining)]
5049
public static bool operator ==(MemberInfo left, MemberInfo right)
5150
{
@@ -57,8 +56,13 @@ public virtual Module Module
5756
return (left is null) ? true : false;
5857
}
5958

60-
// Quick reference equality test prior to calling the virtual Equality
61-
return ReferenceEquals(right, left) ? true : right.Equals(left);
59+
// Try fast reference equality and opposite null check prior to calling the slower virtual Equals
60+
if ((object)left == (object)right)
61+
{
62+
return true;
63+
}
64+
65+
return (left is null) ? false : left.Equals(right);
6266
}
6367

6468
public static bool operator !=(MemberInfo left, MemberInfo right) => !(left == right);

src/System.Private.CoreLib/shared/System/Reflection/MethodBase.cs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,6 @@ public bool IsConstructor
6363
public override bool Equals(object obj) => base.Equals(obj);
6464
public override int GetHashCode() => base.GetHashCode();
6565

66-
// Force inline as the true/false ternary takes it above ALWAYS_INLINE size even though the asm ends up smaller
6766
[MethodImpl(MethodImplOptions.AggressiveInlining)]
6867
public static bool operator ==(MethodBase left, MethodBase right)
6968
{
@@ -75,8 +74,13 @@ public bool IsConstructor
7574
return (left is null) ? true : false;
7675
}
7776

78-
// Quick reference equality test prior to calling the virtual Equality
79-
return ReferenceEquals(right, left) ? true : right.Equals(left);
77+
// Try fast reference equality and opposite null check prior to calling the slower virtual Equals
78+
if ((object)left == (object)right)
79+
{
80+
return true;
81+
}
82+
83+
return (left is null) ? false : left.Equals(right);
8084
}
8185

8286
public static bool operator !=(MethodBase left, MethodBase right) => !(left == right);

src/System.Private.CoreLib/shared/System/Reflection/MethodInfo.cs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ protected MethodInfo() { }
2929
public override bool Equals(object obj) => base.Equals(obj);
3030
public override int GetHashCode() => base.GetHashCode();
3131

32-
// Force inline as the true/false ternary takes it above ALWAYS_INLINE size even though the asm ends up smaller
3332
[MethodImpl(MethodImplOptions.AggressiveInlining)]
3433
public static bool operator ==(MethodInfo left, MethodInfo right)
3534
{
@@ -41,8 +40,13 @@ protected MethodInfo() { }
4140
return (left is null) ? true : false;
4241
}
4342

44-
// Quick reference equality test prior to calling the virtual Equality
45-
return ReferenceEquals(right, left) ? true : right.Equals(left);
43+
// Try fast reference equality and opposite null check prior to calling the slower virtual Equals
44+
if ((object)left == (object)right)
45+
{
46+
return true;
47+
}
48+
49+
return (left is null) ? false : left.Equals(right);
4650
}
4751

4852
public static bool operator !=(MethodInfo left, MethodInfo right) => !(left == right);

src/System.Private.CoreLib/shared/System/Reflection/Module.cs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,6 @@ public virtual Type[] FindTypes(TypeFilter filter, object filterCriteria)
116116
public override bool Equals(object o) => base.Equals(o);
117117
public override int GetHashCode() => base.GetHashCode();
118118

119-
// Force inline as the true/false ternary takes it above ALWAYS_INLINE size even though the asm ends up smaller
120119
[MethodImpl(MethodImplOptions.AggressiveInlining)]
121120
public static bool operator ==(Module left, Module right)
122121
{
@@ -128,8 +127,13 @@ public virtual Type[] FindTypes(TypeFilter filter, object filterCriteria)
128127
return (left is null) ? true : false;
129128
}
130129

131-
// Quick reference equality test prior to calling the virtual Equality
132-
return ReferenceEquals(right, left) ? true : right.Equals(left);
130+
// Try fast reference equality and opposite null check prior to calling the slower virtual Equals
131+
if ((object)left == (object)right)
132+
{
133+
return true;
134+
}
135+
136+
return (left is null) ? false : left.Equals(right);
133137
}
134138

135139
public static bool operator !=(Module left, Module right) => !(left == right);

src/System.Private.CoreLib/shared/System/Reflection/PropertyInfo.cs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,6 @@ protected PropertyInfo() { }
5959
public override bool Equals(object obj) => base.Equals(obj);
6060
public override int GetHashCode() => base.GetHashCode();
6161

62-
// Force inline as the true/false ternary takes it above ALWAYS_INLINE size even though the asm ends up smaller
6362
[MethodImpl(MethodImplOptions.AggressiveInlining)]
6463
public static bool operator ==(PropertyInfo left, PropertyInfo right)
6564
{
@@ -71,8 +70,13 @@ protected PropertyInfo() { }
7170
return (left is null) ? true : false;
7271
}
7372

74-
// Quick reference equality test prior to calling the virtual Equality
75-
return ReferenceEquals(right, left) ? true : right.Equals(left);
73+
// Try fast reference equality and opposite null check prior to calling the slower virtual Equals
74+
if ((object)left == (object)right)
75+
{
76+
return true;
77+
}
78+
79+
return (left is null) ? false : left.Equals(right);
7680
}
7781

7882
public static bool operator !=(PropertyInfo left, PropertyInfo right) => !(left == right);

src/System.Private.CoreLib/shared/System/Version.cs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -406,7 +406,6 @@ private static bool TryParseComponent(ReadOnlySpan<char> component, string compo
406406
return int.TryParse(component, NumberStyles.Integer, CultureInfo.InvariantCulture, out parsedComponent) && parsedComponent >= 0;
407407
}
408408

409-
// Force inline as the true/false ternary takes it above ALWAYS_INLINE size even though the asm ends up smaller
410409
[MethodImpl(MethodImplOptions.AggressiveInlining)]
411410
public static bool operator ==(Version v1, Version v2)
412411
{
@@ -418,8 +417,13 @@ private static bool TryParseComponent(ReadOnlySpan<char> component, string compo
418417
return (v1 is null) ? true : false;
419418
}
420419

421-
// Quick reference equality test prior to calling the virtual Equality
422-
return ReferenceEquals(v2, v1) ? true : v2.Equals(v1);
420+
// Try fast reference equality and opposite null check prior to calling the slower virtual Equals
421+
if ((object)v1 == (object)v2)
422+
{
423+
return true;
424+
}
425+
426+
return (v1 is null) ? false : v1.Equals(v2);
423427
}
424428

425429
public static bool operator !=(Version v1, Version v2)

0 commit comments

Comments
 (0)