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

Override ReadAsync and WriteAsync methods on ConsoleStream. #71971

Merged
merged 3 commits into from
Jul 12, 2022

Conversation

eerhardt
Copy link
Member

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

public 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

Method Job Toolchain Mean Error StdDev Ratio RatioSD
WriteToStream Job-AMXSLA \main\corerun.exe 6.221 us 0.6849 us 0.7888 us 1.00 0.00
WriteToStream Job-CRTAYW \pr\corerun.exe 4.602 us 0.6634 us 0.7374 us 0.74 0.08
WriteNativeMemoryToStream Job-AMXSLA \main\corerun.exe 6.549 us 0.8117 us 0.8685 us 1.00 0.00
WriteNativeMemoryToStream Job-CRTAYW \pr\corerun.exe 4.575 us 0.6448 us 0.7426 us 0.69 0.07

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.
@ghost
Copy link

ghost commented Jul 11, 2022

Tagging subscribers to this area: @dotnet/area-system-console
See info in area-owners.md if you want to be subscribed.

Issue Details

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

public 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

Method Job Toolchain Mean Error StdDev Ratio RatioSD
WriteToStream Job-AMXSLA \main\corerun.exe 6.221 us 0.6849 us 0.7888 us 1.00 0.00
WriteToStream Job-CRTAYW \pr\corerun.exe 4.602 us 0.6634 us 0.7374 us 0.74 0.08
WriteNativeMemoryToStream Job-AMXSLA \main\corerun.exe 6.549 us 0.8117 us 0.8685 us 1.00 0.00
WriteNativeMemoryToStream Job-CRTAYW \pr\corerun.exe 4.575 us 0.6448 us 0.7426 us 0.69 0.07
Author: eerhardt
Assignees: -
Labels:

area-System.Console

Milestone: -

Copy link
Member

@stephentoub stephentoub left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks.

@eerhardt
Copy link
Member Author

The new "OpenStandardInput" tests need to be skipped on platforms that don't support standard input - like wasm, ios, and android. Fixing...

Copy link
Member

@adamsitnik adamsitnik left a 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 !

@adamsitnik
Copy link
Member

/azp list

@adamsitnik
Copy link
Member

/azp run runtime-extra-platforms

@azure-pipelines
Copy link

Azure Pipelines successfully started running 1 pipeline(s).

@eerhardt
Copy link
Member Author

All the runtime-extra-platforms failures are unrelated to System.Console.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants