Skip to content

Commit

Permalink
Using stackalloc instead of new byte[x] on Streams (#88303)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>

* remove trailing whitespace

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

Co-authored-by: Stephen Toub <[email protected]>

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

Co-authored-by: Stephen Toub <[email protected]>

---------

Co-authored-by: Michał Petryka <[email protected]>
Co-authored-by: Stephen Toub <[email protected]>
  • Loading branch information
3 people authored Aug 11, 2023
1 parent 21f3bbe commit d87964d
Showing 1 changed file with 16 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ internal sealed class JsonEncodingStreamWrapper : Stream
{
private const int BufferLength = 128;

private readonly byte[] _byteBuffer = new byte[1];
private int _byteCount;
private int _byteOffset;
private byte[]? _bytes;
Expand Down Expand Up @@ -227,11 +226,13 @@ public override int ReadByte()
{
return _stream.ReadByte();
}
if (Read(_byteBuffer, 0, 1) == 0)

byte b = 0;
if (Read(new Span<byte>(ref b)) == 0)
{
return -1;
}
return _byteBuffer[0];
return b;
}

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

public override void Write(byte[] buffer, int offset, int count)
public override void Write(byte[] buffer, int offset, int count) =>
Write(new ReadOnlySpan<byte>(buffer, offset, count));

public override void Write(ReadOnlySpan<byte> buffer)
{
// Optimize UTF-8 case
if (_encodingCode == SupportedEncoding.UTF8)
{
_stream.Write(buffer, offset, count);
_stream.Write(buffer);
return;
}

Debug.Assert(_bytes != null);
Debug.Assert(_chars != null);
while (count > 0)

while (buffer.Length > 0)
{
int size = _chars.Length < count ? _chars.Length : count;
int charCount = _dec!.GetChars(buffer, offset, size, _chars, 0, false);
int size = Math.Min(_chars.Length, buffer.Length);
int charCount = _dec!.GetChars(buffer.Slice(0, size), _chars, false);
_byteCount = _enc!.GetBytes(_chars, 0, charCount, _bytes, 0, false);
_stream.Write(_bytes, 0, _byteCount);
offset += size;
count -= size;
buffer = buffer.Slice(size);
}
}

Expand All @@ -274,8 +278,8 @@ public override void WriteByte(byte b)
_stream.WriteByte(b);
return;
}
_byteBuffer[0] = b;
Write(_byteBuffer, 0, 1);

Write(new ReadOnlySpan<byte>(in b));
}

private static Encoding GetEncoding(SupportedEncoding e) =>
Expand Down

0 comments on commit d87964d

Please sign in to comment.