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

Optimize WriteEncodedStringAsync #216

Merged
merged 2 commits into from
Jan 26, 2022
Merged
Changes from 1 commit
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
21 changes: 19 additions & 2 deletions src/GraphQLParser/Visitors/SDLPrinter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -998,11 +998,16 @@ private static async ValueTask WriteEncodedStringAsync(TContext context, ROM val
{
await context.WriteAsync("\"").ConfigureAwait(false);

int startIndexOfNotEncodedString = 0;
for (int i = 0; i < value.Span.Length; ++i)
{
char code = value.Span[i];
if (code < ' ')
{
if (startIndexOfNotEncodedString != i)
await context.WriteAsync(value.Slice(startIndexOfNotEncodedString, i - startIndexOfNotEncodedString)).ConfigureAwait(false);
startIndexOfNotEncodedString = i + 1;

if (code == '\b')
await context.WriteAsync("\\b").ConfigureAwait(false);
else if (code == '\f')
Expand All @@ -1017,13 +1022,25 @@ private static async ValueTask WriteEncodedStringAsync(TContext context, ROM val
await context.WriteAsync("\\u" + ((int)code).ToString("X4")).ConfigureAwait(false);
}
else if (code == '\\')
{
if (startIndexOfNotEncodedString != i)
await context.WriteAsync(value.Slice(startIndexOfNotEncodedString, i - startIndexOfNotEncodedString)).ConfigureAwait(false);
startIndexOfNotEncodedString = i + 1;

await context.WriteAsync("\\\\").ConfigureAwait(false);
}
else if (code == '"')
{
if (startIndexOfNotEncodedString != i)
await context.WriteAsync(value.Slice(startIndexOfNotEncodedString, i - startIndexOfNotEncodedString)).ConfigureAwait(false);
startIndexOfNotEncodedString = i + 1;

await context.WriteAsync("\\\"").ConfigureAwait(false);
else
await context.WriteAsync(value.Slice(i, 1)/*code*/).ConfigureAwait(false); // TODO: no method for char
Copy link
Member Author

Choose a reason for hiding this comment

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

Hot path: await + one-symbol slicing

@Shane32 We should use the same approach for comments/block strings

Copy link
Member Author

Choose a reason for hiding this comment

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

So now in case of "normal" strings this loop does nothing just incrementing counter - single write is done at the end of method:
await context.WriteAsync(value.Slice(startIndexOfNotEncodedString, value.Span.Length - startIndexOfNotEncodedString)).ConfigureAwait(false);

}
}

if (startIndexOfNotEncodedString != value.Span.Length)
await context.WriteAsync(value.Slice(startIndexOfNotEncodedString, value.Span.Length - startIndexOfNotEncodedString)).ConfigureAwait(false);
await context.WriteAsync("\"").ConfigureAwait(false);
}
}
Expand Down