Skip to content

Commit 978be80

Browse files
authored
Reduce pooled array allocations in the SyntaxParser (#76610)
These pooled array allocations are showing up in the roslyn editing speedometer test as 2.9% of allocations in the code analysis process. The SyntaxParser has pools for holding the BlendedNode and SyntaxToken arrays it uses. However, it sets the size of the pool to be only 2 items. As these arrays are only released once the parser is disposed, and there are potentially quite a few concurrent parses going on during solution load, there ends up being quite a few allocations of these arrays due to earlier allocations not yet being released to the pool. Instead, just switch these object pools to use the default for the array size. Local testing for opening the Roslyn sln showed about 50K SyntaxParser objects constructed, and I was seeing the blended/token arrays allocated 3.5K/10.5K respectively. With this change, I saw those array allocations reduced to 18/54 respectively. Will create a test insertion to get speedometer numbers.
1 parent 77df282 commit 978be80

File tree

1 file changed

+2
-2
lines changed

1 file changed

+2
-2
lines changed

src/Compilers/CSharp/Portable/Parser/SyntaxParser.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@ internal abstract partial class SyntaxParser : IDisposable
3535
private int _resetCount;
3636
private int _resetStart;
3737

38-
private static readonly ObjectPool<BlendedNode[]> s_blendedNodesPool = new ObjectPool<BlendedNode[]>(() => new BlendedNode[32], 2);
39-
private static readonly ObjectPool<ArrayElement<SyntaxToken>[]> s_lexedTokensPool = new ObjectPool<ArrayElement<SyntaxToken>[]>(() => new ArrayElement<SyntaxToken>[CachedTokenArraySize], 2);
38+
private static readonly ObjectPool<BlendedNode[]> s_blendedNodesPool = new ObjectPool<BlendedNode[]>(() => new BlendedNode[32]);
39+
private static readonly ObjectPool<ArrayElement<SyntaxToken>[]> s_lexedTokensPool = new ObjectPool<ArrayElement<SyntaxToken>[]>(() => new ArrayElement<SyntaxToken>[CachedTokenArraySize]);
4040

4141
// Array size held in token pool. This should be large enough to prevent most allocations, but
4242
// not so large as to be wasteful when not in use.

0 commit comments

Comments
 (0)