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

Improve allocations from JsonResultSerializer #6650

Merged
merged 1 commit into from
Oct 31, 2023
Merged
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 @@ -32,11 +32,20 @@ public sealed class JsonQueryResultSerializer : IQueryResultSerializer
/// </param>
public JsonQueryResultSerializer(bool indented = false, JavaScriptEncoder? encoder = null)
{
_options = new JsonWriterOptions { Indented = indented, Encoder = encoder };
_options = new JsonWriterOptions
{
Indented = indented,
Encoder = encoder ?? JavaScriptEncoder.UnsafeRelaxedJsonEscaping,
};
}

public unsafe string Serialize(IQueryResult result)
{
if (result is null)
{
throw new ArgumentNullException(nameof(result));
}

using var buffer = new ArrayWriter();

Serialize(result, buffer);
Expand All @@ -49,14 +58,29 @@ public unsafe string Serialize(IQueryResult result)

/// <inheritdoc />
public void Serialize(IQueryResult result, IBufferWriter<byte> writer)
{
if (result is null)
{
throw new ArgumentNullException(nameof(result));
}

if (writer is null)
{
throw new ArgumentNullException(nameof(writer));
}

SerializeInternal(result, writer);
}

private void SerializeInternal(IQueryResult result, IBufferWriter<byte> writer)
{
using var jsonWriter = new Utf8JsonWriter(writer, _options);
WriteResult(jsonWriter, result);
jsonWriter.Flush();
}

/// <inheritdoc />
public async Task SerializeAsync(
public Task SerializeAsync(
IQueryResult result,
Stream stream,
CancellationToken cancellationToken = default)
Expand All @@ -71,11 +95,32 @@ public async Task SerializeAsync(
throw new ArgumentNullException(nameof(stream));
}

await using var writer = new Utf8JsonWriter(stream, _options);
return SerializeInternalAsync(result, stream, cancellationToken);
}

private async Task SerializeInternalAsync(
IQueryResult result,
Stream outputStream,
CancellationToken cancellationToken = default)
{
await using var writer = new Utf8JsonWriter(outputStream, _options);

WriteResult(writer, result);
using var buffer = new ArrayWriter();
SerializeInternal(result, buffer);

#if NETSTANDARD2_0
await outputStream
.WriteAsync(buffer.GetInternalBuffer(), 0, buffer.Length, cancellationToken)
.ConfigureAwait(false);
#else
await outputStream
.WriteAsync(buffer.Body, cancellationToken)
.ConfigureAwait(false);
#endif

await writer.FlushAsync(cancellationToken).ConfigureAwait(false);
await outputStream.FlushAsync(cancellationToken).ConfigureAwait(false);
}

private void WriteResult(Utf8JsonWriter writer, IQueryResult result)
Expand Down