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 from creating MissingTokenWithTrivia objects #75404

Conversation

ToddGrun
Copy link
Contributor

@ToddGrun ToddGrun commented Oct 5, 2024

This method accounts for 0.7% of allocations in the roslyn csharp editing speedometer test profile I'm looking at. In addition to caching tokens < LastTokenWithWellKnownText similar to what is already done, the SyntaxKind.IdentifierToken case is handled as it's very commonly hit too. Local testing of opening LanguageParser and typing in a new method reduced the number of MissingTokenWithTrivia from over 1 million to zero.

*** allocations in speedometer profile ***
image

This method accounts for 0.7% of allocations in the roslyn csharp editing speedometer test profile I'm looking at. In addition to caching tokens < LastTokenWithWellKnownText similar to what is already done, the SyntaxKind.IdentifierToken case is handled as it's very commonly hit too. Local testing of opening LanguageParser and typing in a new method reduced the number of MissingTokenWithTrivia from over 1 million to zero.
@ToddGrun ToddGrun requested a review from a team as a code owner October 5, 2024 05:04
@dotnet-issue-labeler dotnet-issue-labeler bot added Area-Compilers untriaged Issues and PRs which have not yet been triaged by a lead labels Oct 5, 2024
@@ -118,6 +118,20 @@ internal static SyntaxToken Create(SyntaxKind kind, GreenNode leading, GreenNode
return new SyntaxTokenWithTrivia(kind, leading, trailing);
}

internal static SyntaxToken CreateMissing(SyntaxKind kind)
Copy link
Member

Choose a reason for hiding this comment

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

Would it be better to instead have just one CreateMissing(SyntaxKind kind, GreenNode leading, GreenNode trailing) and do the optimization if leading is null && trailing is null?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm flexible to whatever you folks want, but that's the way I started and I switched to this because:

  1. Two less null checks in the 99% case that I see called
  2. This mimics what the two Create methods do right above it by having one that handles leading/trailing and one that doesn't take them in.

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 2)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area-Compilers 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