From: Maryam Ariyan Date: Sat, 8 Jun 2019 05:38:48 +0000 (-0700) Subject: Apply feedback from JsonElement back into original logic from Utf8JsonReader (dotnet... X-Git-Tag: submit/tizen/20210909.063632~11031^2~1352 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=01b409296ee1301cc37e0914f61a3c905abfe8c6;p=platform%2Fupstream%2Fdotnet%2Fruntime.git 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 --- 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) {