Skip to content

Commit d87964d

Browse files
PoppytoMichalPetrykastephentoub
authored
Using stackalloc instead of new byte[x] on Streams (#88303)
* first XOR Linq implemetation * replace classic allocation by stack allocation for ReadByte * replace classic allocation by stack allocation for ReadByte & WriteByte * replace classic allocation by stack allocation for ReadByte/CopyToAsync * add missing ConfigureAwait(false) on ReadAsync * replace classic allocation by stack allocation for ReadByte/WriteByte * Revert "first XOR Linq implemetation" This reverts commit 449fdde. * indent fix * rollback stackalloc on async methods * missing char * fix ConfigureAwait * rollback ReadByte optim (degrade perfs) * variablename fix * missing comment * rollback Stream allocations * missing arguments * add Write(ReadOnlySpan<byte>) method * better initialize Span * add CRLF * resolve comments * simplification call method Write * Update src/libraries/System.Private.DataContractSerialization/src/System/Runtime/Serialization/Json/JsonEncodingStreamWrapper.cs replace Span by ReadOnlySpan Co-authored-by: Michał Petryka <35800402+MichalPetryka@users.noreply.github.com> * remove trailing whitespace * Update src/libraries/System.Private.DataContractSerialization/src/System/Runtime/Serialization/Json/JsonEncodingStreamWrapper.cs Co-authored-by: Stephen Toub <stoub@microsoft.com> * Update src/libraries/System.Private.DataContractSerialization/src/System/Runtime/Serialization/Json/JsonEncodingStreamWrapper.cs Co-authored-by: Stephen Toub <stoub@microsoft.com> --------- Co-authored-by: Michał Petryka <35800402+MichalPetryka@users.noreply.github.com> Co-authored-by: Stephen Toub <stoub@microsoft.com>
1 parent 21f3bbe commit d87964d

File tree

1 file changed

+16
-12
lines changed

1 file changed

+16
-12
lines changed

src/libraries/System.Private.DataContractSerialization/src/System/Runtime/Serialization/Json/JsonEncodingStreamWrapper.cs

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ internal sealed class JsonEncodingStreamWrapper : Stream
1616
{
1717
private const int BufferLength = 128;
1818

19-
private readonly byte[] _byteBuffer = new byte[1];
2019
private int _byteCount;
2120
private int _byteOffset;
2221
private byte[]? _bytes;
@@ -227,11 +226,13 @@ public override int ReadByte()
227226
{
228227
return _stream.ReadByte();
229228
}
230-
if (Read(_byteBuffer, 0, 1) == 0)
229+
230+
byte b = 0;
231+
if (Read(new Span<byte>(ref b)) == 0)
231232
{
232233
return -1;
233234
}
234-
return _byteBuffer[0];
235+
return b;
235236
}
236237

237238
public override long Seek(long offset, SeekOrigin origin)
@@ -245,25 +246,28 @@ public override void SetLength(long value)
245246
throw new NotSupportedException();
246247
}
247248

248-
public override void Write(byte[] buffer, int offset, int count)
249+
public override void Write(byte[] buffer, int offset, int count) =>
250+
Write(new ReadOnlySpan<byte>(buffer, offset, count));
251+
252+
public override void Write(ReadOnlySpan<byte> buffer)
249253
{
250254
// Optimize UTF-8 case
251255
if (_encodingCode == SupportedEncoding.UTF8)
252256
{
253-
_stream.Write(buffer, offset, count);
257+
_stream.Write(buffer);
254258
return;
255259
}
256260

257261
Debug.Assert(_bytes != null);
258262
Debug.Assert(_chars != null);
259-
while (count > 0)
263+
264+
while (buffer.Length > 0)
260265
{
261-
int size = _chars.Length < count ? _chars.Length : count;
262-
int charCount = _dec!.GetChars(buffer, offset, size, _chars, 0, false);
266+
int size = Math.Min(_chars.Length, buffer.Length);
267+
int charCount = _dec!.GetChars(buffer.Slice(0, size), _chars, false);
263268
_byteCount = _enc!.GetBytes(_chars, 0, charCount, _bytes, 0, false);
264269
_stream.Write(_bytes, 0, _byteCount);
265-
offset += size;
266-
count -= size;
270+
buffer = buffer.Slice(size);
267271
}
268272
}
269273

@@ -274,8 +278,8 @@ public override void WriteByte(byte b)
274278
_stream.WriteByte(b);
275279
return;
276280
}
277-
_byteBuffer[0] = b;
278-
Write(_byteBuffer, 0, 1);
281+
282+
Write(new ReadOnlySpan<byte>(in b));
279283
}
280284

281285
private static Encoding GetEncoding(SupportedEncoding e) =>

0 commit comments

Comments
 (0)