-
Notifications
You must be signed in to change notification settings - Fork 4k
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
Reduce allocations from creating MissingTokenWithTrivia objects #75404
Conversation
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.
@@ -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) |
There was a problem hiding this comment.
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
?
There was a problem hiding this comment.
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:
- Two less null checks in the 99% case that I see called
- 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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM (commit 2)
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 ***