Better performance for MulticastDelegate.Equals (dotnet/coreclr#6113)
authorJames Ko <jamesqko@gmail.com>
Tue, 5 Jul 2016 09:50:52 +0000 (05:50 -0400)
committerJan Kotas <jkotas@microsoft.com>
Tue, 5 Jul 2016 09:50:52 +0000 (11:50 +0200)
Better performance for MulticastDelegate.Equals

Commit migrated from https://github.com/dotnet/coreclr/commit/a8cdb489463200598631c5acacc5aa521e10b139

src/coreclr/src/mscorlib/src/System/MulticastDelegate.cs

index 43545cc..f7d864d 100644 (file)
@@ -7,6 +7,7 @@ namespace System
     using System;
     using System.Reflection;
     using System.Runtime;
+    using System.Runtime.CompilerServices;
     using System.Runtime.Serialization;
     using System.Diagnostics.Contracts;
     using System.Reflection.Emit;
@@ -103,11 +104,18 @@ namespace System
         [System.Security.SecuritySafeCritical]  // auto-generated
         public override sealed bool Equals(Object obj)
         {
-            if (obj == null || !InternalEqualTypes(this, obj))
-                return false;      
-            MulticastDelegate d = obj as MulticastDelegate;
-            if (d == null)
+            if (obj == null)
                 return false;
+            if (object.ReferenceEquals(this, obj))
+                return true;
+            if (!InternalEqualTypes(this, obj))
+                return false;
+            
+            // Since this is a MulticastDelegate and we know
+            // the types are the same, obj should also be a
+            // MulticastDelegate
+            Contract.Assert(obj is MulticastDelegate, "Shouldn't have failed here since we already checked the types are the same!");
+            var d = JitHelpers.UnsafeCast<MulticastDelegate>(obj);
 
             if (_invocationCount != (IntPtr)0) 
             {