-
Notifications
You must be signed in to change notification settings - Fork 4.7k
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
Override ReadAsync and WriteAsync methods on ConsoleStream. #71971
Conversation
The base Stream class implements these overloads by renting a buffer, creating a ReadWriteTask, and copying data as necessary. Instead, ConsoleStreams can just override these Async methods and synchronously call the underlying OS API. Add tests to verify that input and output console streams behave correctly.
Tagging subscribers to this area: @dotnet/area-system-console Issue DetailsThe base Stream class implements these overloads by renting a buffer, Instead, ConsoleStreams can just override these Async methods and synchronously Benchmarkpublic class ConsoleStreamsBenchmark
{
static byte[] _bytes = Encoding.ASCII.GetBytes("Hello, World!");
static Stream _stream = Console.OpenStandardOutput();
static NativeMemoryManager _nativeMemoryManager = CreateNativeMemory();
private unsafe static NativeMemoryManager CreateNativeMemory()
{
byte* mem = (byte*)NativeMemory.Alloc((nuint)_bytes.Length);
_bytes.AsSpan().CopyTo(new Span<byte>(mem, _bytes.Length));
return new NativeMemoryManager(mem, _bytes.Length);
}
[Benchmark]
public async Task WriteToStream()
{
await _stream.WriteAsync(_bytes);
}
[Benchmark]
public async Task WriteNativeMemoryToStream()
{
await _stream.WriteAsync(_nativeMemoryManager.Memory);
}
unsafe class NativeMemoryManager : MemoryManager<byte>
{
private byte* _pointer;
private int _length;
internal NativeMemoryManager(byte* pointer, int length)
{
_pointer = pointer;
_length = length;
}
public override Span<byte> GetSpan() => new Span<byte>(_pointer, _length);
public override MemoryHandle Pin(int elementIndex = 0) => default;
public override void Unpin() { }
protected override void Dispose(bool disposing) { }
}
} Results
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks.
The new "OpenStandardInput" tests need to be skipped on platforms that don't support standard input - like wasm, ios, and android. Fixing... |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM, thank you @eerhardt !
/azp list |
/azp run runtime-extra-platforms |
Azure Pipelines successfully started running 1 pipeline(s). |
All the |
The base Stream class implements these overloads by renting a buffer,
creating a ReadWriteTask, and copying data as necessary.
Instead, ConsoleStreams can just override these Async methods and synchronously
call the underlying OS API.
Benchmark
Results