Skip to content

Reduce allocations during analysis result creation.#82139

Merged
ToddGrun merged 2 commits into
dotnet:mainfrom
ToddGrun:dev/toddgrun/ReduceAllocationsDuringAnalysisResultCreation
Feb 2, 2026
Merged

Reduce allocations during analysis result creation.#82139
ToddGrun merged 2 commits into
dotnet:mainfrom
ToddGrun:dev/toddgrun/ReduceAllocationsDuringAnalysisResultCreation

Conversation

@ToddGrun

@ToddGrun ToddGrun commented Jan 24, 2026

Copy link
Copy Markdown
Contributor

The local ImmutableHashSet usage in this method accounts for 0.9% of total allocations in the devhub process in the razor completion speedometer test. Local testing of mudblazor showed on average about 130 analyzers were added to this set per call. A standard HashSet is more efficient wrt memory usage and query performance than an ImmutableHashSet.

*** Previous allocations ***
image

The local ImmutableHashSet usage in this method accounts for 0.9% of total allocations in the devhub process in the razor completion speedometer test. Local testing of mudblazor showed on average about 130 analyzers were added to this set per call. A standard HashSet is more efficient wrt memory usage and query performance than an ImmutableHashSet. The GetImmutableArray methods were modified to take in a IReadOnlyCollection instead of an ImmutableHashSet to maintain their contract of not being able to change that input set.
@ToddGrun ToddGrun requested a review from a team as a code owner January 24, 2026 14:43
@ToddGrun

Copy link
Copy Markdown
Contributor Author

/pr-val

@github-actions

Copy link
Copy Markdown
Contributor

View PR Validation Run triggered by @ToddGrun

Parameters
  • Validation Type: pr-val
  • Pipeline ID: 8972
  • Pipeline Version: main
  • PR Number: 82139
  • Commit SHA: 9b52cbb02d1a03b00d1b4d4ea5fedd9cfd9ba116
  • Source Branch: dev/toddgrun/ReduceAllocationsDuringAnalysisResultCreation
  • Target Branch: main
  • Build ID: 13163073

@CyrusNajmabadi

Copy link
Copy Markdown
Contributor

to maintain their contract of not being able to change that input set.

I'm fine not wrapping in that case. Just doc they won't change the inputs. It's all internal.

@ToddGrun

Copy link
Copy Markdown
Contributor Author

/pr-val

@github-actions

Copy link
Copy Markdown
Contributor

View PR Validation Run triggered by @ToddGrun

Parameters
  • Validation Type: pr-val
  • Pipeline ID: 8972
  • Pipeline Version: main
  • PR Number: 82139
  • Commit SHA: 5e8735f190035c695d34ce05e960e858d56925bc
  • Source Branch: dev/toddgrun/ReduceAllocationsDuringAnalysisResultCreation
  • Target Branch: main
  • Build ID: 13166311

/// <param name="localDiagnosticsOpt">Diagnostic map to operate on</param>
private static ImmutableDictionary<TKey, ImmutableDictionary<DiagnosticAnalyzer, ImmutableArray<Diagnostic>>> GetImmutable<TKey>(
ImmutableHashSet<DiagnosticAnalyzer> analyzers,
HashSet<DiagnosticAnalyzer> analyzers, // Will not be modified

@AlekseyTs AlekseyTs Jan 26, 2026

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

HashSet

Could we use IReadOnlySet instead? Since we want the set to remain unchanged. #Closed

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I changed this away from IReadOnlyCollection in commit 2 due to @CyrusNajmabadi 's request to just use HashSet and to comment it. I can change the PR comment to better reflect commit 2's state wrt this, or I can change back to what was done in commit 1.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I changed this away from IReadOnlyCollection in commit 2

I am not suggesting to use this type

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

HashSet doesn't derive from IReadOnlySet, but rather from IReadOnlyCollection.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

HashSet doesn't derive from IReadOnlySet, but rather from IReadOnlyCollection.

It does according to this page https://learn.microsoft.com/en-us/dotnet/api/system.collections.generic.hashset-1?view=net-10.0. Perhaps this depends on the target framework?

@AlekseyTs

Copy link
Copy Markdown
Contributor

The GetImmutableArray methods were modified to take in a IReadOnlyCollection instead of an ImmutableHashSet to maintain their contract of not being able to change that input set.

It looks like this doesn't reflect the final state of the PR

@AlekseyTs

Copy link
Copy Markdown
Contributor

Done with review pass (commit 2)

@AlekseyTs AlekseyTs left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

LGTM (commit 2)

@AlekseyTs

Copy link
Copy Markdown
Contributor

@dotnet/roslyn-compiler For a second review

@ToddGrun

Copy link
Copy Markdown
Contributor Author

/pr-val

@github-actions

Copy link
Copy Markdown
Contributor

View PR Validation Run triggered by @ToddGrun

Parameters
  • Validation Type: pr-val
  • Pipeline ID: 8972
  • Pipeline Version: main
  • PR Number: 82139
  • Commit SHA: 5e8735f190035c695d34ce05e960e858d56925bc
  • Source Branch: dev/toddgrun/ReduceAllocationsDuringAnalysisResultCreation
  • Target Branch: main
  • Build ID: 13173244

@ToddGrun

Copy link
Copy Markdown
Contributor Author

@dotnet/roslyn-compiler -- ptal for second review

@ToddGrun

ToddGrun commented Feb 1, 2026

Copy link
Copy Markdown
Contributor Author

@dotnet/roslyn-compiler -- trivial change still in need of second review

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants