Skip to content

Commit ead9f87

Browse files
committed
Clear only a dirty part of the buffer
1 parent ef7524d commit ead9f87

File tree

1 file changed

+15
-13
lines changed

1 file changed

+15
-13
lines changed

src/libraries/System.Text.Json/Common/JsonSeparatorNamingPolicy.cs

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -17,29 +17,30 @@ internal JsonSeparatorNamingPolicy(bool lowercase, char separator) =>
1717
public sealed override string ConvertName(string name)
1818
{
1919
// Rented buffer 20% longer that the input.
20-
int bufferLength = (12 * name.Length) / 10;
21-
char[]? buffer = bufferLength > JsonConstants.StackallocCharThreshold
22-
? ArrayPool<char>.Shared.Rent(bufferLength)
20+
int rentedBufferLength = (12 * name.Length) / 10;
21+
char[]? rentedBuffer = rentedBufferLength > JsonConstants.StackallocCharThreshold
22+
? ArrayPool<char>.Shared.Rent(rentedBufferLength)
2323
: null;
2424

2525
int resultLength = 0;
26-
Span<char> result = buffer is null
26+
Span<char> result = rentedBuffer is null
2727
? stackalloc char[JsonConstants.StackallocCharThreshold]
28-
: buffer;
28+
: rentedBuffer;
2929

3030
void ExpandBuffer(ref Span<char> result)
3131
{
32-
char[] bufferNew = ArrayPool<char>.Shared.Rent(result.Length * 2);
32+
char[] newBuffer = ArrayPool<char>.Shared.Rent(result.Length * 2);
3333

34-
result.CopyTo(bufferNew);
34+
result.CopyTo(newBuffer);
3535

36-
if (buffer is not null)
36+
if (rentedBuffer is not null)
3737
{
38-
ArrayPool<char>.Shared.Return(buffer, clearArray: true);
38+
result.Slice(0, resultLength).Clear();
39+
ArrayPool<char>.Shared.Return(rentedBuffer);
3940
}
4041

41-
buffer = bufferNew;
42-
result = buffer;
42+
rentedBuffer = newBuffer;
43+
result = rentedBuffer;
4344
}
4445

4546
void WriteWord(ReadOnlySpan<char> word, ref Span<char> result)
@@ -139,9 +140,10 @@ void WriteWord(ReadOnlySpan<char> word, ref Span<char> result)
139140

140141
name = result.Slice(0, resultLength).ToString();
141142

142-
if (buffer is not null)
143+
if (rentedBuffer is not null)
143144
{
144-
ArrayPool<char>.Shared.Return(buffer, clearArray: true);
145+
result.Slice(0, resultLength).Clear();
146+
ArrayPool<char>.Shared.Return(rentedBuffer);
145147
}
146148

147149
return name;

0 commit comments

Comments
 (0)