File tree Expand file tree Collapse file tree 1 file changed +10
-5
lines changed
src/Compiler/Microsoft.AspNetCore.Razor.Language/src/Legacy Expand file tree Collapse file tree 1 file changed +10
-5
lines changed Original file line number Diff line number Diff line change @@ -11,23 +11,21 @@ internal static partial class LegacySyntaxNodeExtensions
1111{
1212 private struct NodeStack : IDisposable
1313 {
14- [ ThreadStatic ]
15- private static SyntaxNode [ ] ? g_nodeStack ;
14+ private static readonly ObjectPool < SyntaxNode [ ] > s_stackPool = new ( ( ) => new SyntaxNode [ 64 ] ) ;
1615
1716 private readonly SyntaxNode [ ] _stack ;
1817 private int _stackPtr ;
1918
2019 public NodeStack ( IEnumerable < SyntaxNode > nodes )
2120 {
22- _stack = g_nodeStack ??= new SyntaxNode [ 64 ] ;
21+ _stack = s_stackPool . Allocate ( ) ;
2322 _stackPtr = - 1 ;
2423
2524 foreach ( var node in nodes )
2625 {
2726 if ( ++ _stackPtr == _stack . Length )
2827 {
2928 Array . Resize ( ref _stack , _stack . Length * 2 ) ;
30- g_nodeStack = _stack ;
3129 }
3230
3331 _stack [ _stackPtr ] = node ;
@@ -41,6 +39,13 @@ public bool IsEmpty
4139 => _stackPtr < 0 ;
4240
4341 public void Dispose ( )
44- => Array . Clear ( _stack , 0 , _stack . Length ) ;
42+ {
43+ // Return only reasonably-sized stacks to the pool.
44+ if ( _stack . Length < 256 )
45+ {
46+ Array . Clear ( _stack , 0 , _stack . Length ) ;
47+ s_stackPool . Free ( _stack ) ;
48+ }
49+ }
4550 }
4651}
You can’t perform that action at this time.
0 commit comments