From abdb7ed8fbae28fcf7bc8270252cb46884d5d41c Mon Sep 17 00:00:00 2001 From: Steve Harter Date: Tue, 27 Apr 2021 12:59:38 -0500 Subject: [PATCH] Use PooledByteBufferWriter to save 1K in trimmed System.Memory.dll (#51896) --- .../src/System/Text/Json/Node/JsonNode.To.cs | 27 ++++++++++--------- .../src/System/Text/Json/ThrowHelper.Node.cs | 4 --- 2 files changed, 14 insertions(+), 17 deletions(-) diff --git a/src/libraries/System.Text.Json/src/System/Text/Json/Node/JsonNode.To.cs b/src/libraries/System.Text.Json/src/System/Text/Json/Node/JsonNode.To.cs index 7be46a1db4855..eca2e4b99e573 100644 --- a/src/libraries/System.Text.Json/src/System/Text/Json/Node/JsonNode.To.cs +++ b/src/libraries/System.Text.Json/src/System/Text/Json/Node/JsonNode.To.cs @@ -1,8 +1,6 @@ // Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. -using System.Buffers; - namespace System.Text.Json.Node { public abstract partial class JsonNode @@ -14,12 +12,15 @@ public abstract partial class JsonNode /// JSON representation of current instance. public string ToJsonString(JsonSerializerOptions? options = null) { - var output = new ArrayBufferWriter(); - using (var writer = new Utf8JsonWriter(output, options == null ? default(JsonWriterOptions) : options.GetWriterOptions())) + using (var output = new PooledByteBufferWriter(JsonSerializerOptions.BufferSizeDefault)) { - WriteTo(writer, options); + using (var writer = new Utf8JsonWriter(output, options == null ? default(JsonWriterOptions) : options.GetWriterOptions())) + { + WriteTo(writer, options); + } + + return JsonHelpers.Utf8GetString(output.WrittenMemory.ToArray()); } - return JsonHelpers.Utf8GetString(output.WrittenSpan); } /// @@ -43,15 +44,15 @@ public override string ToString() } } - var options = new JsonWriterOptions { Indented = true }; - var output = new ArrayBufferWriter(); - - using (var writer = new Utf8JsonWriter(output, options)) + using (var output = new PooledByteBufferWriter(JsonSerializerOptions.BufferSizeDefault)) { - WriteTo(writer); - } + using (var writer = new Utf8JsonWriter(output, new JsonWriterOptions { Indented = true })) + { + WriteTo(writer); + } - return JsonHelpers.Utf8GetString(output.WrittenSpan); + return JsonHelpers.Utf8GetString(output.WrittenMemory.ToArray()); + } } /// diff --git a/src/libraries/System.Text.Json/src/System/Text/Json/ThrowHelper.Node.cs b/src/libraries/System.Text.Json/src/System/Text/Json/ThrowHelper.Node.cs index 8528cf14e4a6a..0c0415b5bca81 100644 --- a/src/libraries/System.Text.Json/src/System/Text/Json/ThrowHelper.Node.cs +++ b/src/libraries/System.Text.Json/src/System/Text/Json/ThrowHelper.Node.cs @@ -1,12 +1,8 @@ // Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. -using System.Buffers; -using System.Diagnostics; using System.Diagnostics.CodeAnalysis; -using System.Reflection; using System.Runtime.CompilerServices; -using System.Text.Json.Serialization; namespace System.Text.Json {