Skip to content

Compiler: Optimize SyntaxList<TNode>, SyntaxTokenList and PooledArrayBuilder<T> operations #11909

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

Merged
merged 17 commits into from
Jun 11, 2025

Conversation

DustinCampbell
Copy link
Member

@DustinCampbell DustinCampbell commented Jun 3, 2025

I noticed that Razor's new SyntaxTokenList always creates a new array of ArrayElement<GreenNode> whenever a mutation method is called, even if there are few enough elements that an array is unnecessary. This pull request addresses that and brings the work done in SyntaxTokenList to SyntaxList<TNode>. Here is a summary of the changes:

  • Introduce ImmutableArray<T>.Builder.InsertRange(...) extension method that takes a ReadOnlySpan<T>. For some reason, there's an AddRange overload for ReadOnlySpan<T> but no matching InsertRange overload.
  • Add PooledArrayBuilder<T>.InsertRange(...) methods.
  • Update SyntaxTokenList to use PooledArrayBuilder<SyntaxToken> for mutation operations. This should result in fewer ArrayElement<GreenNode>[] allocations.
  • Bring SyntaxList<TNode> in line with SyntaxTokenList and use PooledArrayBuilder<TNode> for mutation operations rather than a List<TNode>.
  • Loads of unit tests.

CI Build: https://dev.azure.com/dnceng/internal/_build/results?buildId=2722495&view=results
Test Insertion: https://dev.azure.com/devdiv/DevDiv/_git/VS/pullrequest/640753
Toolset Test Run: https://dev.azure.com/dnceng/internal/_build/results?buildId=2722496&view=results

It turns out that ImmutableArray<T>.Builder does not provide an InsertRange(...) method that takes a ReadOnlySpan<T> -- though it *does* provide an AddRange(...) overload that takes a ReadOnlySpan<T>.

This change adds an extension method for that InsertRange(...) overload.
This change ensures that PooledArrayBuilder<T> provides the same overloads for AddRange(...) and InsertRange(...).
This is now unused.
@DustinCampbell DustinCampbell requested review from a team as code owners June 3, 2025 20:38
@DustinCampbell DustinCampbell changed the title Optimize SyntaxList<TNode>, SyntaxTokenList and PooledArrayBuilder<T> operations Compiler: Optimize SyntaxList<TNode>, SyntaxTokenList and PooledArrayBuilder<T> operations Jun 3, 2025
@DustinCampbell
Copy link
Member Author

Pinging @dotnet/razor-compiler for a second review.

@DustinCampbell DustinCampbell merged commit 302ae2b into dotnet:main Jun 11, 2025
11 checks passed
@DustinCampbell DustinCampbell deleted the syntaxlist-optimizations branch June 11, 2025 22:56
@dotnet-policy-service dotnet-policy-service bot added this to the Next milestone Jun 11, 2025
@DustinCampbell
Copy link
Member Author

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants