|
3 | 3 | // See the LICENSE file in the project root for more information. |
4 | 4 |
|
5 | 5 | using System.Collections.Generic; |
| 6 | +using System.Runtime.CompilerServices; |
6 | 7 |
|
7 | 8 | namespace System.Reflection |
8 | 9 | { |
@@ -44,32 +45,20 @@ public virtual Module Module |
44 | 45 | public override bool Equals(object obj) => base.Equals(obj); |
45 | 46 | public override int GetHashCode() => base.GetHashCode(); |
46 | 47 |
|
| 48 | + // Force inline as the true/false ternary takes it above ALWAYS_INLINE size even though the asm ends up smaller |
| 49 | + [MethodImpl(MethodImplOptions.AggressiveInlining)] |
47 | 50 | public static bool operator ==(MemberInfo left, MemberInfo right) |
48 | 51 | { |
49 | | - if (object.ReferenceEquals(left, right)) |
50 | | - return true; |
51 | | - |
52 | | - if ((object)left == null || (object)right == null) |
53 | | - return false; |
54 | | - |
55 | | - Type type1, type2; |
56 | | - MethodBase method1, method2; |
57 | | - FieldInfo field1, field2; |
58 | | - EventInfo event1, event2; |
59 | | - PropertyInfo property1, property2; |
60 | | - |
61 | | - if ((type1 = left as Type) != null && (type2 = right as Type) != null) |
62 | | - return type1 == type2; |
63 | | - else if ((method1 = left as MethodBase) != null && (method2 = right as MethodBase) != null) |
64 | | - return method1 == method2; |
65 | | - else if ((field1 = left as FieldInfo) != null && (field2 = right as FieldInfo) != null) |
66 | | - return field1 == field2; |
67 | | - else if ((event1 = left as EventInfo) != null && (event2 = right as EventInfo) != null) |
68 | | - return event1 == event2; |
69 | | - else if ((property1 = left as PropertyInfo) != null && (property2 = right as PropertyInfo) != null) |
70 | | - return property1 == property2; |
| 52 | + // Test "right" first to allow branch elimination when inlined for null checks (== null) |
| 53 | + // so it can become a simple test |
| 54 | + if (right is null) |
| 55 | + { |
| 56 | + // return true/false not the test result https://github.com/dotnet/coreclr/issues/914 |
| 57 | + return (left is null) ? true : false; |
| 58 | + } |
71 | 59 |
|
72 | | - return false; |
| 60 | + // Quick reference equality test prior to calling the virtual Equality |
| 61 | + return ReferenceEquals(right, left) ? true : right.Equals(left); |
73 | 62 | } |
74 | 63 |
|
75 | 64 | public static bool operator !=(MemberInfo left, MemberInfo right) => !(left == right); |
|
0 commit comments