-
Notifications
You must be signed in to change notification settings - Fork 5k
Improve performance on virtual method processing #100897
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
- Make _virtual_methods a dictionary to prevent duplicates due to the Scope object in the tuple - Exit early when possible in IsInterfaceImplementationMarkedRecursively - Return a list from TypeMapInfo.GetRecursiveInterfaces and iterate over with a regular for loop - Iterate over _typesWithInterfaces with a for loop instead of copying to an array
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It also exits early when possible in the IsInterfaceImplementationMarkedRecursively method.
Looks like you reverted that change, but there are still some left-over related diffs.
I'm good with the changes to how we iterate over overrides. I haven't thought of a case where we would modify the set while iterating over it, but the for loop doesn't hurt. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good find, thank you!
@@ -220,7 +220,7 @@ public partial class MarkStep : IStep | |||
public MarkStep () | |||
{ | |||
_methods = new Queue<(MethodDefinition, DependencyInfo, MessageOrigin)> (); | |||
_virtual_methods = new HashSet<(MethodDefinition, MarkScopeStack.Scope)> (); | |||
_virtual_methods = new Dictionary<MethodDefinition, MarkScopeStack.Scope> (); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is the perf improvement due to the change of key? If so is MethodDefinition
actually a good option for lookups? It doesn't implement IEquatable
and doesn't have a custom GetHashcode
. Would a custom record using what makes a MethodDefinition
identity better then?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The trimmer only creates a single MethodDefinition
object for each method definition in metadata, so object
's implementation of GetHashCode
and Equals
are fine here.
The _virtual_methods cache had duplicates due to the Scope item in the tuple, which made ProcessVirtualMethods do significantly more work. This PR makes the cache a dictionary, with TryAdd's instead of Add to only add each method once. These changes made caused the overrides cache in TypeMapInfo to be modified during iteration, so a for loop is used instead of a foreach loop. This should be find since the list will always be append-only. I noticed we copy _typesWithInterfaces to an array for the same reason, though since that's also append-only we can iterate with a for loop there, too. Fixes dotnet/sdk#40060.
Fixes dotnet/sdk#40060.
The _virtual_methods cache had duplicates due to the
Scope
item in the tuple, which made ProcessVirtualMethods do significantly more work. This PR makes the cache a dictionary, with TryAdd's instead of Add to only add each method once.It also exits early when possible in the IsInterfaceImplementationMarkedRecursively method.These changes made caused theoverrides
cache in TypeMapInfo to be modified during iteration, so afor
loop is used instead of a foreach loop. This should be find since the list will always be append-only. I noticed we copy _typesWithInterfaces to an array for the same reason, though since that's also append-only we can iterate with afor
loop there, too.On my machine, this reduces the trim time of the aspnetcore benchmarks from ~80 seconds to ~35 seconds.