Skip to content

Commit

Permalink
Follow-up feedback from dotnet#54186.
Browse files Browse the repository at this point in the history
  • Loading branch information
CodeBlanch authored and eiriktsarpalis committed Jul 16, 2021
1 parent 62fa1f8 commit 5c4d4fd
Show file tree
Hide file tree
Showing 25 changed files with 89 additions and 68 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,8 @@ internal static class JsonConstants
public const int MinimumDateTimeParseLength = 10; // YYYY-MM-DD
public const int MaximumEscapedDateTimeOffsetParseLength = MaxExpansionFactorWhileEscaping * MaximumDateTimeOffsetParseLength;

public const int MaximumLiteralLength = 5; // Must be able to fit null, true, & false.

// Encoding Helpers
public const char HighSurrogateStart = '\ud800';
public const char HighSurrogateEnd = '\udbff';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public static byte[] GetEscapedPropertyNameSection(ReadOnlySpan<byte> utf8Value,
int length = JsonWriterHelper.GetMaxEscapedLength(utf8Value.Length, firstEscapeIndexVal);

Span<byte> escapedValue = length <= JsonConstants.StackallocThreshold ?
stackalloc byte[length] :
(stackalloc byte[JsonConstants.StackallocThreshold]).Slice(0, length) :
(valueArray = ArrayPool<byte>.Shared.Rent(length));

JsonWriterHelper.EscapeString(utf8Value, escapedValue, firstEscapeIndexVal, encoder, out int written);
Expand Down Expand Up @@ -66,7 +66,7 @@ public static byte[] GetEscapedPropertyNameSection(ReadOnlySpan<byte> utf8Value,
int length = JsonWriterHelper.GetMaxEscapedLength(utf8Value.Length, firstEscapeIndexVal);

Span<byte> escapedValue = length <= JsonConstants.StackallocThreshold ?
stackalloc byte[length] :
(stackalloc byte[JsonConstants.StackallocThreshold]).Slice(0, length) :
(valueArray = ArrayPool<byte>.Shared.Rent(length));

JsonWriterHelper.EscapeString(utf8Value, escapedValue, firstEscapeIndexVal, encoder, out int written);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public static bool TryGetUnescapedBase64Bytes(ReadOnlySpan<byte> utf8Source, int
byte[]? unescapedArray = null;

Span<byte> utf8Unescaped = utf8Source.Length <= JsonConstants.StackallocThreshold ?
stackalloc byte[utf8Source.Length] :
(stackalloc byte[JsonConstants.StackallocThreshold]).Slice(0, utf8Source.Length) :
(unescapedArray = ArrayPool<byte>.Shared.Rent(utf8Source.Length));

Unescape(utf8Source, utf8Unescaped, idx, out int written);
Expand Down Expand Up @@ -45,7 +45,7 @@ public static string GetUnescapedString(ReadOnlySpan<byte> utf8Source, int idx)
byte[]? pooledName = null;

Span<byte> utf8Unescaped = length <= JsonConstants.StackallocThreshold ?
stackalloc byte[length] :
(stackalloc byte[JsonConstants.StackallocThreshold]).Slice(0, length) :
(pooledName = ArrayPool<byte>.Shared.Rent(length));

Unescape(utf8Source, utf8Unescaped, idx, out int written);
Expand All @@ -72,7 +72,7 @@ public static ReadOnlySpan<byte> GetUnescapedSpan(ReadOnlySpan<byte> utf8Source,
byte[]? pooledName = null;

Span<byte> utf8Unescaped = length <= JsonConstants.StackallocThreshold ?
stackalloc byte[length] :
(stackalloc byte[JsonConstants.StackallocThreshold]).Slice(0, length) :
(pooledName = ArrayPool<byte>.Shared.Rent(length));

Unescape(utf8Source, utf8Unescaped, idx, out int written);
Expand All @@ -97,7 +97,7 @@ public static bool UnescapeAndCompare(ReadOnlySpan<byte> utf8Source, ReadOnlySpa
byte[]? unescapedArray = null;

Span<byte> utf8Unescaped = utf8Source.Length <= JsonConstants.StackallocThreshold ?
stackalloc byte[utf8Source.Length] :
(stackalloc byte[JsonConstants.StackallocThreshold]).Slice(0, utf8Source.Length) :
(unescapedArray = ArrayPool<byte>.Shared.Rent(utf8Source.Length));

Unescape(utf8Source, utf8Unescaped, 0, out int written);
Expand Down Expand Up @@ -128,11 +128,11 @@ public static bool UnescapeAndCompare(ReadOnlySequence<byte> utf8Source, ReadOnl
int length = checked((int)utf8Source.Length);

Span<byte> utf8Unescaped = length <= JsonConstants.StackallocThreshold ?
stackalloc byte[length] :
(stackalloc byte[JsonConstants.StackallocThreshold]).Slice(0, length) :
(unescapedArray = ArrayPool<byte>.Shared.Rent(length));

Span<byte> utf8Escaped = length <= JsonConstants.StackallocThreshold ?
stackalloc byte[length] :
(stackalloc byte[JsonConstants.StackallocThreshold]).Slice(0, length) :
(escapedArray = ArrayPool<byte>.Shared.Rent(length));

utf8Source.CopyTo(utf8Escaped);
Expand Down Expand Up @@ -175,7 +175,7 @@ public static bool TryDecodeBase64(ReadOnlySpan<byte> utf8Unescaped, [NotNullWhe
byte[]? pooledArray = null;

Span<byte> byteSpan = utf8Unescaped.Length <= JsonConstants.StackallocThreshold ?
stackalloc byte[utf8Unescaped.Length] :
(stackalloc byte[JsonConstants.StackallocThreshold]).Slice(0, utf8Unescaped.Length) :
(pooledArray = ArrayPool<byte>.Shared.Rent(utf8Unescaped.Length));

OperationStatus status = Base64.DecodeFromUtf8(utf8Unescaped, byteSpan, out int bytesConsumed, out int bytesWritten);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ public static bool TryGetEscapedDateTime(ReadOnlySpan<byte> source, out DateTime
Debug.Assert(backslash != -1);

Debug.Assert(source.Length <= JsonConstants.MaximumEscapedDateTimeOffsetParseLength);
Span<byte> sourceUnescaped = stackalloc byte[source.Length];
Span<byte> sourceUnescaped = (stackalloc byte[JsonConstants.MaximumEscapedDateTimeOffsetParseLength]).Slice(0, source.Length);

Unescape(source, sourceUnescaped, backslash, out int written);
Debug.Assert(written > 0);
Expand All @@ -277,7 +277,7 @@ public static bool TryGetEscapedDateTimeOffset(ReadOnlySpan<byte> source, out Da
Debug.Assert(backslash != -1);

Debug.Assert(source.Length <= JsonConstants.MaximumEscapedDateTimeOffsetParseLength);
Span<byte> sourceUnescaped = stackalloc byte[source.Length];
Span<byte> sourceUnescaped = (stackalloc byte[JsonConstants.MaximumEscapedDateTimeOffsetParseLength]).Slice(0, source.Length);

Unescape(source, sourceUnescaped, backslash, out int written);
Debug.Assert(written > 0);
Expand All @@ -303,7 +303,7 @@ public static bool TryGetEscapedGuid(ReadOnlySpan<byte> source, out Guid value)
int idx = source.IndexOf(JsonConstants.BackSlash);
Debug.Assert(idx != -1);

Span<byte> utf8Unescaped = stackalloc byte[source.Length];
Span<byte> utf8Unescaped = (stackalloc byte[JsonConstants.MaximumEscapedGuidLength]).Slice(0, source.Length);

Unescape(source, utf8Unescaped, idx, out int written);
Debug.Assert(written > 0);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -540,9 +540,9 @@ private bool ConsumeLiteralMultiSegment(ReadOnlySpan<byte> literal, JsonTokenTyp

private bool CheckLiteralMultiSegment(ReadOnlySpan<byte> span, ReadOnlySpan<byte> literal, out int consumed)
{
Debug.Assert(span.Length > 0 && span[0] == literal[0]);
Debug.Assert(span.Length > 0 && span[0] == literal[0] && literal.Length <= JsonConstants.MaximumLiteralLength);

Span<byte> readSoFar = stackalloc byte[literal.Length];
Span<byte> readSoFar = (stackalloc byte[JsonConstants.MaximumLiteralLength]).Slice(0, literal.Length);
int written = 0;

long prevTotalConsumed = _totalConsumed;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public override TimeSpan Read(ref Utf8JsonReader reader, Type typeToConvert, Jso
int backslash = source.IndexOf(JsonConstants.BackSlash);
Debug.Assert(backslash != -1);

Span<byte> sourceUnescaped = stackalloc byte[source.Length];
Span<byte> sourceUnescaped = (stackalloc byte[MaximumEscapedTimeSpanFormatLength]).Slice(0, source.Length);

JsonReaderHelper.Unescape(source, sourceUnescaped, backslash, out int written);
Debug.Assert(written > 0);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ private void WriteBase64EscapeProperty(ReadOnlySpan<char> propertyName, ReadOnly
int length = JsonWriterHelper.GetMaxEscapedLength(propertyName.Length, firstEscapeIndexProp);

Span<char> escapedPropertyName = length <= JsonConstants.StackallocThreshold ?
stackalloc char[length] :
(stackalloc char[JsonConstants.StackallocThreshold]).Slice(0, length) :
(propertyArray = ArrayPool<char>.Shared.Rent(length));

JsonWriterHelper.EscapeString(propertyName, escapedPropertyName, firstEscapeIndexProp, _options.Encoder, out int written);
Expand All @@ -163,7 +163,7 @@ private void WriteBase64EscapeProperty(ReadOnlySpan<byte> utf8PropertyName, Read
int length = JsonWriterHelper.GetMaxEscapedLength(utf8PropertyName.Length, firstEscapeIndexProp);

Span<byte> escapedPropertyName = length <= JsonConstants.StackallocThreshold ?
stackalloc byte[length] :
(stackalloc byte[JsonConstants.StackallocThreshold]).Slice(0, length) :
(propertyArray = ArrayPool<byte>.Shared.Rent(length));

JsonWriterHelper.EscapeString(utf8PropertyName, escapedPropertyName, firstEscapeIndexProp, _options.Encoder, out int written);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ private void WriteStringEscapeProperty(ReadOnlySpan<char> propertyName, DateTime
int length = JsonWriterHelper.GetMaxEscapedLength(propertyName.Length, firstEscapeIndexProp);

Span<char> escapedPropertyName = length <= JsonConstants.StackallocThreshold ?
stackalloc char[length] :
(stackalloc char[JsonConstants.StackallocThreshold]).Slice(0, length) :
(propertyArray = ArrayPool<char>.Shared.Rent(length));

JsonWriterHelper.EscapeString(propertyName, escapedPropertyName, firstEscapeIndexProp, _options.Encoder, out int written);
Expand All @@ -168,7 +168,7 @@ private void WriteStringEscapeProperty(ReadOnlySpan<byte> utf8PropertyName, Date
int length = JsonWriterHelper.GetMaxEscapedLength(utf8PropertyName.Length, firstEscapeIndexProp);

Span<byte> escapedPropertyName = length <= JsonConstants.StackallocThreshold ?
stackalloc byte[length] :
(stackalloc byte[JsonConstants.StackallocThreshold]).Slice(0, length) :
(propertyArray = ArrayPool<byte>.Shared.Rent(length));

JsonWriterHelper.EscapeString(utf8PropertyName, escapedPropertyName, firstEscapeIndexProp, _options.Encoder, out int written);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ private void WriteStringEscapeProperty(ReadOnlySpan<char> propertyName, DateTime
int length = JsonWriterHelper.GetMaxEscapedLength(propertyName.Length, firstEscapeIndexProp);

Span<char> escapedPropertyName = length <= JsonConstants.StackallocThreshold ?
stackalloc char[length] :
(stackalloc char[JsonConstants.StackallocThreshold]).Slice(0, length) :
(propertyArray = ArrayPool<char>.Shared.Rent(length));

JsonWriterHelper.EscapeString(propertyName, escapedPropertyName, firstEscapeIndexProp, _options.Encoder, out int written);
Expand All @@ -167,7 +167,7 @@ private void WriteStringEscapeProperty(ReadOnlySpan<byte> utf8PropertyName, Date
int length = JsonWriterHelper.GetMaxEscapedLength(utf8PropertyName.Length, firstEscapeIndexProp);

Span<byte> escapedPropertyName = length <= JsonConstants.StackallocThreshold ?
stackalloc byte[length] :
(stackalloc byte[JsonConstants.StackallocThreshold]).Slice(0, length) :
(propertyArray = ArrayPool<byte>.Shared.Rent(length));

JsonWriterHelper.EscapeString(utf8PropertyName, escapedPropertyName, firstEscapeIndexProp, _options.Encoder, out int written);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ private void WriteNumberEscapeProperty(ReadOnlySpan<char> propertyName, decimal
int length = JsonWriterHelper.GetMaxEscapedLength(propertyName.Length, firstEscapeIndexProp);

Span<char> escapedPropertyName = length <= JsonConstants.StackallocThreshold ?
stackalloc char[length] :
(stackalloc char[JsonConstants.StackallocThreshold]).Slice(0, length) :
(propertyArray = ArrayPool<char>.Shared.Rent(length));

JsonWriterHelper.EscapeString(propertyName, escapedPropertyName, firstEscapeIndexProp, _options.Encoder, out int written);
Expand All @@ -167,7 +167,7 @@ private void WriteNumberEscapeProperty(ReadOnlySpan<byte> utf8PropertyName, deci
int length = JsonWriterHelper.GetMaxEscapedLength(utf8PropertyName.Length, firstEscapeIndexProp);

Span<byte> escapedPropertyName = length <= JsonConstants.StackallocThreshold ?
stackalloc byte[length] :
(stackalloc byte[JsonConstants.StackallocThreshold]).Slice(0, length) :
(propertyArray = ArrayPool<byte>.Shared.Rent(length));

JsonWriterHelper.EscapeString(utf8PropertyName, escapedPropertyName, firstEscapeIndexProp, _options.Encoder, out int written);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ private void WriteNumberEscapeProperty(ReadOnlySpan<char> propertyName, double v
int length = JsonWriterHelper.GetMaxEscapedLength(propertyName.Length, firstEscapeIndexProp);

Span<char> escapedPropertyName = length <= JsonConstants.StackallocThreshold ?
stackalloc char[length] :
(stackalloc char[JsonConstants.StackallocThreshold]).Slice(0, length) :
(propertyArray = ArrayPool<char>.Shared.Rent(length));

JsonWriterHelper.EscapeString(propertyName, escapedPropertyName, firstEscapeIndexProp, _options.Encoder, out int written);
Expand All @@ -171,7 +171,7 @@ private void WriteNumberEscapeProperty(ReadOnlySpan<byte> utf8PropertyName, doub
int length = JsonWriterHelper.GetMaxEscapedLength(utf8PropertyName.Length, firstEscapeIndexProp);

Span<byte> escapedPropertyName = length <= JsonConstants.StackallocThreshold ?
stackalloc byte[length] :
(stackalloc byte[JsonConstants.StackallocThreshold]).Slice(0, length) :
(propertyArray = ArrayPool<byte>.Shared.Rent(length));

JsonWriterHelper.EscapeString(utf8PropertyName, escapedPropertyName, firstEscapeIndexProp, _options.Encoder, out int written);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ private void WriteNumberEscapeProperty(ReadOnlySpan<char> propertyName, float va
int length = JsonWriterHelper.GetMaxEscapedLength(propertyName.Length, firstEscapeIndexProp);

Span<char> escapedPropertyName = length <= JsonConstants.StackallocThreshold ?
stackalloc char[length] :
(stackalloc char[JsonConstants.StackallocThreshold]).Slice(0, length) :
(propertyArray = ArrayPool<char>.Shared.Rent(length));

JsonWriterHelper.EscapeString(propertyName, escapedPropertyName, firstEscapeIndexProp, _options.Encoder, out int written);
Expand All @@ -171,7 +171,7 @@ private void WriteNumberEscapeProperty(ReadOnlySpan<byte> utf8PropertyName, floa
int length = JsonWriterHelper.GetMaxEscapedLength(utf8PropertyName.Length, firstEscapeIndexProp);

Span<byte> escapedPropertyName = length <= JsonConstants.StackallocThreshold ?
stackalloc byte[length] :
(stackalloc byte[JsonConstants.StackallocThreshold]).Slice(0, length) :
(propertyArray = ArrayPool<byte>.Shared.Rent(length));

JsonWriterHelper.EscapeString(utf8PropertyName, escapedPropertyName, firstEscapeIndexProp, _options.Encoder, out int written);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ private void WriteNumberEscapeProperty(ReadOnlySpan<char> propertyName, ReadOnly
int length = JsonWriterHelper.GetMaxEscapedLength(propertyName.Length, firstEscapeIndexProp);

Span<char> escapedPropertyName = length <= JsonConstants.StackallocThreshold ?
stackalloc char[length] :
(stackalloc char[JsonConstants.StackallocThreshold]).Slice(0, length) :
(propertyArray = ArrayPool<char>.Shared.Rent(length));

JsonWriterHelper.EscapeString(propertyName, escapedPropertyName, firstEscapeIndexProp, _options.Encoder, out int written);
Expand All @@ -144,7 +144,7 @@ private void WriteNumberEscapeProperty(ReadOnlySpan<byte> utf8PropertyName, Read
int length = JsonWriterHelper.GetMaxEscapedLength(utf8PropertyName.Length, firstEscapeIndexProp);

Span<byte> escapedPropertyName = length <= JsonConstants.StackallocThreshold ?
stackalloc byte[length] :
(stackalloc byte[JsonConstants.StackallocThreshold]).Slice(0, length) :
(propertyArray = ArrayPool<byte>.Shared.Rent(length));

JsonWriterHelper.EscapeString(utf8PropertyName, escapedPropertyName, firstEscapeIndexProp, _options.Encoder, out int written);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ private void WriteStringEscapeProperty(ReadOnlySpan<char> propertyName, Guid val
int length = JsonWriterHelper.GetMaxEscapedLength(propertyName.Length, firstEscapeIndexProp);

Span<char> escapedPropertyName = length <= JsonConstants.StackallocThreshold ?
stackalloc char[length] :
(stackalloc char[JsonConstants.StackallocThreshold]).Slice(0, length) :
(propertyArray = ArrayPool<char>.Shared.Rent(length));

JsonWriterHelper.EscapeString(propertyName, escapedPropertyName, firstEscapeIndexProp, _options.Encoder, out int written);
Expand All @@ -167,7 +167,7 @@ private void WriteStringEscapeProperty(ReadOnlySpan<byte> utf8PropertyName, Guid
int length = JsonWriterHelper.GetMaxEscapedLength(utf8PropertyName.Length, firstEscapeIndexProp);

Span<byte> escapedPropertyName = length <= JsonConstants.StackallocThreshold ?
stackalloc byte[length] :
(stackalloc byte[JsonConstants.StackallocThreshold]).Slice(0, length) :
(propertyArray = ArrayPool<byte>.Shared.Rent(length));

JsonWriterHelper.EscapeString(utf8PropertyName, escapedPropertyName, firstEscapeIndexProp, _options.Encoder, out int written);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ private void WriteLiteralEscapeProperty(ReadOnlySpan<char> propertyName, ReadOnl
int length = JsonWriterHelper.GetMaxEscapedLength(propertyName.Length, firstEscapeIndexProp);

Span<char> escapedPropertyName = length <= JsonConstants.StackallocThreshold ?
stackalloc char[length] :
(stackalloc char[JsonConstants.StackallocThreshold]).Slice(0, length) :
(propertyArray = ArrayPool<char>.Shared.Rent(length));

JsonWriterHelper.EscapeString(propertyName, escapedPropertyName, firstEscapeIndexProp, _options.Encoder, out int written);
Expand All @@ -283,7 +283,7 @@ private void WriteLiteralEscapeProperty(ReadOnlySpan<byte> utf8PropertyName, Rea
int length = JsonWriterHelper.GetMaxEscapedLength(utf8PropertyName.Length, firstEscapeIndexProp);

Span<byte> escapedPropertyName = length <= JsonConstants.StackallocThreshold ?
stackalloc byte[length] :
(stackalloc byte[JsonConstants.StackallocThreshold]).Slice(0, length) :
(propertyArray = ArrayPool<byte>.Shared.Rent(length));

JsonWriterHelper.EscapeString(utf8PropertyName, escapedPropertyName, firstEscapeIndexProp, _options.Encoder, out int written);
Expand Down
Loading

0 comments on commit 5c4d4fd

Please sign in to comment.