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

Better performance for MulticastDelegate.Equals - #6113

Merged
jkotas merged 2 commits into
dotnet:masterfrom
jamesqo:patch-3
Jul 5, 2016
Merged

jkotas merged 2 commits into
dotnet:masterfrom
jamesqo:patch-3

Conversation

@jamesqo

@jamesqo jamesqo commented Jul 4, 2016

Copy link
Copy Markdown

cc: @jkotas

@jkotas

jkotas commented Jul 4, 2016

Copy link
Copy Markdown
Member

The actual delegate types inherit from MulticastDelegate, not Delegate. If you would like to improve the fast path for comparing identical delegate instances, it should be better done in MulticastDelegate.Equals. Results from a small microbenchmark would be nice.

@jamesqo

jamesqo commented Jul 4, 2016 •

Copy link
Copy Markdown
Author

@jkotas I see. I googled about MutlicastDelegate and came across this StackOverflow question: http://stackoverflow.com/a/4711164

If System.Delegate can't be inherited from in C# and the CLR is incapable of loading such a type, then why not just have the whole Delegate.Equals (and other methods, like Delegate.GetInvocationList) throw a NotImplemented/SupportedException or Contract.Assert(false)? (I may be interested in doing this in another PR)

edit: Never mind, looks like it's called as base.Equals Actually, it'd still be less obscure to move all of that code into MulticastDelegate since that's the only place it can be called from, so the question still stands.

@jamesqo jamesqo changed the title Short-circuit for reference equality in Delegate.Equals Better performance for MulticastDelegate.Equals Jul 5, 2016
@jamesqo

jamesqo commented Jul 5, 2016

Copy link
Copy Markdown
Author

Here are the numbers for the new MulticastDelegate.Equals: old / new / source code

I added a JitHelpers.UnsafeCast after the InternalEqualTypes since we know the other object has to be a MulticastDelegate, but it doesn't seem to be having much of an impact in the case where the delegates are the same method, but not reference-equal. (I don't know the exact codepath it goes down in the method, so perhaps the latter parts have more of an overhead making the cast insignificant.)

For the delegates being reference-equal, there is a 10x speedup.

@stephentoub

Copy link
Copy Markdown
Member

For the delegates being reference-equal

What about when they're not?

@stephentoub

Copy link
Copy Markdown
Member

there is a 10x speedup

I'm confused. Comparing the old and new results you posted, reference equality looks appx the same between them, and method equality appears to regress 10%. What am I misunderstanding?

@jamesqo

jamesqo commented Jul 5, 2016

Copy link
Copy Markdown
Author

@stephentoub Whoops! Looks like I was actually testing the new code twice, I looked at the wrong copy of System.Private.CoreLib.ni.dll in ILSpy. Will post results for the old implementation soon.

@jamesqo

jamesqo commented Jul 5, 2016

Copy link
Copy Markdown
Author

I've updated the old results. It looks like the new changes actually do make it 10x faster, and have a 10% improvement for the method equality case.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The GetType JIT intrinsic is only optimized in specific usage patterns. This is not one of them.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jkotas I removed the TODO. (It seems kinda unintuitive that you have to define a new function/ P/Invoke to get faster performance than what's written 99% of the time, but whatever.)

@jkotas

jkotas commented Jul 5, 2016

Copy link
Copy Markdown
Member

LGTM. Thanks!

@jkotas
jkotas merged commit a8cdb48 into dotnet:master Jul 5, 2016
@jkotas

jkotas commented Jul 5, 2016

Copy link
Copy Markdown
Member

@jamesqo Could you please make a matching fix in CoreRT as well?

@jamesqo
jamesqo deleted the patch-3 branch July 5, 2016 14:36
picenka21 pushed a commit to picenka21/runtime that referenced this pull request Feb 18, 2022
Better performance for MulticastDelegate.Equals

Commit migrated from dotnet/coreclr@a8cdb48
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants