-
Notifications
You must be signed in to change notification settings - Fork 10.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Expand the SyntaxTokenCache to contain more than just whitespace tokens.
- Loading branch information
Showing
3 changed files
with
76 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
74 changes: 74 additions & 0 deletions
74
src/Razor/Microsoft.AspNetCore.Razor.Language/src/Syntax/InternalSyntax/SyntaxTokenCache.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
// Copyright (c) .NET Foundation. All rights reserved. | ||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. | ||
|
||
using System; | ||
using Microsoft.Extensions.Internal; | ||
|
||
namespace Microsoft.AspNetCore.Razor.Language.Syntax.InternalSyntax | ||
{ | ||
// Simplified version of Roslyn's SyntaxNodeCache | ||
internal static class SyntaxTokenCache | ||
{ | ||
private const int CacheSizeBits = 16; | ||
private const int CacheSize = 1 << CacheSizeBits; | ||
private const int CacheMask = CacheSize - 1; | ||
private static readonly Entry[] s_cache = new Entry[CacheSize]; | ||
|
||
private struct Entry | ||
{ | ||
public int Hash { get; } | ||
public SyntaxToken Token { get; } | ||
|
||
internal Entry(int hash, SyntaxToken token) | ||
{ | ||
Hash = hash; | ||
Token = token; | ||
} | ||
} | ||
|
||
public static bool CanBeCached(SyntaxKind kind, params RazorDiagnostic[] diagnostics) | ||
{ | ||
if (diagnostics.Length == 0) | ||
{ | ||
switch (kind) | ||
{ | ||
case SyntaxKind.CharacterLiteral: | ||
case SyntaxKind.Dot: | ||
case SyntaxKind.Identifier: | ||
case SyntaxKind.IntegerLiteral: | ||
case SyntaxKind.Keyword: | ||
case SyntaxKind.NewLine: | ||
case SyntaxKind.RazorCommentStar: | ||
case SyntaxKind.RazorCommentTransition: | ||
case SyntaxKind.StringLiteral: | ||
case SyntaxKind.Transition: | ||
case SyntaxKind.Whitespace: | ||
return true; | ||
} | ||
} | ||
|
||
return false; | ||
} | ||
|
||
public static SyntaxToken GetCachedToken(SyntaxKind kind, string content) | ||
{ | ||
var hash = (kind, content).GetHashCode(); | ||
|
||
// Allow the upper 16 bits to contribute to the index | ||
var indexableHash = hash ^ (hash >> 16); | ||
|
||
var idx = indexableHash & CacheMask; | ||
var e = s_cache[idx]; | ||
|
||
if (e.Hash == hash && e.Token.Kind == kind && e.Token.Content == content) | ||
{ | ||
return e.Token; | ||
} | ||
|
||
var token = new SyntaxToken(kind, content, Array.Empty<RazorDiagnostic>()); | ||
s_cache[idx] = new Entry(hash, token); | ||
|
||
return token; | ||
} | ||
} | ||
} |
46 changes: 0 additions & 46 deletions
46
...zor/Microsoft.AspNetCore.Razor.Language/src/Syntax/InternalSyntax/WhitespaceTokenCache.cs
This file was deleted.
Oops, something went wrong.