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 string allocations from GetCategoryBasedDotnetAnalyzerDiagnosticSeverityKey #72163

Merged
merged 9 commits into from
Feb 23, 2024

Conversation

Youssef1313
Copy link
Member

Fixes #72161

The idea here is to avoid calling GetCategoryBasedDotnetAnalyzerDiagnosticSeverityKey in a loop. This is done by first calling GetCategoryBasedDotnetAnalyzerDiagnosticSeverityKey once before the loop, then passing it to TryGetSeverityFromBulkConfiguration

@Youssef1313 Youssef1313 requested a review from a team as a code owner February 17, 2024 12:13
@dotnet-issue-labeler dotnet-issue-labeler bot added Area-Analyzers untriaged Issues and PRs which have not yet been triaged by a lead labels Feb 17, 2024
@ghost ghost added the Community The pull request was submitted by a contributor who is not a Microsoft employee. label Feb 17, 2024
@AlekseyTs
Copy link
Contributor

@dotnet/dotnet-analyzers Please review

@AlekseyTs AlekseyTs requested a review from a team February 19, 2024 15:44
Copy link
Contributor

@mavasani mavasani left a comment

Choose a reason for hiding this comment

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

Thanks

private static string GetCategoryBasedDotnetAnalyzerDiagnosticSeverityKey(string category)
=> $"{DotnetAnalyzerDiagnosticPrefix}.{CategoryPrefix}-{category}.{SeveritySuffix}";
=> s_categoryToSeverityKeyMap.GetOrAdd(category, category => $"{DotnetAnalyzerDiagnosticPrefix}.{CategoryPrefix}-{category}.{SeveritySuffix}");
Copy link
Contributor

@AlekseyTs AlekseyTs Feb 20, 2024

Choose a reason for hiding this comment

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

category

Who controls these values? Is it some predefined set, or is it something that is coming from users? #Closed

Copy link
Member Author

@Youssef1313 Youssef1313 Feb 20, 2024

Choose a reason for hiding this comment

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

@AlekseyTs This method is all about constructing the key. The values for these keys are read from .editorconfig/.globalconfig from AnalyzerOptions. Not sure if that answers your question?

Copy link
Contributor

Choose a reason for hiding this comment

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

So, the values are user controlled and a user will be able to grow s_categoryToSeverityKeyMap dictionary without any limit. Is this correct?

Copy link
Member Author

Choose a reason for hiding this comment

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

@AlekseyTs The keys are defined by the analyzers themselves. I don't think it's realistic for a build to have too many analyzers with too many different categories. In fact, lots of analyzers share the same category.

Copy link
Contributor

@AlekseyTs AlekseyTs Feb 20, 2024

Choose a reason for hiding this comment

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

It looks like the dictionary is going to be alive for as long as the process is alive. It could be shared between different compilations. Theoretically, it might be possible to create more and more compilations to keep the dictionary grow. If that is the case, I would be more comfortable to have an explicit cap on the amount of keys cached (for example, keep certain amount of most recently used keys), or have the cache tied to a compilation (goes away once compilation goes away).

Copy link
Member Author

Choose a reason for hiding this comment

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

Sharing between compilations was actually intended per review comment #72163 (comment)

Different compilations will still share most categories. I wouldn't expect all categories out there to exceed 20 or something.

Copy link
Contributor

Choose a reason for hiding this comment

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

I wouldn't expect all categories out there to exceed 20 or something.

In a perfect world, yes. But we should also think about a worst case scenario. If we are certain about the amount of categories we will have to support, let's explicitly cap the size of the cache, let's say to 50 items. For example, we have ConcurrentLruCache type.

@AlekseyTs
Copy link
Contributor

@Youssef1313 It looks like there are CI failures

Copy link
Contributor

@AlekseyTs AlekseyTs left a comment

Choose a reason for hiding this comment

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

LGTM (commit 8)

@AlekseyTs AlekseyTs merged commit c63b305 into dotnet:main Feb 23, 2024
24 checks passed
@AlekseyTs
Copy link
Contributor

@Youssef1313 Thanks for the contribution

@Youssef1313 Youssef1313 deleted the issues/72161 branch February 24, 2024 00:23
@jjonescz jjonescz added this to the 17.10 P2 milestone Feb 27, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area-Analyzers Community The pull request was submitted by a contributor who is not a Microsoft employee. 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.

Too much string allocations in GetCategoryBasedDotnetAnalyzerDiagnosticSeverityKey
4 participants