Skip to content

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

Merged
merged 8 commits into from
Apr 12, 2024

Conversation

jtschuster
Copy link
Member

@jtschuster jtschuster commented Apr 11, 2024

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 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.

On my machine, this reduces the trim time of the aspnetcore benchmarks from ~80 seconds to ~35 seconds.

- 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
@jtschuster jtschuster requested a review from marek-safar as a code owner April 11, 2024 01:25
@dotnet-issue-labeler dotnet-issue-labeler bot added the area-Tools-ILLink .NET linker development as well as trimming analyzers label Apr 11, 2024
@dotnet-policy-service dotnet-policy-service bot added the linkable-framework Issues associated with delivering a linker friendly framework label Apr 11, 2024
@jtschuster jtschuster requested a review from agocke April 11, 2024 21:59
@jtschuster jtschuster removed the linkable-framework Issues associated with delivering a linker friendly framework label Apr 11, 2024
@dotnet-policy-service dotnet-policy-service bot added the linkable-framework Issues associated with delivering a linker friendly framework label Apr 11, 2024
Copy link
Member

@sbomer sbomer left a 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.

@sbomer
Copy link
Member

sbomer commented Apr 11, 2024

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.

Copy link
Member

@sbomer sbomer left a 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> ();
Copy link
Member

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?

Copy link
Member Author

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.

@jtschuster jtschuster merged commit 7b18be5 into dotnet:main Apr 12, 2024
78 of 80 checks passed
matouskozak pushed a commit to matouskozak/runtime that referenced this pull request Apr 30, 2024
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.
@github-actions github-actions bot locked and limited conversation to collaborators May 13, 2024
@sebastienros
Copy link
Member

Quick update on this PR, this definitely fixed the regression

image

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-Tools-ILLink .NET linker development as well as trimming analyzers linkable-framework Issues associated with delivering a linker friendly framework
Projects
None yet
Development

Successfully merging this pull request may close these issues.

PublishTrimmed build time regression.
3 participants