Skip to content
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

Don't use IEnumerable and yield return for GetDefaultInterfaceImplementations #98183

Merged
merged 3 commits into from
Feb 10, 2024

Conversation

jtschuster
Copy link
Member

@jtschuster jtschuster commented Feb 8, 2024

GetDefaultInterfaceImplementations takes the most time of anything in the linker in a hello world app, and allocates ~200mb total. If we refactor to not use yield return and directly add the DIMs to the cache in the method, we speed up by about 20% on a hello world app and use much less memory.

@ghost ghost added the linkable-framework Issues associated with delivering a linker friendly framework label Feb 8, 2024
@dotnet-issue-labeler dotnet-issue-labeler bot added the area-Tools-ILLink .NET linker development as well as trimming analyzers label Feb 8, 2024
@ghost ghost assigned jtschuster Feb 8, 2024
@ghost
Copy link

ghost commented Feb 8, 2024

Tagging subscribers to 'linkable-framework': @eerhardt, @vitek-karas, @LakshanF, @sbomer, @joperezr, @marek-safar
See info in area-owners.md if you want to be subscribed.

Issue Details

GetDefaultInterfaceImplementations takes the most time of anything in the linker in a hello world app, and allocates ~200mb total. If we refactor to not use yield return and directly add the DIMs in the method, we speed up by about 20% on a hello world app and use much less memory.

Author: jtschuster
Assignees: -
Labels:

linkable-framework

Milestone: -

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.

Nice find, thanks!

src/tools/illink/src/linker/Linker/TypeMapInfo.cs Outdated Show resolved Hide resolved
src/tools/illink/src/linker/Linker/TypeMapInfo.cs Outdated Show resolved Hide resolved
src/tools/illink/src/linker/Linker/TypeMapInfo.cs Outdated Show resolved Hide resolved
@jtschuster jtschuster merged commit 8af0b00 into dotnet:main Feb 10, 2024
76 checks passed
@@ -313,8 +311,7 @@ IEnumerable<InterfaceImplementation> GetDefaultInterfaceImplementations (TypeDef
// We haven't found a MethodImpl on the current interface, but one of the interfaces
// this interface requires could still provide it.
if (!foundImpl) {
foreach (var impl in GetDefaultInterfaceImplementations (potentialImplInterface, interfaceMethod))
yield return impl;
FindAndAddDefaultInterfaceImplementations (potentialImplInterface, interfaceMethod);
Copy link
Member

Choose a reason for hiding this comment

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

This is not producing equivalent results.

Previously, we always called AddDefaultInterfaceImplementation (resolvedInterfaceMethod, type, {ThisComesFromTheAlgorithm});

Now we are calling AddDefaultInterfaceImplementation (resolvedInterfaceMethod, {ThisComesFromTheAlgorithm}, {ThisComesFromTheAlgorithm}); because at this spot type is swapped for potentialImplInterface when this recursively calls itself at this spot.

Is that a problem? Or the second parameter doesn't matter for anything? If it doesn't matter, the perf improvement in #98436 can be even better - we don't have to look for default implementation per each class - we can only look for default implementations when MapType is called for an interface and don't worry about default implementations when looking at classes at all.

Copy link
Member Author

Choose a reason for hiding this comment

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

Oh, good catch. We do need the correct 'ImplementingType' for this, so we'll have to make sure the original type is passed to AddDefaultInterfaceImplementation.

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.

3 participants