Skip to content

Commit 0814f7a

Browse files
authored
Cache more SyntaxTokens (#12283)
Increase SyntaxToken cache usage to not limit the SyntaxKinds allowed in the cache. SyntaxTokens account for about 0.4% of total allocations in the RoslynCodeAnalysisService process in the Cohosting speedometer test. Allocations of SyntaxTokens decreased over 90% with this change. See PR for more details of the before/after numbers.
1 parent 8f21442 commit 0814f7a

File tree

2 files changed

+14
-33
lines changed

2 files changed

+14
-33
lines changed

src/Compiler/Microsoft.CodeAnalysis.Razor.Compiler/src/Language/Legacy/HtmlTokenizer.cs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -130,10 +130,15 @@ protected override string GetTokenContent(SyntaxKind type)
130130
break;
131131
}
132132
}
133-
134-
if (tokenLength == 2 && type == SyntaxKind.NewLine)
133+
else if (tokenLength == 2)
135134
{
136-
return "\r\n";
135+
switch (type)
136+
{
137+
case SyntaxKind.NewLine:
138+
return "\r\n";
139+
case SyntaxKind.DoubleHyphen:
140+
return "--";
141+
}
137142
}
138143

139144
return base.GetTokenContent(type);

src/Compiler/Microsoft.CodeAnalysis.Razor.Compiler/src/Language/Syntax/InternalSyntax/SyntaxTokenCache.cs

Lines changed: 6 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
// Licensed to the .NET Foundation under one or more agreements.
22
// The .NET Foundation licenses this file to you under the MIT license.
33

4-
using System;
5-
64
namespace Microsoft.AspNetCore.Razor.Language.Syntax.InternalSyntax;
75

86
// Simplified version of Roslyn's SyntaxNodeCache
@@ -12,7 +10,8 @@ internal sealed class SyntaxTokenCache
1210
private const int CacheSize = 1 << CacheSizeBits;
1311
private const int CacheMask = CacheSize - 1;
1412
public static readonly SyntaxTokenCache Instance = new();
15-
private static readonly Entry[] s_cache = new Entry[CacheSize];
13+
14+
private readonly Entry[] _cache = new Entry[CacheSize];
1615

1716
internal SyntaxTokenCache() { }
1817

@@ -29,30 +28,7 @@ internal Entry(int hash, SyntaxToken token)
2928
}
3029

3130
public bool CanBeCached(SyntaxKind kind, params RazorDiagnostic[] diagnostics)
32-
{
33-
if (diagnostics.Length == 0)
34-
{
35-
switch (kind)
36-
{
37-
case SyntaxKind.CharacterLiteral:
38-
case SyntaxKind.Dot:
39-
case SyntaxKind.Identifier:
40-
case SyntaxKind.IntegerLiteral:
41-
case SyntaxKind.NumericLiteral:
42-
case SyntaxKind.Keyword:
43-
case SyntaxKind.NewLine:
44-
case SyntaxKind.RazorCommentStar:
45-
case SyntaxKind.RazorCommentTransition:
46-
case SyntaxKind.StringLiteral:
47-
case SyntaxKind.Transition:
48-
case SyntaxKind.Whitespace:
49-
case SyntaxKind.EndOfFile:
50-
return true;
51-
}
52-
}
53-
54-
return false;
55-
}
31+
=> diagnostics.Length == 0;
5632

5733
public SyntaxToken GetCachedToken(SyntaxKind kind, string content)
5834
{
@@ -62,15 +38,15 @@ public SyntaxToken GetCachedToken(SyntaxKind kind, string content)
6238
var indexableHash = hash ^ (hash >> 16);
6339

6440
var idx = indexableHash & CacheMask;
65-
var e = s_cache[idx];
41+
var e = _cache[idx];
6642

6743
if (e.Hash == hash && e.Token != null && e.Token.Kind == kind && e.Token.Content == content)
6844
{
6945
return e.Token;
7046
}
7147

72-
var token = new SyntaxToken(kind, content, Array.Empty<RazorDiagnostic>());
73-
s_cache[idx] = new Entry(hash, token);
48+
var token = new SyntaxToken(kind, content, []);
49+
_cache[idx] = new Entry(hash, token);
7450

7551
return token;
7652
}

0 commit comments

Comments
 (0)