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:
parent
016fd1ee9e
commit
d782a06462
|
|
@ -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 =
|
private static readonly Func<SyntaxToken, bool> IsValidStatementSpacingToken =
|
||||||
IsSpacingTokenIncludingNewLinesAndComments;
|
IsSpacingTokenIncludingNewLinesAndComments;
|
||||||
|
|
||||||
|
|
@ -2622,26 +2643,6 @@ namespace Microsoft.AspNetCore.Razor.Language.Legacy
|
||||||
result.Value == keyword;
|
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
|
protected class Block
|
||||||
{
|
{
|
||||||
public Block(string name, SourceLocation start)
|
public Block(string name, SourceLocation start)
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue