From fded808124defd9917581f2959d11ec8383193c5 Mon Sep 17 00:00:00 2001 From: James Ko Date: Tue, 5 Jul 2016 05:50:52 -0400 Subject: [PATCH] Better performance for MulticastDelegate.Equals (dotnet/coreclr#6113) Better performance for MulticastDelegate.Equals Commit migrated from https://github.com/dotnet/coreclr/commit/a8cdb489463200598631c5acacc5aa521e10b139 --- src/coreclr/src/mscorlib/src/System/MulticastDelegate.cs | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/src/coreclr/src/mscorlib/src/System/MulticastDelegate.cs b/src/coreclr/src/mscorlib/src/System/MulticastDelegate.cs index 43545cc..f7d864d 100644 --- a/src/coreclr/src/mscorlib/src/System/MulticastDelegate.cs +++ b/src/coreclr/src/mscorlib/src/System/MulticastDelegate.cs @@ -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(obj); if (_invocationCount != (IntPtr)0) { -- 2.7.4