-
Notifications
You must be signed in to change notification settings - Fork 4.1k
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 the C# lexer ctor #76544
base: main
Are you sure you want to change the base?
Conversation
The lexer ctor shows as about 0.5% of all allocations over the lifetime of the RoslynCodeAnalysisProcess in the csharp editing speedometer tests. There was already the concept of the LexerCache which had some pooling usage in it. This PR moves a couple other members to it to allow for their pooling. This PR is going to be marked as a draft PR until I can get some data from a test insertion indicating whether these changes improve allocations in the speedometer tests.
Test insertion: https://dev.azure.com/devdiv/DevDiv/_git/VS/pullrequest/599798 |
Speedometer results look promising, going to promote out of draft mode. Allocations under Lexer.*ctor in IncPaths: |
@dotnet/roslyn-compiler -- ptal |
@@ -24,6 +25,7 @@ public static SyntaxListBuilder Create() | |||
|
|||
public void Clear() | |||
{ | |||
Array.Clear(_nodes, 0, Count); |
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.
} | ||
|
||
internal void Free() | ||
{ | ||
_keywordKindMap.Free(); | ||
_keywordKindMap = s_keywordKindPool.Allocate(); |
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.
@@ -70,6 +70,8 @@ internal enum XmlDocCommentStyle | |||
|
|||
internal sealed partial class Lexer : AbstractLexer | |||
{ | |||
private static readonly ObjectPool<LexerCache> s_lexerCachePool = new ObjectPool<LexerCache>(() => new LexerCache()); |
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.
|
||
private const int LeadingTriviaCacheInitialCapacity = 128; | ||
private const int TrailingTriviaCacheInitialCapacity = 16; | ||
|
||
internal LexerCache() |
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.
Done with review pass (commit 1) |
The lexer ctor shows as about 0.6% of all allocations over the lifetime of the RoslynCodeAnalysisProcess in the csharp editing speedometer tests.
There was already the concept of the LexerCache which had some pooling usage in it. This PR moves a couple other members to it to allow for their pooling.
Speedometer results look quite promising:
Allocations under Lexer.ctor in IncPaths reduced from 60 MB to 1.5 MB
Allocations under Lexer in IncPaths reduced from 1.57 GB to 1.36 GB