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

Reduce allocations in AbstractProjectExtensionProvider.FilterExtensions #74112

Conversation

ToddGrun
Copy link
Contributor

This ends up reducing allocations in the profile where I repeatedly brough up a lightbulb by about 0.7%.

  1. Got rid of an Enum.ToString that happened on each filtering. Instead changed the code that reads the mef attributes to convert from string -> enum.
  2. Got rid of a a couple Contains calls which were allocating an enumerator
  3. Got rid of some closures by using the args version of WhereAsArray and by creating a separate method to handle the CWT insertion.

*** Prior allocations that no longer show up ***
image

This ends up reducing allocations in the profile where I repeatedly brough up a lightbulb by about 0.7%.

1) Got rid of an Enum.ToString that happened on each filtering. Instead changed the code that reads the mef attributes to convert from string -> enum.
2) Got rid of a a couple Contains calls which were allocating an enumerator
3) Got rid of some closures by using the args version of WhereAsArray and by creating a separate method to handle the CWT insertion.
@ToddGrun ToddGrun requested a review from a team as a code owner June 22, 2024 05:02
@dotnet-issue-labeler dotnet-issue-labeler bot added Area-IDE untriaged Issues and PRs which have not yet been triaged by a lead labels Jun 22, 2024

bool ShouldIncludeExtension(TExtension extension)
static bool ShouldIncludeExtension(TExtension extension, (TextDocument, Func<TExportAttribute, ExtensionInfo>) args)
Copy link
Member

Choose a reason for hiding this comment

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

i have no problem with you inlining this method into WhereAsArray.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Tried it, stared at it for a minute, and I personally like it better in the current form. Will gladly change it though if you feel fairly strongly or have an objective reason.


if (extensionInfo == null)
return true;

if (!extensionInfo.DocumentKinds.Contains(document.Kind.ToString()))
if (-1 == Array.IndexOf(extensionInfo.DocumentKinds, document.Kind))
Copy link
Member

Choose a reason for hiding this comment

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

would prefer a < 0 check. even if doc'ed some apis end up returning different from -1.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done!

}

return new(kinds, attribute.DocumentExtensions);
}
Copy link
Member

@CyrusNajmabadi CyrusNajmabadi Jun 25, 2024

Choose a reason for hiding this comment

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

can we not have a helper for this? (since it seems duplicated).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done!


internal class EnumArrayConverter
{
public static TEnum[] FromStringArray<TEnum>(string[] strings) where TEnum : struct
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
public static TEnum[] FromStringArray<TEnum>(string[] strings) where TEnum : struct
public static TEnum[] FromStringArray<TEnum>(string[] strings) where TEnum : struct, System.Enum

Copy link
Member

Choose a reason for hiding this comment

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

Nite: consider returning ImmutableArray instread if you don't need to mutate hte array.


namespace Microsoft.CodeAnalysis.CodeFixesAndRefactorings;

internal class EnumArrayConverter
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
internal class EnumArrayConverter
internal static class EnumArrayConverter

@ToddGrun ToddGrun merged commit 6a1c377 into dotnet:main Jun 26, 2024
25 checks passed
@dotnet-policy-service dotnet-policy-service bot added this to the Next milestone Jun 26, 2024
@RikkiGibson RikkiGibson modified the milestones: Next, 17.12 P1 Jul 29, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area-IDE untriaged Issues and PRs which have not yet been triaged by a lead
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants