Decrease allocations from using method groups (dotnet/aspnetcore-tooling#1896)

* Previous optimization didn't help as much as intended.

By specifying a method group as the arguments to method like ReadWhile, an allocation was still occurring. Instead, cache the Func as a member in the class and use it instead of the method group.\n\nCommit migrated from 2e6aa150bc
This commit is contained in:
Todd Grunke 2020-05-13 16:00:34 -07:00 committed by GitHub
parent 016fd1ee9e
commit d782a06462
1 changed files with 21 additions and 20 deletions

View File

@ -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<SyntaxToken, bool> IsSpacingToken = (token) =>
{
return token.Kind == SyntaxKind.Whitespace;
};
protected static readonly Func<SyntaxToken, bool> IsSpacingTokenIncludingNewLines = (token) =>
{
return IsSpacingToken(token) || token.Kind == SyntaxKind.NewLine;
};
protected static readonly Func<SyntaxToken, bool> IsSpacingTokenIncludingComments = (token) =>
{
return IsSpacingToken(token) || token.Kind == SyntaxKind.CSharpComment;
};
protected static readonly Func<SyntaxToken, bool> IsSpacingTokenIncludingNewLinesAndComments = (token) =>
{
return IsSpacingTokenIncludingNewLines(token) || token.Kind == SyntaxKind.CSharpComment;
};
private static readonly Func<SyntaxToken, bool> 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)