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 TextDocumentStates.SelectAs* methods #72159

Merged

Conversation

ToddGrun
Copy link
Contributor

These methods were using an unsized ArrayBuilder to create an ImmutableArray.

Instead, as the size is pre-known in all these methods, we can just use ImmutableArray.CreateBuilder directly.

These two methods were showing up as a combined 0.4% of allocations in the codeanalysis process when inspecting a profile taken while typing a line of text.

Name                                                        	                                                                         Inc %	    Inc
Microsoft.CodeAnalysis.Workspaces!Microsoft.CodeAnalysis.TextDocumentStates`1[System.__Canon].SelectAsArray(class System.Func`3,!!1) 	  0.3	   39,169,344
Microsoft.CodeAnalysis.Workspaces!Microsoft.CodeAnalysis.TextDocumentStates`1[System.__Canon].SelectAsArray(class System.Func`2) 	  0.1	   9,204,744

These methods were using an unsized ArrayBuilder to create an ImmutableArray.

Instead, as the size is pre-known in all these methods, we can just use ImmutableArray.CreateBuilder directly.
@ToddGrun ToddGrun requested a review from a team as a code owner February 17, 2024 02:15
@dotnet-issue-labeler dotnet-issue-labeler bot added Area-IDE untriaged Issues and PRs which have not yet been triaged by a lead labels Feb 17, 2024
@@ -100,38 +100,41 @@ public IEnumerable<TState> GetStatesInCompilationOrder()

public ImmutableArray<TValue> SelectAsArray<TValue>(Func<TState, TValue> selector)
{
using var _ = ArrayBuilder<TValue>.GetInstance(out var builder);
// Directly use ImmutableArray.Builder as we know the final size
var builder = ImmutableArray.CreateBuilder<TValue>(_map.Count);
Copy link
Member

Choose a reason for hiding this comment

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

Previously this was reusing instances from a pool. Now, this will allocate each time.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

A couple thoughts:

  1. Previously, the code called builder.ToImmutable every time, which allocates.
  2. Previously the code would incrementally resize by calling Add. This could cause multiple re-allocations before reaching it's final size. (and have it's final size be up to twice too large)
  3. Previously the code would end up not adding the builder back to the pool if it exceeded the size threshold (fairly common in the scenario I'm testing)
  4. Previously, the code would need to do the overhead of searching the ObjectPool for an object to use.

Copy link
Member

Choose a reason for hiding this comment

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

Makes sense!

@ToddGrun ToddGrun merged commit 086e84a into dotnet:main Feb 19, 2024
27 checks passed
@ghost ghost added this to the Next milestone Feb 19, 2024
@jjonescz jjonescz modified the milestones: Next, 17.10 P2 Feb 27, 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.

4 participants