Avoid calling GetInvocationList on hot paths #4736
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Description
GetInvocationList on one or more delegates is being called in response to most any input operation. We can instead call GetInvocationList only when the set of delegates changes, and use that list over and over.
Customer Impact
Reduced Delegate[] allocation leading to less pressure on the GC. You can see the impact of this with a simple sample WPF app; in this trace, I just open the app and interact with it for a few seconds. These are the top allocations:

Regression
No
Testing
Just CI
Risk
I'm making an assumption that it's much more common for these events to be invoked than it is for them to be changed (delegates added or removed from the events). If it were more common for them to be changed than for them to be invoked, this would be a deoptimization.