Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using stackalloc instead of new byte[x] on Streams #88303

Merged
merged 26 commits into from
Aug 11, 2023
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
449fdde
first XOR Linq implemetation
Poppyto Jul 1, 2023
980d208
replace classic allocation by stack allocation for ReadByte
Poppyto Jul 2, 2023
1846980
replace classic allocation by stack allocation for ReadByte & WriteByte
Poppyto Jul 2, 2023
d56f48a
replace classic allocation by stack allocation for ReadByte/CopyToAsync
Poppyto Jul 2, 2023
a5ca7c8
add missing ConfigureAwait(false) on ReadAsync
Poppyto Jul 2, 2023
c40cea4
replace classic allocation by stack allocation for ReadByte/WriteByte
Poppyto Jul 2, 2023
10d9882
Revert "first XOR Linq implemetation"
Poppyto Jul 2, 2023
f208335
indent fix
Poppyto Jul 2, 2023
6125271
rollback stackalloc on async methods
Poppyto Jul 2, 2023
b3291fc
missing char
Poppyto Jul 2, 2023
74d02fe
fix ConfigureAwait
Poppyto Jul 2, 2023
707fcf3
rollback ReadByte optim (degrade perfs)
Poppyto Jul 2, 2023
4f9d3b4
variablename fix
Poppyto Jul 2, 2023
af391ab
missing comment
Poppyto Jul 2, 2023
2af0c8e
rollback Stream allocations
Poppyto Jul 2, 2023
6cc3122
missing arguments
Poppyto Jul 2, 2023
ca86d8b
add Write(ReadOnlySpan<byte>) method
Poppyto Jul 4, 2023
ba3511e
better initialize Span
Poppyto Jul 4, 2023
cad94f5
add CRLF
Poppyto Jul 4, 2023
3a0e121
resolve comments
Poppyto Jul 5, 2023
00544fb
simplification call method Write
Poppyto Jul 5, 2023
6ebec71
Merge branch 'dotnet:main' into streamStackAlloc
Poppyto Jul 5, 2023
2ea5c7c
Update src/libraries/System.Private.DataContractSerialization/src/Sys…
Poppyto Jul 5, 2023
213ab21
remove trailing whitespace
Poppyto Jul 5, 2023
a520eef
Update src/libraries/System.Private.DataContractSerialization/src/Sys…
Poppyto Aug 9, 2023
ce38130
Update src/libraries/System.Private.DataContractSerialization/src/Sys…
Poppyto Aug 9, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -395,7 +395,7 @@ public override ValueTask<int> ReadAsync(Memory<byte> buffer, CancellationToken
}

Debug.Assert(_firstByteStatus == FirstByteStatus.Consumed);
return _stream.ReadAsync(buffer, cancellationToken);
return _stream.ReadAsync(buffer, cancellationToken).ConfigureAwait(false);
Poppyto marked this conversation as resolved.
Show resolved Hide resolved
}

public override async Task CopyToAsync(Stream destination, int bufferSize, CancellationToken cancellationToken)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -740,12 +740,10 @@ public override int ReadByte()
}

// Otherwise, fall back to reading a byte via Read, the same way Stream.ReadByte does.
// This allocation is unfortunate but should be relatively rare, as it'll only occur once
// per buffer fill internally by Read.
byte[] oneByte = new byte[1];
int bytesRead = Read(oneByte, 0, 1);
Span<byte> oneByteSpan = stackalloc byte[1];
Poppyto marked this conversation as resolved.
Show resolved Hide resolved
int bytesRead = Read(oneByteSpan);
Debug.Assert(bytesRead == 0 || bytesRead == 1);
return bytesRead == 1 ? oneByte[0] : -1;
return bytesRead == 1 ? oneByteSpan[0] : -1;
}

public override int Read(byte[] buffer, int offset, int count)
Expand Down
8 changes: 4 additions & 4 deletions src/libraries/System.Private.CoreLib/src/System/IO/Stream.cs
Original file line number Diff line number Diff line change
Expand Up @@ -809,9 +809,9 @@ public virtual int Read(Span<byte> buffer)

public virtual int ReadByte()
{
var oneByteArray = new byte[1];
int r = Read(oneByteArray, 0, 1);
return r == 0 ? -1 : oneByteArray[0];
Span<byte> oneByteSpan = stackalloc byte[1];
int r = Read(oneByteSpan);
Poppyto marked this conversation as resolved.
Show resolved Hide resolved
return r == 0 ? -1 : oneByteSpan[0];
}

/// <summary>
Expand Down Expand Up @@ -933,7 +933,7 @@ public virtual void Write(ReadOnlySpan<byte> buffer)
}
}

public virtual void WriteByte(byte value) => Write(new byte[1] { value }, 0, 1);
public virtual void WriteByte(byte value) => Write(stackalloc byte[1] { value });
Poppyto marked this conversation as resolved.
Show resolved Hide resolved

public static Stream Synchronized(Stream stream)
{
Expand Down
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,12 @@ public override int ReadByte()
{
return _stream.ReadByte();
}
if (Read(_byteBuffer, 0, 1) == 0)
Span<byte> oneByteSpan = stackalloc byte[1];
if (Read(oneByteSpan) == 0)
{
return -1;
}
return _byteBuffer[0];
return oneByteSpan[0];
Poppyto marked this conversation as resolved.
Show resolved Hide resolved
}

public override long Seek(long offset, SeekOrigin origin)
Expand Down Expand Up @@ -274,8 +274,7 @@ public override void WriteByte(byte b)
_stream.WriteByte(b);
return;
}
_byteBuffer[0] = b;
Write(_byteBuffer, 0, 1);
Write(stackalloc byte[1] {b});
Poppyto marked this conversation as resolved.
Show resolved Hide resolved
Poppyto marked this conversation as resolved.
Show resolved Hide resolved
}

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