Skip to content

Commit b1c746d

Browse files
CodeBlancheiriktsarpalis
authored andcommitted
Dedicated constant for the max number of chars permitted to be allocated on the stack.
1 parent 035b980 commit b1c746d

25 files changed

+128
-127
lines changed

src/libraries/System.Text.Json/src/System/Text/Json/Document/JsonDocument.TryGetProperty.cs

+3-3
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,9 @@ internal bool TryGetNamedPropertyValue(int index, ReadOnlySpan<char> propertyNam
2727
int startIndex = index + DbRow.Size;
2828
int endIndex = checked(row.NumberOfRows * DbRow.Size + index);
2929

30-
if (maxBytes < JsonConstants.StackallocThreshold)
30+
if (maxBytes < JsonConstants.StackallocByteThreshold)
3131
{
32-
Span<byte> utf8Name = stackalloc byte[JsonConstants.StackallocThreshold];
32+
Span<byte> utf8Name = stackalloc byte[JsonConstants.StackallocByteThreshold];
3333
int len = JsonReaderHelper.GetUtf8FromText(propertyName, utf8Name);
3434
utf8Name = utf8Name.Slice(0, len);
3535

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

144144
// Move to the row before the EndObject
145145
int index = endIndex - DbRow.Size;

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -298,8 +298,8 @@ internal bool TextEquals(int index, ReadOnlySpan<char> otherText, bool isPropert
298298
byte[]? otherUtf8TextArray = null;
299299

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

305305
ReadOnlySpan<byte> utf16Text = MemoryMarshal.AsBytes(otherText);

src/libraries/System.Text.Json/src/System/Text/Json/JsonConstants.cs

+2-1
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,8 @@ internal static class JsonConstants
5151
public const int MaxWriterDepth = 1_000;
5252
public const int RemoveFlagsBitMask = 0x7FFFFFFF;
5353

54-
public const int StackallocThreshold = 256;
54+
public const int StackallocByteThreshold = 256;
55+
public const int StackallocCharThreshold = StackallocByteThreshold / 2;
5556

5657
// In the worst case, an ASCII character represented as a single utf-8 byte could expand 6x when escaped.
5758
// For example: '+' becomes '\u0043'

src/libraries/System.Text.Json/src/System/Text/Json/JsonHelpers.Date.cs

+4-4
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,8 @@ public static bool TryParseAsISO(ReadOnlySpan<char> source, out DateTime value)
5151

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

54-
Span<byte> bytes = maxLength <= JsonConstants.StackallocThreshold
55-
? stackalloc byte[JsonConstants.StackallocThreshold]
54+
Span<byte> bytes = maxLength <= JsonConstants.StackallocByteThreshold
55+
? stackalloc byte[JsonConstants.StackallocByteThreshold]
5656
: new byte[maxLength];
5757

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

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

89-
Span<byte> bytes = maxLength <= JsonConstants.StackallocThreshold
90-
? stackalloc byte[JsonConstants.StackallocThreshold]
89+
Span<byte> bytes = maxLength <= JsonConstants.StackallocByteThreshold
90+
? stackalloc byte[JsonConstants.StackallocByteThreshold]
9191
: new byte[maxLength];
9292

9393
int length = JsonReaderHelper.GetUtf8FromText(source, bytes);

src/libraries/System.Text.Json/src/System/Text/Json/JsonHelpers.Escaping.cs

+4-4
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@ public static byte[] EscapeValue(
3737

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

40-
Span<byte> escapedValue = length <= JsonConstants.StackallocThreshold ?
41-
stackalloc byte[JsonConstants.StackallocThreshold] :
40+
Span<byte> escapedValue = length <= JsonConstants.StackallocByteThreshold ?
41+
stackalloc byte[JsonConstants.StackallocByteThreshold] :
4242
(valueArray = ArrayPool<byte>.Shared.Rent(length));
4343

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

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

68-
Span<byte> escapedValue = length <= JsonConstants.StackallocThreshold ?
69-
stackalloc byte[JsonConstants.StackallocThreshold] :
68+
Span<byte> escapedValue = length <= JsonConstants.StackallocByteThreshold ?
69+
stackalloc byte[JsonConstants.StackallocByteThreshold] :
7070
(valueArray = ArrayPool<byte>.Shared.Rent(length));
7171

7272
JsonWriterHelper.EscapeString(utf8Value, escapedValue, firstEscapeIndexVal, encoder, out int written);

src/libraries/System.Text.Json/src/System/Text/Json/Reader/JsonReaderHelper.Unescaping.cs

+14-14
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ public static bool TryGetUnescapedBase64Bytes(ReadOnlySpan<byte> utf8Source, int
1414
{
1515
byte[]? unescapedArray = null;
1616

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

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

47-
Span<byte> utf8Unescaped = length <= JsonConstants.StackallocThreshold ?
48-
stackalloc byte[JsonConstants.StackallocThreshold] :
47+
Span<byte> utf8Unescaped = length <= JsonConstants.StackallocByteThreshold ?
48+
stackalloc byte[JsonConstants.StackallocByteThreshold] :
4949
(pooledName = ArrayPool<byte>.Shared.Rent(length));
5050

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

74-
Span<byte> utf8Unescaped = length <= JsonConstants.StackallocThreshold ?
75-
stackalloc byte[JsonConstants.StackallocThreshold] :
74+
Span<byte> utf8Unescaped = length <= JsonConstants.StackallocByteThreshold ?
75+
stackalloc byte[JsonConstants.StackallocByteThreshold] :
7676
(pooledName = ArrayPool<byte>.Shared.Rent(length));
7777

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

9797
byte[]? unescapedArray = null;
9898

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

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

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

130-
Span<byte> utf8Unescaped = length <= JsonConstants.StackallocThreshold ?
131-
stackalloc byte[JsonConstants.StackallocThreshold] :
130+
Span<byte> utf8Unescaped = length <= JsonConstants.StackallocByteThreshold ?
131+
stackalloc byte[JsonConstants.StackallocByteThreshold] :
132132
(unescapedArray = ArrayPool<byte>.Shared.Rent(length));
133133

134-
Span<byte> utf8Escaped = length <= JsonConstants.StackallocThreshold ?
135-
stackalloc byte[JsonConstants.StackallocThreshold] :
134+
Span<byte> utf8Escaped = length <= JsonConstants.StackallocByteThreshold ?
135+
stackalloc byte[JsonConstants.StackallocByteThreshold] :
136136
(escapedArray = ArrayPool<byte>.Shared.Rent(length));
137137

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

177-
Span<byte> byteSpan = utf8Unescaped.Length <= JsonConstants.StackallocThreshold ?
178-
stackalloc byte[JsonConstants.StackallocThreshold] :
177+
Span<byte> byteSpan = utf8Unescaped.Length <= JsonConstants.StackallocByteThreshold ?
178+
stackalloc byte[JsonConstants.StackallocByteThreshold] :
179179
(pooledArray = ArrayPool<byte>.Shared.Rent(utf8Unescaped.Length));
180180

181181
OperationStatus status = Base64.DecodeFromUtf8(utf8Unescaped, byteSpan, out int bytesConsumed, out int bytesWritten);

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

+3-3
Original file line numberDiff line numberDiff line change
@@ -513,7 +513,7 @@ public bool ValueTextEquals(ReadOnlySpan<char> text)
513513

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

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

src/libraries/System.Text.Json/src/System/Text/Json/Writer/Utf8JsonWriter.WriteProperties.Bytes.cs

+4-4
Original file line numberDiff line numberDiff line change
@@ -139,8 +139,8 @@ private void WriteBase64EscapeProperty(ReadOnlySpan<char> propertyName, ReadOnly
139139

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

142-
Span<char> escapedPropertyName = length <= JsonConstants.StackallocThreshold ?
143-
stackalloc char[JsonConstants.StackallocThreshold] :
142+
Span<char> escapedPropertyName = length <= JsonConstants.StackallocCharThreshold ?
143+
stackalloc char[JsonConstants.StackallocCharThreshold] :
144144
(propertyArray = ArrayPool<char>.Shared.Rent(length));
145145

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

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

165-
Span<byte> escapedPropertyName = length <= JsonConstants.StackallocThreshold ?
166-
stackalloc byte[JsonConstants.StackallocThreshold] :
165+
Span<byte> escapedPropertyName = length <= JsonConstants.StackallocByteThreshold ?
166+
stackalloc byte[JsonConstants.StackallocByteThreshold] :
167167
(propertyArray = ArrayPool<byte>.Shared.Rent(length));
168168

169169
JsonWriterHelper.EscapeString(utf8PropertyName, escapedPropertyName, firstEscapeIndexProp, _options.Encoder, out int written);

src/libraries/System.Text.Json/src/System/Text/Json/Writer/Utf8JsonWriter.WriteProperties.DateTime.cs

+4-4
Original file line numberDiff line numberDiff line change
@@ -144,8 +144,8 @@ private void WriteStringEscapeProperty(ReadOnlySpan<char> propertyName, DateTime
144144

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

147-
Span<char> escapedPropertyName = length <= JsonConstants.StackallocThreshold ?
148-
stackalloc char[JsonConstants.StackallocThreshold] :
147+
Span<char> escapedPropertyName = length <= JsonConstants.StackallocCharThreshold ?
148+
stackalloc char[JsonConstants.StackallocCharThreshold] :
149149
(propertyArray = ArrayPool<char>.Shared.Rent(length));
150150

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

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

170-
Span<byte> escapedPropertyName = length <= JsonConstants.StackallocThreshold ?
171-
stackalloc byte[JsonConstants.StackallocThreshold] :
170+
Span<byte> escapedPropertyName = length <= JsonConstants.StackallocByteThreshold ?
171+
stackalloc byte[JsonConstants.StackallocByteThreshold] :
172172
(propertyArray = ArrayPool<byte>.Shared.Rent(length));
173173

174174
JsonWriterHelper.EscapeString(utf8PropertyName, escapedPropertyName, firstEscapeIndexProp, _options.Encoder, out int written);

src/libraries/System.Text.Json/src/System/Text/Json/Writer/Utf8JsonWriter.WriteProperties.DateTimeOffset.cs

+4-4
Original file line numberDiff line numberDiff line change
@@ -143,8 +143,8 @@ private void WriteStringEscapeProperty(ReadOnlySpan<char> propertyName, DateTime
143143

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

146-
Span<char> escapedPropertyName = length <= JsonConstants.StackallocThreshold ?
147-
stackalloc char[JsonConstants.StackallocThreshold] :
146+
Span<char> escapedPropertyName = length <= JsonConstants.StackallocCharThreshold ?
147+
stackalloc char[JsonConstants.StackallocCharThreshold] :
148148
(propertyArray = ArrayPool<char>.Shared.Rent(length));
149149

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

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

169-
Span<byte> escapedPropertyName = length <= JsonConstants.StackallocThreshold ?
170-
stackalloc byte[JsonConstants.StackallocThreshold] :
169+
Span<byte> escapedPropertyName = length <= JsonConstants.StackallocByteThreshold ?
170+
stackalloc byte[JsonConstants.StackallocByteThreshold] :
171171
(propertyArray = ArrayPool<byte>.Shared.Rent(length));
172172

173173
JsonWriterHelper.EscapeString(utf8PropertyName, escapedPropertyName, firstEscapeIndexProp, _options.Encoder, out int written);

src/libraries/System.Text.Json/src/System/Text/Json/Writer/Utf8JsonWriter.WriteProperties.Decimal.cs

+4-4
Original file line numberDiff line numberDiff line change
@@ -143,8 +143,8 @@ private void WriteNumberEscapeProperty(ReadOnlySpan<char> propertyName, decimal
143143

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

146-
Span<char> escapedPropertyName = length <= JsonConstants.StackallocThreshold ?
147-
stackalloc char[JsonConstants.StackallocThreshold] :
146+
Span<char> escapedPropertyName = length <= JsonConstants.StackallocCharThreshold ?
147+
stackalloc char[JsonConstants.StackallocCharThreshold] :
148148
(propertyArray = ArrayPool<char>.Shared.Rent(length));
149149

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

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

169-
Span<byte> escapedPropertyName = length <= JsonConstants.StackallocThreshold ?
170-
stackalloc byte[JsonConstants.StackallocThreshold] :
169+
Span<byte> escapedPropertyName = length <= JsonConstants.StackallocByteThreshold ?
170+
stackalloc byte[JsonConstants.StackallocByteThreshold] :
171171
(propertyArray = ArrayPool<byte>.Shared.Rent(length));
172172

173173
JsonWriterHelper.EscapeString(utf8PropertyName, escapedPropertyName, firstEscapeIndexProp, _options.Encoder, out int written);

src/libraries/System.Text.Json/src/System/Text/Json/Writer/Utf8JsonWriter.WriteProperties.Double.cs

+4-4
Original file line numberDiff line numberDiff line change
@@ -147,8 +147,8 @@ private void WriteNumberEscapeProperty(ReadOnlySpan<char> propertyName, double v
147147

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

150-
Span<char> escapedPropertyName = length <= JsonConstants.StackallocThreshold ?
151-
stackalloc char[JsonConstants.StackallocThreshold] :
150+
Span<char> escapedPropertyName = length <= JsonConstants.StackallocCharThreshold ?
151+
stackalloc char[JsonConstants.StackallocCharThreshold] :
152152
(propertyArray = ArrayPool<char>.Shared.Rent(length));
153153

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

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

173-
Span<byte> escapedPropertyName = length <= JsonConstants.StackallocThreshold ?
174-
stackalloc byte[JsonConstants.StackallocThreshold] :
173+
Span<byte> escapedPropertyName = length <= JsonConstants.StackallocByteThreshold ?
174+
stackalloc byte[JsonConstants.StackallocByteThreshold] :
175175
(propertyArray = ArrayPool<byte>.Shared.Rent(length));
176176

177177
JsonWriterHelper.EscapeString(utf8PropertyName, escapedPropertyName, firstEscapeIndexProp, _options.Encoder, out int written);

src/libraries/System.Text.Json/src/System/Text/Json/Writer/Utf8JsonWriter.WriteProperties.Float.cs

+4-4
Original file line numberDiff line numberDiff line change
@@ -147,8 +147,8 @@ private void WriteNumberEscapeProperty(ReadOnlySpan<char> propertyName, float va
147147

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

150-
Span<char> escapedPropertyName = length <= JsonConstants.StackallocThreshold ?
151-
stackalloc char[JsonConstants.StackallocThreshold] :
150+
Span<char> escapedPropertyName = length <= JsonConstants.StackallocCharThreshold ?
151+
stackalloc char[JsonConstants.StackallocCharThreshold] :
152152
(propertyArray = ArrayPool<char>.Shared.Rent(length));
153153

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

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

173-
Span<byte> escapedPropertyName = length <= JsonConstants.StackallocThreshold ?
174-
stackalloc byte[JsonConstants.StackallocThreshold] :
173+
Span<byte> escapedPropertyName = length <= JsonConstants.StackallocByteThreshold ?
174+
stackalloc byte[JsonConstants.StackallocByteThreshold] :
175175
(propertyArray = ArrayPool<byte>.Shared.Rent(length));
176176

177177
JsonWriterHelper.EscapeString(utf8PropertyName, escapedPropertyName, firstEscapeIndexProp, _options.Encoder, out int written);

0 commit comments

Comments
 (0)