Skip to content

Cache more SyntaxTokens#12283

Merged
ToddGrun merged 1 commit intodotnet:mainfrom
ToddGrun:dev/toddgrun/CacheMoreSyntaxTokens
Oct 1, 2025
Merged

Cache more SyntaxTokens#12283
ToddGrun merged 1 commit intodotnet:mainfrom
ToddGrun:dev/toddgrun/CacheMoreSyntaxTokens

Conversation

@ToddGrun
Copy link
Contributor

@ToddGrun ToddGrun commented Sep 30, 2025

Increase SyntaxToken cache usage to not limit the SyntaxKinds allowed in the cache.

SyntaxTokens account for about 0.4% of total allocations in the RoslynCodeAnalysisService process in the Cohosting speedometer test.

Test insertion: https://dev.azure.com/devdiv/DevDiv/_git/VS/pullrequest/675292

Allocations of SyntaxTokens decreased over 90% with this change.

Before:
image

After:
image

I don't see why we can't cache more SyntaxTokens than the kinds that we are currently special casing.

Currently, I see about 0.3% of allocations are creating SyntaxTokens in the Cohosting speedometer test. Going to do a test run and see what that looks like with this change.
@ToddGrun ToddGrun requested a review from a team as a code owner September 30, 2025 22:29
Copy link
Member

@DustinCampbell DustinCampbell left a comment

Choose a reason for hiding this comment

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

FWIW, it'd be great if we could be smarter about caching green tokens that have known text. In Roslyn, SyntaxKind is ordered specifically to make it easy to index into an array of tokens with well-known text. Razor's SyntaxKind would need to be reordered more sensibly to allow for that. However, if that were done, we'd only need to hash the tokens that can have different text, such as StringLiteral. This would cut down on cache collisions.

@ToddGrun
Copy link
Contributor Author

ToddGrun commented Oct 1, 2025

FWIW, it'd be great if we could be smarter about caching green tokens that have known text.

That would be nice. I do see some collisions when operating in mudblazor, but the perf numbers came back good enough that I'm not going to worry too much about that right now.

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.

3 participants