@@ -11,33 +11,40 @@ namespace Microsoft.AspNetCore.Razor.Language.Legacy;
11
11
12
12
internal sealed class ClassifiedSpanVisitor : SyntaxWalker
13
13
{
14
- private static readonly ObjectPool < ImmutableArray < ClassifiedSpanInternal > . Builder > Pool = DefaultPool . Create ( Policy . Instance , size : 5 ) ;
14
+ private static readonly ObjectPool < ClassifiedSpanVisitor > Pool = DefaultPool . Create ( Policy . Instance , size : 5 ) ;
15
15
16
- private readonly RazorSourceDocument _source ;
17
16
private readonly ImmutableArray < ClassifiedSpanInternal > . Builder _spans ;
18
17
18
+ private RazorSourceDocument _source = null ! ;
19
19
private SyntaxNode ? _currentBlock ;
20
20
private SourceSpan ? _currentBlockSpan ;
21
21
private BlockKindInternal _currentBlockKind ;
22
22
23
- private ClassifiedSpanVisitor ( RazorSourceDocument source , ImmutableArray < ClassifiedSpanInternal > . Builder spans )
23
+ private ClassifiedSpanVisitor ( )
24
24
{
25
- _source = source ;
26
- _spans = spans ;
25
+ _spans = ImmutableArray . CreateBuilder < ClassifiedSpanInternal > ( ) ;
26
+ _source = null ! ;
27
+ }
27
28
29
+ private void Initialize ( RazorSourceDocument source )
30
+ {
31
+ _source = source ;
28
32
_currentBlockKind = BlockKindInternal . Markup ;
29
33
}
30
34
31
35
public static ImmutableArray < ClassifiedSpanInternal > VisitRoot ( RazorSyntaxTree syntaxTree )
32
36
{
33
- using var _ = Pool . GetPooledObject ( out var builder ) ;
37
+ using var _ = Pool . GetPooledObject ( out var visitor ) ;
34
38
35
- var visitor = new ClassifiedSpanVisitor ( syntaxTree . Source , builder ) ;
39
+ visitor . Initialize ( syntaxTree . Source ) ;
36
40
visitor . Visit ( syntaxTree . Root ) ;
37
41
38
- return builder . ToImmutableAndClear ( ) ;
42
+ return visitor . GetSpansAndClear ( ) ;
39
43
}
40
44
45
+ private ImmutableArray < ClassifiedSpanInternal > GetSpansAndClear ( )
46
+ => _spans . ToImmutableAndClear ( ) ;
47
+
41
48
public override void VisitRazorCommentBlock ( RazorCommentBlockSyntax node )
42
49
{
43
50
using ( CommentBlock ( node ) )
@@ -426,7 +433,23 @@ private void AddSpan(SyntaxToken token, SpanKindInternal kind, AcceptedCharacter
426
433
private void AddSpan ( SourceSpan span , SpanKindInternal kind , AcceptedCharactersInternal acceptedCharacters )
427
434
=> _spans . Add ( new ( span , CurrentBlockSpan , kind , _currentBlockKind , acceptedCharacters ) ) ;
428
435
429
- private sealed class Policy : IPooledObjectPolicy < ImmutableArray < ClassifiedSpanInternal > . Builder >
436
+ private void Reset ( )
437
+ {
438
+ _spans . Clear ( ) ;
439
+
440
+ if ( _spans . Capacity > Policy . MaximumObjectSize )
441
+ {
442
+ // Differs from ArrayBuilderPool.Policy's behavior as we allow our array to grow significantly larger
443
+ _spans . Capacity = 0 ;
444
+ }
445
+
446
+ _source = null ! ;
447
+ _currentBlock = null ! ;
448
+ _currentBlockSpan = null ;
449
+ _currentBlockKind = BlockKindInternal . Markup ;
450
+ }
451
+
452
+ private sealed class Policy : IPooledObjectPolicy < ClassifiedSpanVisitor >
430
453
{
431
454
public static readonly Policy Instance = new ( ) ;
432
455
@@ -438,17 +461,11 @@ private Policy()
438
461
{
439
462
}
440
463
441
- public ImmutableArray < ClassifiedSpanInternal > . Builder Create ( ) => ImmutableArray . CreateBuilder < ClassifiedSpanInternal > ( ) ;
464
+ public ClassifiedSpanVisitor Create ( ) => new ( ) ;
442
465
443
- public bool Return ( ImmutableArray < ClassifiedSpanInternal > . Builder builder )
466
+ public bool Return ( ClassifiedSpanVisitor visitor )
444
467
{
445
- builder . Clear ( ) ;
446
-
447
- if ( builder . Capacity > MaximumObjectSize )
448
- {
449
- // Differs from ArrayBuilderPool.Policy's behavior as we allow our array to grow significantly larger
450
- builder . Capacity = 0 ;
451
- }
468
+ visitor . Reset ( ) ;
452
469
453
470
return true ;
454
471
}
0 commit comments