Skip to content

Move IsSpacingToken perf optimizations down to base class #22555

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
1 commit merged into from
Jun 4, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,27 +16,6 @@ internal class CSharpCodeParser : TokenizerBackedParser<CSharpTokenizer>
'@', '!', '<', '/', '?', '[', '>', ']', '=', '"', '\'', '*'
});

// 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;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ public MarkupBlockSyntax ParseBlock()
return null;
}

AcceptWhile(IsSpacingToken(includeNewLines: true));
AcceptWhile(IsSpacingTokenIncludingNewLines);
builder.Add(OutputAsMarkupLiteral());

if (At(SyntaxKind.OpenAngle))
Expand Down Expand Up @@ -683,7 +683,7 @@ private MarkupStartTagSyntax ParseStartTag(
var bookmark = CurrentStart.AbsoluteIndex;

// Skip whitespace
ReadWhile(IsSpacingToken(includeNewLines: true));
ReadWhile(IsSpacingTokenIncludingNewLines);

// Open Angle
if (At(SyntaxKind.OpenAngle) && NextIs(SyntaxKind.ForwardSlash))
Expand Down Expand Up @@ -754,7 +754,7 @@ private MarkupStartTagSyntax ParseStartTextTag(SyntaxToken openAngleToken, out M
SyntaxToken forwardSlashToken = null;
SyntaxToken closeAngleToken = null;

AcceptWhile(IsSpacingToken(includeNewLines: false));
AcceptWhile(IsSpacingToken);
miscAttributeContentBuilder.Add(OutputAsMarkupLiteral());

if (At(SyntaxKind.CloseAngle) ||
Expand Down Expand Up @@ -1442,7 +1442,7 @@ private bool ParseCData(in SyntaxListBuilder<RazorSyntaxNode> builder)

private void ParseDoubleTransition(in SyntaxListBuilder<RazorSyntaxNode> builder)
{
AcceptWhile(IsSpacingToken(includeNewLines: true));
AcceptWhile(IsSpacingTokenIncludingNewLines);
builder.Add(OutputAsMarkupLiteral());

// First transition
Expand All @@ -1463,7 +1463,7 @@ private void ParseCodeTransition(in SyntaxListBuilder<RazorSyntaxNode> builder)
// We don't want to write it to output.
Context.NullGenerateWhitespaceAndNewLine = false;
SpanContext.ChunkGenerator = SpanChunkGenerator.Null;
AcceptWhile(IsSpacingToken(includeNewLines: false));
AcceptWhile(IsSpacingToken);
if (At(SyntaxKind.NewLine))
{
AcceptAndMoveNext();
Expand Down Expand Up @@ -1549,7 +1549,7 @@ private void ParseRazorCommentWithLeadingAndTrailingWhitespace(in SyntaxListBuil
// We don't want to write it to output.
Context.NullGenerateWhitespaceAndNewLine = false;
SpanContext.ChunkGenerator = SpanChunkGenerator.Null;
AcceptWhile(IsSpacingToken(includeNewLines: false));
AcceptWhile(IsSpacingToken);
if (At(SyntaxKind.NewLine))
{
AcceptAndMoveNext();
Expand Down Expand Up @@ -1596,7 +1596,7 @@ private void ParseRazorCommentWithLeadingAndTrailingWhitespace(in SyntaxListBuil
(At(SyntaxKind.NewLine) ||
(At(SyntaxKind.Whitespace) && NextIs(SyntaxKind.NewLine))))
{
AcceptWhile(IsSpacingToken(includeNewLines: false));
AcceptWhile(IsSpacingToken);
AcceptAndMoveNext();
SpanContext.ChunkGenerator = SpanChunkGenerator.Null;
builder.Add(OutputAsMarkupEphemeralLiteral());
Expand All @@ -1611,7 +1611,7 @@ private void ParseMisc(in SyntaxListBuilder<RazorSyntaxNode> builder)
// We don't want to write it to output.
Context.NullGenerateWhitespaceAndNewLine = false;
SpanContext.ChunkGenerator = SpanChunkGenerator.Null;
AcceptWhile(IsSpacingToken(includeNewLines: false));
AcceptWhile(IsSpacingToken);
if (At(SyntaxKind.NewLine))
{
AcceptAndMoveNext();
Expand All @@ -1620,7 +1620,7 @@ private void ParseMisc(in SyntaxListBuilder<RazorSyntaxNode> builder)
builder.Add(OutputAsMarkupEphemeralLiteral());
}

AcceptWhile(IsSpacingToken(includeNewLines: true));
AcceptWhile(IsSpacingTokenIncludingNewLines);
}

private bool ScriptTagExpectsHtml(MarkupStartTagSyntax tagBlock)
Expand Down Expand Up @@ -2111,11 +2111,6 @@ internal static bool IsCommentContentEndingInvalid(IEnumerable<SyntaxToken> sequ
return false;
}

protected static Func<SyntaxToken, bool> IsSpacingToken(bool includeNewLines)
{
return token => token.Kind == SyntaxKind.Whitespace || (includeNewLines && token.Kind == SyntaxKind.NewLine);
}

internal static bool IsHyphen(SyntaxToken token)
{
return token.Kind == SyntaxKind.Text && token.Content == "-";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,27 @@ internal abstract class TokenizerBackedParser<TTokenizer> : ParserBase
private readonly TokenizerView<TTokenizer> _tokenizer;
private SyntaxListBuilder<SyntaxToken>? _tokenBuilder;

// 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;
};

protected TokenizerBackedParser(LanguageCharacteristics<TTokenizer> language, ParserContext context)
: base(context)
{
Expand Down