diff --git a/src/Razor/Microsoft.AspNetCore.Razor.Language/src/Legacy/CSharpCodeParser.cs b/src/Razor/Microsoft.AspNetCore.Razor.Language/src/Legacy/CSharpCodeParser.cs index f3b6e22919..7339e9149f 100644 --- a/src/Razor/Microsoft.AspNetCore.Razor.Language/src/Legacy/CSharpCodeParser.cs +++ b/src/Razor/Microsoft.AspNetCore.Razor.Language/src/Legacy/CSharpCodeParser.cs @@ -16,6 +16,27 @@ namespace Microsoft.AspNetCore.Razor.Language.Legacy '@', '!', '<', '/', '?', '[', '>', ']', '=', '"', '\'', '*' }); + // Following four high traffic methods cached as using method groups would cause allocation on every invocation. + protected static readonly Func IsSpacingToken = (token) => + { + return token.Kind == SyntaxKind.Whitespace; + }; + + protected static readonly Func IsSpacingTokenIncludingNewLines = (token) => + { + return IsSpacingToken(token) || token.Kind == SyntaxKind.NewLine; + }; + + protected static readonly Func IsSpacingTokenIncludingComments = (token) => + { + return IsSpacingToken(token) || token.Kind == SyntaxKind.CSharpComment; + }; + + protected static readonly Func IsSpacingTokenIncludingNewLinesAndComments = (token) => + { + return IsSpacingTokenIncludingNewLines(token) || token.Kind == SyntaxKind.CSharpComment; + }; + private static readonly Func IsValidStatementSpacingToken = IsSpacingTokenIncludingNewLinesAndComments; @@ -2622,26 +2643,6 @@ namespace Microsoft.AspNetCore.Razor.Language.Legacy result.Value == keyword; } - protected static bool IsSpacingToken(SyntaxToken token) - { - return token.Kind == SyntaxKind.Whitespace; - } - - protected static bool IsSpacingTokenIncludingNewLines(SyntaxToken token) - { - return IsSpacingToken(token) || token.Kind == SyntaxKind.NewLine; - } - - protected static bool IsSpacingTokenIncludingComments(SyntaxToken token) - { - return IsSpacingToken(token) || token.Kind == SyntaxKind.CSharpComment; - } - - protected static bool IsSpacingTokenIncludingNewLinesAndComments(SyntaxToken token) - { - return IsSpacingTokenIncludingNewLines(token) || token.Kind == SyntaxKind.CSharpComment; - } - protected class Block { public Block(string name, SourceLocation start)