Skip to content

Commit 0dccbfd

Browse files
authored
Remove SyntaxTokenCache.Entry (#12370)
While debugging some related code, I noticed this class didn't use a lock when getting the Entry, which smelled to me like a possible torn read/writes issue (and those suck). I mentioned this to Dustin to make sure I wasn't being goofy, and he thought that might be a concern here too, but then raised the valid point that the reason for it being a struct (to have a hash in addition to the SyntaxToken) wasn't even necessary.
1 parent 984ece1 commit 0dccbfd

File tree

1 file changed

+6
-18
lines changed
  • src/Compiler/Microsoft.CodeAnalysis.Razor.Compiler/src/Language/Syntax/InternalSyntax

1 file changed

+6
-18
lines changed

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

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -11,22 +11,10 @@ internal sealed class SyntaxTokenCache
1111
private const int CacheMask = CacheSize - 1;
1212
public static readonly SyntaxTokenCache Instance = new();
1313

14-
private readonly Entry[] _cache = new Entry[CacheSize];
14+
private readonly SyntaxToken[] _cache = new SyntaxToken[CacheSize];
1515

1616
internal SyntaxTokenCache() { }
1717

18-
private readonly struct Entry
19-
{
20-
public int Hash { get; }
21-
public SyntaxToken? Token { get; }
22-
23-
internal Entry(int hash, SyntaxToken token)
24-
{
25-
Hash = hash;
26-
Token = token;
27-
}
28-
}
29-
3018
public bool CanBeCached(SyntaxKind kind, params RazorDiagnostic[] diagnostics)
3119
=> diagnostics.Length == 0;
3220

@@ -38,15 +26,15 @@ public SyntaxToken GetCachedToken(SyntaxKind kind, string content)
3826
var indexableHash = hash ^ (hash >> 16);
3927

4028
var idx = indexableHash & CacheMask;
41-
var e = _cache[idx];
29+
var token = _cache[idx];
4230

43-
if (e.Hash == hash && e.Token != null && e.Token.Kind == kind && e.Token.Content == content)
31+
if (token != null && token.Kind == kind && token.Content == content)
4432
{
45-
return e.Token;
33+
return token;
4634
}
4735

48-
var token = new SyntaxToken(kind, content, []);
49-
_cache[idx] = new Entry(hash, token);
36+
token = new SyntaxToken(kind, content, []);
37+
_cache[idx] = token;
5038

5139
return token;
5240
}

0 commit comments

Comments
 (0)