Skip to content
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

Reduce allocations from creating MissingTokenWithTrivia objects #75404

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 @@ -146,7 +146,7 @@ internal static SyntaxToken Token(GreenNode leading, SyntaxKind kind, string tex

internal static SyntaxToken MissingToken(SyntaxKind kind)
{
return SyntaxToken.CreateMissing(kind, null, null);
return SyntaxToken.CreateMissing(kind);
}

internal static SyntaxToken MissingToken(GreenNode leading, SyntaxKind kind, GreenNode trailing)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ internal static SyntaxToken Create(SyntaxKind kind)
throw new ArgumentException(string.Format(CSharpResources.ThisMethodCanOnlyBeUsedToCreateTokens, kind), nameof(kind));
}

return CreateMissing(kind, null, null);
return CreateMissing(kind);
}

return s_tokensWithNoTrivia[(int)kind].Value;
Expand Down Expand Up @@ -118,6 +118,20 @@ internal static SyntaxToken Create(SyntaxKind kind, GreenNode leading, GreenNode
return new SyntaxTokenWithTrivia(kind, leading, trailing);
}

internal static SyntaxToken CreateMissing(SyntaxKind kind)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it be better to instead have just one CreateMissing(SyntaxKind kind, GreenNode leading, GreenNode trailing) and do the optimization if leading is null && trailing is null?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm flexible to whatever you folks want, but that's the way I started and I switched to this because:

  1. Two less null checks in the 99% case that I see called
  2. This mimics what the two Create methods do right above it by having one that handles leading/trailing and one that doesn't take them in.

{
if (kind <= LastTokenWithWellKnownText)
{
return s_missingTokensWithNoTrivia[(int)kind].Value;
}
else if (kind == SyntaxKind.IdentifierToken)
{
return s_missingIdentifierTokenWithNoTrivia;
}

return new MissingTokenWithTrivia(kind, leading: null, trailing: null);
}

internal static SyntaxToken CreateMissing(SyntaxKind kind, GreenNode leading, GreenNode trailing)
{
return new MissingTokenWithTrivia(kind, leading, trailing);
Expand All @@ -131,6 +145,9 @@ internal static SyntaxToken CreateMissing(SyntaxKind kind, GreenNode leading, Gr
private static readonly ArrayElement<SyntaxToken>[] s_tokensWithElasticTrivia = new ArrayElement<SyntaxToken>[(int)LastTokenWithWellKnownText + 1];
private static readonly ArrayElement<SyntaxToken>[] s_tokensWithSingleTrailingSpace = new ArrayElement<SyntaxToken>[(int)LastTokenWithWellKnownText + 1];
private static readonly ArrayElement<SyntaxToken>[] s_tokensWithSingleTrailingCRLF = new ArrayElement<SyntaxToken>[(int)LastTokenWithWellKnownText + 1];
private static readonly ArrayElement<SyntaxToken>[] s_missingTokensWithNoTrivia = new ArrayElement<SyntaxToken>[(int)LastTokenWithWellKnownText + 1];

private static readonly SyntaxToken s_missingIdentifierTokenWithNoTrivia = new MissingTokenWithTrivia(SyntaxKind.IdentifierToken, leading: null, trailing: null);

static SyntaxToken()
{
Expand All @@ -140,6 +157,7 @@ static SyntaxToken()
s_tokensWithElasticTrivia[(int)kind].Value = new SyntaxTokenWithTrivia(kind, SyntaxFactory.ElasticZeroSpace, SyntaxFactory.ElasticZeroSpace);
s_tokensWithSingleTrailingSpace[(int)kind].Value = new SyntaxTokenWithTrivia(kind, null, SyntaxFactory.Space);
s_tokensWithSingleTrailingCRLF[(int)kind].Value = new SyntaxTokenWithTrivia(kind, null, SyntaxFactory.CarriageReturnLineFeed);
s_missingTokensWithNoTrivia[(int)kind].Value = new MissingTokenWithTrivia(kind, leading: null, trailing: null);
}
}

Expand Down