Apply feedback from JsonElement back into original logic from Utf8JsonReader (dotnet...
authorMaryam Ariyan <maryam.ariyan@microsoft.com>
Sat, 8 Jun 2019 05:38:48 +0000 (22:38 -0700)
committerGitHub <noreply@github.com>
Sat, 8 Jun 2019 05:38:48 +0000 (22:38 -0700)
use const length for stackalloc but keep using unsafe span stackalloc pattern

Commit migrated from https://github.com/dotnet/corefx/commit/20ba99aaf60ba91180f304211b096e1147564a7c

src/libraries/System.Text.Json/src/System/Text/Json/Document/JsonDocument.cs
src/libraries/System.Text.Json/src/System/Text/Json/Reader/Utf8JsonReader.cs

index 528a55e..3691e26 100644 (file)
@@ -275,20 +275,24 @@ namespace System.Text.Json
 
             int length = checked(otherText.Length * JsonConstants.MaxExpansionFactorWhileTranscoding);
             Span<byte> otherUtf8Text = length <= JsonConstants.StackallocThreshold ?
-                stackalloc byte[length] :
+                stackalloc byte[JsonConstants.StackallocThreshold] :
                 (otherUtf8TextArray = ArrayPool<byte>.Shared.Rent(length));
 
             ReadOnlySpan<byte> 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)
             {
index a5874c9..74811ea 100644 (file)
@@ -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<byte>(ptr, length);
+                    byte* ptr = stackalloc byte[JsonConstants.StackallocThreshold];
+                    otherUtf8Text = new Span<byte>(ptr, JsonConstants.StackallocThreshold);
                 }
             }
 
             ReadOnlySpan<byte> 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)
             {