Skip to content

Commit

Permalink
Use pooled Utf8JsonWriter in fast-path async serialization
Browse files Browse the repository at this point in the history
  • Loading branch information
eiriktsarpalis committed Dec 1, 2022
1 parent 0d4374a commit 7546782
Showing 1 changed file with 4 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -81,18 +81,20 @@ internal async Task SerializeAsync(
Debug.Assert(Converter is JsonMetadataServicesConverter<T>);

using var bufferWriter = new PooledByteBufferWriter(Options.DefaultBufferSize);
using var writer = new Utf8JsonWriter(bufferWriter, Options.GetWriterOptions());
Utf8JsonWriter writer = Utf8JsonWriterCache.RentWriter(Options, bufferWriter);

try
{
SerializeHandler(writer, rootValue!);
writer.Flush();
await bufferWriter.WriteToStreamAsync(utf8Json, cancellationToken).ConfigureAwait(false);
}
finally
{
OnRootLevelAsyncSerializationCompleted(writer.BytesCommitted + writer.BytesPending);
Utf8JsonWriterCache.ReturnWriter(writer);
}

await bufferWriter.WriteToStreamAsync(utf8Json, cancellationToken).ConfigureAwait(false);
}
else if (
#if NETCOREAPP
Expand Down

0 comments on commit 7546782

Please sign in to comment.