Skip to content

Commit

Permalink
Use PooledByteBufferWriter to save 1K in trimmed System.Memory.dll (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
steveharter authored Apr 27, 2021
1 parent 3de9cda commit abdb7ed
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 17 deletions.
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -14,12 +12,15 @@ public abstract partial class JsonNode
/// <returns>JSON representation of current instance.</returns>
public string ToJsonString(JsonSerializerOptions? options = null)
{
var output = new ArrayBufferWriter<byte>();
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);
}

/// <summary>
Expand All @@ -43,15 +44,15 @@ public override string ToString()
}
}

var options = new JsonWriterOptions { Indented = true };
var output = new ArrayBufferWriter<byte>();

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());
}
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
@@ -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
{
Expand Down

0 comments on commit abdb7ed

Please sign in to comment.