Skip to content

Commit

Permalink
Dedicated constant for the max number of chars permitted to be alloca…
Browse files Browse the repository at this point in the history
…ted on the stack.
  • Loading branch information
CodeBlanch authored and eiriktsarpalis committed Jul 16, 2021
1 parent 035b980 commit b1c746d
Show file tree
Hide file tree
Showing 25 changed files with 128 additions and 127 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ internal bool TryGetNamedPropertyValue(int index, ReadOnlySpan<char> propertyNam
int startIndex = index + DbRow.Size;
int endIndex = checked(row.NumberOfRows * DbRow.Size + index);

if (maxBytes < JsonConstants.StackallocThreshold)
if (maxBytes < JsonConstants.StackallocByteThreshold)
{
Span<byte> utf8Name = stackalloc byte[JsonConstants.StackallocThreshold];
Span<byte> utf8Name = stackalloc byte[JsonConstants.StackallocByteThreshold];
int len = JsonReaderHelper.GetUtf8FromText(propertyName, utf8Name);
utf8Name = utf8Name.Slice(0, len);

Expand Down Expand Up @@ -139,7 +139,7 @@ private bool TryGetNamedPropertyValue(
out JsonElement value)
{
ReadOnlySpan<byte> documentSpan = _utf8Json.Span;
Span<byte> utf8UnescapedStack = stackalloc byte[JsonConstants.StackallocThreshold];
Span<byte> utf8UnescapedStack = stackalloc byte[JsonConstants.StackallocByteThreshold];

// Move to the row before the EndObject
int index = endIndex - DbRow.Size;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -298,8 +298,8 @@ internal bool TextEquals(int index, ReadOnlySpan<char> otherText, bool isPropert
byte[]? otherUtf8TextArray = null;

int length = checked(otherText.Length * JsonConstants.MaxExpansionFactorWhileTranscoding);
Span<byte> otherUtf8Text = length <= JsonConstants.StackallocThreshold ?
stackalloc byte[JsonConstants.StackallocThreshold] :
Span<byte> otherUtf8Text = length <= JsonConstants.StackallocByteThreshold ?
stackalloc byte[JsonConstants.StackallocByteThreshold] :
(otherUtf8TextArray = ArrayPool<byte>.Shared.Rent(length));

ReadOnlySpan<byte> utf16Text = MemoryMarshal.AsBytes(otherText);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ internal static class JsonConstants
public const int MaxWriterDepth = 1_000;
public const int RemoveFlagsBitMask = 0x7FFFFFFF;

public const int StackallocThreshold = 256;
public const int StackallocByteThreshold = 256;
public const int StackallocCharThreshold = StackallocByteThreshold / 2;

// In the worst case, an ASCII character represented as a single utf-8 byte could expand 6x when escaped.
// For example: '+' becomes '\u0043'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ public static bool TryParseAsISO(ReadOnlySpan<char> source, out DateTime value)

int maxLength = checked(source.Length * JsonConstants.MaxExpansionFactorWhileTranscoding);

Span<byte> bytes = maxLength <= JsonConstants.StackallocThreshold
? stackalloc byte[JsonConstants.StackallocThreshold]
Span<byte> bytes = maxLength <= JsonConstants.StackallocByteThreshold
? stackalloc byte[JsonConstants.StackallocByteThreshold]
: new byte[maxLength];

int length = JsonReaderHelper.GetUtf8FromText(source, bytes);
Expand Down Expand Up @@ -86,8 +86,8 @@ public static bool TryParseAsISO(ReadOnlySpan<char> source, out DateTimeOffset v

int maxLength = checked(source.Length * JsonConstants.MaxExpansionFactorWhileTranscoding);

Span<byte> bytes = maxLength <= JsonConstants.StackallocThreshold
? stackalloc byte[JsonConstants.StackallocThreshold]
Span<byte> bytes = maxLength <= JsonConstants.StackallocByteThreshold
? stackalloc byte[JsonConstants.StackallocByteThreshold]
: new byte[maxLength];

int length = JsonReaderHelper.GetUtf8FromText(source, bytes);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ public static byte[] EscapeValue(

int length = JsonWriterHelper.GetMaxEscapedLength(utf8Value.Length, firstEscapeIndexVal);

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

JsonWriterHelper.EscapeString(utf8Value, escapedValue, firstEscapeIndexVal, encoder, out int written);
Expand All @@ -65,8 +65,8 @@ private static byte[] GetEscapedPropertyNameSection(

int length = JsonWriterHelper.GetMaxEscapedLength(utf8Value.Length, firstEscapeIndexVal);

Span<byte> escapedValue = length <= JsonConstants.StackallocThreshold ?
stackalloc byte[JsonConstants.StackallocThreshold] :
Span<byte> escapedValue = length <= JsonConstants.StackallocByteThreshold ?
stackalloc byte[JsonConstants.StackallocByteThreshold] :
(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 @@ -14,8 +14,8 @@ public static bool TryGetUnescapedBase64Bytes(ReadOnlySpan<byte> utf8Source, int
{
byte[]? unescapedArray = null;

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

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

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

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

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

Unescape(utf8Source, utf8Unescaped, idx, out int written);
Expand All @@ -96,8 +96,8 @@ public static bool UnescapeAndCompare(ReadOnlySpan<byte> utf8Source, ReadOnlySpa

byte[]? unescapedArray = null;

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

Unescape(utf8Source, utf8Unescaped, 0, out int written);
Expand Down Expand Up @@ -127,12 +127,12 @@ public static bool UnescapeAndCompare(ReadOnlySequence<byte> utf8Source, ReadOnl

int length = checked((int)utf8Source.Length);

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

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

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

Span<byte> byteSpan = utf8Unescaped.Length <= JsonConstants.StackallocThreshold ?
stackalloc byte[JsonConstants.StackallocThreshold] :
Span<byte> byteSpan = utf8Unescaped.Length <= JsonConstants.StackallocByteThreshold ?
stackalloc byte[JsonConstants.StackallocByteThreshold] :
(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 @@ -513,7 +513,7 @@ public bool ValueTextEquals(ReadOnlySpan<char> text)

int length = checked(text.Length * JsonConstants.MaxExpansionFactorWhileTranscoding);

if (length > JsonConstants.StackallocThreshold)
if (length > JsonConstants.StackallocByteThreshold)
{
otherUtf8TextArray = ArrayPool<byte>.Shared.Rent(length);
otherUtf8Text = otherUtf8TextArray;
Expand All @@ -523,8 +523,8 @@ public bool ValueTextEquals(ReadOnlySpan<char> text)
// Cannot create a span directly since it gets passed to instance methods on a ref struct.
unsafe
{
byte* ptr = stackalloc byte[JsonConstants.StackallocThreshold];
otherUtf8Text = new Span<byte>(ptr, JsonConstants.StackallocThreshold);
byte* ptr = stackalloc byte[JsonConstants.StackallocByteThreshold];
otherUtf8Text = new Span<byte>(ptr, JsonConstants.StackallocByteThreshold);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,8 +139,8 @@ private void WriteBase64EscapeProperty(ReadOnlySpan<char> propertyName, ReadOnly

int length = JsonWriterHelper.GetMaxEscapedLength(propertyName.Length, firstEscapeIndexProp);

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

JsonWriterHelper.EscapeString(propertyName, escapedPropertyName, firstEscapeIndexProp, _options.Encoder, out int written);
Expand All @@ -162,8 +162,8 @@ private void WriteBase64EscapeProperty(ReadOnlySpan<byte> utf8PropertyName, Read

int length = JsonWriterHelper.GetMaxEscapedLength(utf8PropertyName.Length, firstEscapeIndexProp);

Span<byte> escapedPropertyName = length <= JsonConstants.StackallocThreshold ?
stackalloc byte[JsonConstants.StackallocThreshold] :
Span<byte> escapedPropertyName = length <= JsonConstants.StackallocByteThreshold ?
stackalloc byte[JsonConstants.StackallocByteThreshold] :
(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,8 +144,8 @@ private void WriteStringEscapeProperty(ReadOnlySpan<char> propertyName, DateTime

int length = JsonWriterHelper.GetMaxEscapedLength(propertyName.Length, firstEscapeIndexProp);

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

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

int length = JsonWriterHelper.GetMaxEscapedLength(utf8PropertyName.Length, firstEscapeIndexProp);

Span<byte> escapedPropertyName = length <= JsonConstants.StackallocThreshold ?
stackalloc byte[JsonConstants.StackallocThreshold] :
Span<byte> escapedPropertyName = length <= JsonConstants.StackallocByteThreshold ?
stackalloc byte[JsonConstants.StackallocByteThreshold] :
(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 @@ -143,8 +143,8 @@ private void WriteStringEscapeProperty(ReadOnlySpan<char> propertyName, DateTime

int length = JsonWriterHelper.GetMaxEscapedLength(propertyName.Length, firstEscapeIndexProp);

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

JsonWriterHelper.EscapeString(propertyName, escapedPropertyName, firstEscapeIndexProp, _options.Encoder, out int written);
Expand All @@ -166,8 +166,8 @@ private void WriteStringEscapeProperty(ReadOnlySpan<byte> utf8PropertyName, Date

int length = JsonWriterHelper.GetMaxEscapedLength(utf8PropertyName.Length, firstEscapeIndexProp);

Span<byte> escapedPropertyName = length <= JsonConstants.StackallocThreshold ?
stackalloc byte[JsonConstants.StackallocThreshold] :
Span<byte> escapedPropertyName = length <= JsonConstants.StackallocByteThreshold ?
stackalloc byte[JsonConstants.StackallocByteThreshold] :
(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 @@ -143,8 +143,8 @@ private void WriteNumberEscapeProperty(ReadOnlySpan<char> propertyName, decimal

int length = JsonWriterHelper.GetMaxEscapedLength(propertyName.Length, firstEscapeIndexProp);

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

JsonWriterHelper.EscapeString(propertyName, escapedPropertyName, firstEscapeIndexProp, _options.Encoder, out int written);
Expand All @@ -166,8 +166,8 @@ private void WriteNumberEscapeProperty(ReadOnlySpan<byte> utf8PropertyName, deci

int length = JsonWriterHelper.GetMaxEscapedLength(utf8PropertyName.Length, firstEscapeIndexProp);

Span<byte> escapedPropertyName = length <= JsonConstants.StackallocThreshold ?
stackalloc byte[JsonConstants.StackallocThreshold] :
Span<byte> escapedPropertyName = length <= JsonConstants.StackallocByteThreshold ?
stackalloc byte[JsonConstants.StackallocByteThreshold] :
(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 @@ -147,8 +147,8 @@ private void WriteNumberEscapeProperty(ReadOnlySpan<char> propertyName, double v

int length = JsonWriterHelper.GetMaxEscapedLength(propertyName.Length, firstEscapeIndexProp);

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

JsonWriterHelper.EscapeString(propertyName, escapedPropertyName, firstEscapeIndexProp, _options.Encoder, out int written);
Expand All @@ -170,8 +170,8 @@ private void WriteNumberEscapeProperty(ReadOnlySpan<byte> utf8PropertyName, doub

int length = JsonWriterHelper.GetMaxEscapedLength(utf8PropertyName.Length, firstEscapeIndexProp);

Span<byte> escapedPropertyName = length <= JsonConstants.StackallocThreshold ?
stackalloc byte[JsonConstants.StackallocThreshold] :
Span<byte> escapedPropertyName = length <= JsonConstants.StackallocByteThreshold ?
stackalloc byte[JsonConstants.StackallocByteThreshold] :
(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 @@ -147,8 +147,8 @@ private void WriteNumberEscapeProperty(ReadOnlySpan<char> propertyName, float va

int length = JsonWriterHelper.GetMaxEscapedLength(propertyName.Length, firstEscapeIndexProp);

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

JsonWriterHelper.EscapeString(propertyName, escapedPropertyName, firstEscapeIndexProp, _options.Encoder, out int written);
Expand All @@ -170,8 +170,8 @@ private void WriteNumberEscapeProperty(ReadOnlySpan<byte> utf8PropertyName, floa

int length = JsonWriterHelper.GetMaxEscapedLength(utf8PropertyName.Length, firstEscapeIndexProp);

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

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

0 comments on commit b1c746d

Please sign in to comment.