From 01b409296ee1301cc37e0914f61a3c905abfe8c6 Mon Sep 17 00:00:00 2001 From: Maryam Ariyan Date: Fri, 7 Jun 2019 22:38:48 -0700 Subject: [PATCH] Apply feedback from JsonElement back into original logic from Utf8JsonReader (dotnet/corefx#38205) use const length for stackalloc but keep using unsafe span stackalloc pattern Commit migrated from https://github.com/dotnet/corefx/commit/20ba99aaf60ba91180f304211b096e1147564a7c --- .../src/System/Text/Json/Document/JsonDocument.cs | 14 +++++++++----- .../src/System/Text/Json/Reader/Utf8JsonReader.cs | 16 ++++++++++------ 2 files changed, 19 insertions(+), 11 deletions(-) diff --git a/src/libraries/System.Text.Json/src/System/Text/Json/Document/JsonDocument.cs b/src/libraries/System.Text.Json/src/System/Text/Json/Document/JsonDocument.cs index 528a55e..3691e26 100644 --- a/src/libraries/System.Text.Json/src/System/Text/Json/Document/JsonDocument.cs +++ b/src/libraries/System.Text.Json/src/System/Text/Json/Document/JsonDocument.cs @@ -275,20 +275,24 @@ namespace System.Text.Json int length = checked(otherText.Length * JsonConstants.MaxExpansionFactorWhileTranscoding); Span otherUtf8Text = length <= JsonConstants.StackallocThreshold ? - stackalloc byte[length] : + stackalloc byte[JsonConstants.StackallocThreshold] : (otherUtf8TextArray = ArrayPool.Shared.Rent(length)); ReadOnlySpan utf16Text = MemoryMarshal.AsBytes(otherText); OperationStatus status = JsonWriterHelper.ToUtf8(utf16Text, otherUtf8Text, out int consumed, out int written); Debug.Assert(status != OperationStatus.DestinationTooSmall); + bool result; if (status > OperationStatus.DestinationTooSmall) // Equivalent to: (status == NeedMoreData || status == InvalidData) { - return false; + result = false; } - Debug.Assert(status == OperationStatus.Done); - Debug.Assert(consumed == utf16Text.Length); + else + { + Debug.Assert(status == OperationStatus.Done); + Debug.Assert(consumed == utf16Text.Length); - bool result = TextEquals(index, otherUtf8Text.Slice(0, written), isPropertyName); + result = TextEquals(index, otherUtf8Text.Slice(0, written), isPropertyName); + } if (otherUtf8TextArray != null) { diff --git a/src/libraries/System.Text.Json/src/System/Text/Json/Reader/Utf8JsonReader.cs b/src/libraries/System.Text.Json/src/System/Text/Json/Reader/Utf8JsonReader.cs index a5874c9..74811ea 100644 --- a/src/libraries/System.Text.Json/src/System/Text/Json/Reader/Utf8JsonReader.cs +++ b/src/libraries/System.Text.Json/src/System/Text/Json/Reader/Utf8JsonReader.cs @@ -501,22 +501,26 @@ namespace System.Text.Json // Cannot create a span directly since it gets passed to instance methods on a ref struct. unsafe { - byte* ptr = stackalloc byte[length]; - otherUtf8Text = new Span(ptr, length); + byte* ptr = stackalloc byte[JsonConstants.StackallocThreshold]; + otherUtf8Text = new Span(ptr, JsonConstants.StackallocThreshold); } } ReadOnlySpan utf16Text = MemoryMarshal.AsBytes(text); OperationStatus status = JsonWriterHelper.ToUtf8(utf16Text, otherUtf8Text, out int consumed, out int written); Debug.Assert(status != OperationStatus.DestinationTooSmall); + bool result; if (status > OperationStatus.DestinationTooSmall) // Equivalent to: (status == NeedMoreData || status == InvalidData) { - return false; + result = false; } - Debug.Assert(status == OperationStatus.Done); - Debug.Assert(consumed == utf16Text.Length); + else + { + Debug.Assert(status == OperationStatus.Done); + Debug.Assert(consumed == utf16Text.Length); - bool result = TextEqualsHelper(otherUtf8Text.Slice(0, written)); + result = TextEqualsHelper(otherUtf8Text.Slice(0, written)); + } if (otherUtf8TextArray != null) { -- 2.7.4