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

API Proposal: Memory<byte> apis for MemoryStream #44140

Closed
huoyaoyuan opened this issue Nov 2, 2020 · 2 comments
Closed

API Proposal: Memory<byte> apis for MemoryStream #44140

huoyaoyuan opened this issue Nov 2, 2020 · 2 comments
Labels
api-suggestion Early API idea and discussion, it is NOT ready for implementation

Comments

@huoyaoyuan
Copy link
Member

Background and Motivation

Saw code in roslyn creating MemoryStream from underlying array of ImmutableArray.

https://github.com/dotnet/roslyn/blob/a6f654bb03596c2c3392bb16f082cc1615fac425/src/Compilers/CSharp/Test/Emit/Emit/CompilationEmitTests.cs#L921-L922

Proposed API

namespace System.IO
{
    public class MemoryStream
    {
+        public MemoryStream(Memory<byte> memory);
+        public MemoryStream(ReadOnlyMemory<byte> memory);
+        public MemoryStream(Memory<byte> memory, bool writable);
+        public MemoryStream(Memory<byte> memory, bool writable, bool publicVisible);

        public bool TryGetBuffer(out ArraySegment<byte> buffer);
        // Use a different name to not break `out var`
+       public bool TryGetMemoryBuffer(out Memory<byte> buffer);
    }
}

Usage Examples

new MemoryStream(image1.AsMemory())

Alternative Designs

A new class named ReadOnlyMemoryStream? The current MemoryStream is actually "array stream", but takes the name.

Risks

Unlike span, memory is slower than array AFAIK. Delegating array implementation to Memory may cause performance regression.
Memory and ReadOnlyMemory may require 2 versions of fields, or some casting hacks.

@huoyaoyuan huoyaoyuan added the api-suggestion Early API idea and discussion, it is NOT ready for implementation label Nov 2, 2020
@stephentoub
Copy link
Member

Dup of #22838

@huoyaoyuan
Copy link
Member Author

Sure, that issue covers the scenario. It's a bit old and needs work, please don't forget it.

@ghost ghost locked as resolved and limited conversation to collaborators Dec 6, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
api-suggestion Early API idea and discussion, it is NOT ready for implementation
Projects
None yet
Development

No branches or pull requests

2 participants