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]: Span<byte> constructor for MemoryStream #72723

Closed
RedIODev opened this issue Jul 23, 2022 · 5 comments
Closed

[API Proposal]: Span<byte> constructor for MemoryStream #72723

RedIODev opened this issue Jul 23, 2022 · 5 comments
Labels
api-suggestion Early API idea and discussion, it is NOT ready for implementation area-System.IO

Comments

@RedIODev
Copy link

Background and motivation

A memory stream should work with a span of bytes. When you have a span of bytes as your data and need a MemoryStream for another api you shouldn't need to copy the data from the span to a new byte array to get a "view" in form of a memory stream to it. This conversion from one type of memory view to another should be seamlessly possible.

API Proposal

namespace System.IO;

public class MemoryStream : Stream
{
    public MemoryStream(Span<byte> buffer) 
    {
        ...
    }
    ...
}

API Usage

Span<byte> someData = ...;
using MemoryStream ms = new MemoryStream(someData);
//The FromImage is just an example for an API that uses a stream and doesn't take a span. I don't propose a change to the creation of an Image from bytes.
Image imgFromData = Image.FromStream(ms);

Alternative Designs

Alternatively the byte array constructor of MemoryStream could be changed to a Span with likely braking no code since a byte array can implicitly be converted to a Span.

Risks

No response

@RedIODev RedIODev added the api-suggestion Early API idea and discussion, it is NOT ready for implementation label Jul 23, 2022
@ghost ghost added the untriaged New issue has not been triaged by the area owner label Jul 23, 2022
@ghost
Copy link

ghost commented Jul 23, 2022

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

Issue Details

Background and motivation

A memory stream should work with a span of bytes. When you have a span of bytes as your data and need a MemoryStream for another api you shouldn't need to copy the data from the span to a new byte array to get a "view" in form of a memory stream to it. This conversion from one type of memory view to another should be seamlessly possible.

API Proposal

namespace System.IO;

public class MemoryStream : Stream
{
    public MemoryStream(Span<byte> buffer) 
    {
        ...
    }
    ...
}

API Usage

Span<byte> someData = ...;
using MemoryStream ms = new MemoryStream(someData);
//The FromImage is just an example for an API that uses a stream and doesn't take a span. I don't propose a change to the creation of an Image from bytes.
Image imgFromData = Image.FromStream(ms);

Alternative Designs

Alternatively the byte array constructor of MemoryStream could be changed to a Span with likely braking no code since a byte array can implicitly be converted to a Span.

Risks

No response

Author: RedIODev
Assignees: -
Labels:

api-suggestion, area-System.Memory

Milestone: -

@EgorBo
Copy link
Member

EgorBo commented Jul 23, 2022

MemoryStream has to copy your data to its field on the heap so you can't avoid it.
In your case you can do new MemoryStream(someData.ToArray()) - there won't be a redundant copy we can avoid here.

@tfenise
Copy link
Contributor

tfenise commented Jul 23, 2022

If you really need a Stream backed by a Span<byte>, you may pin the Span<byte>, get a byte*, and create an UnmanagedMemoryStream. A more hacky solution is maybe to somehow get a pointer to the Span<byte> and use that pointer to access the Span<byte> in your own implementation of Stream. However, I don't recommend these solutions, as extra attention is needed to preserve memory safety.

@ghost
Copy link

ghost commented Jul 23, 2022

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

Issue Details

Background and motivation

A memory stream should work with a span of bytes. When you have a span of bytes as your data and need a MemoryStream for another api you shouldn't need to copy the data from the span to a new byte array to get a "view" in form of a memory stream to it. This conversion from one type of memory view to another should be seamlessly possible.

API Proposal

namespace System.IO;

public class MemoryStream : Stream
{
    public MemoryStream(Span<byte> buffer) 
    {
        ...
    }
    ...
}

API Usage

Span<byte> someData = ...;
using MemoryStream ms = new MemoryStream(someData);
//The FromImage is just an example for an API that uses a stream and doesn't take a span. I don't propose a change to the creation of an Image from bytes.
Image imgFromData = Image.FromStream(ms);

Alternative Designs

Alternatively the byte array constructor of MemoryStream could be changed to a Span with likely braking no code since a byte array can implicitly be converted to a Span.

Risks

No response

Author: RedIODev
Assignees: -
Labels:

api-suggestion, area-System.IO, untriaged

Milestone: -

@teo-tsirpanis
Copy link
Contributor

A MemoryStream constructor that takes a span and does not copy the data is simply impossible to have due to ref struct restrictions.

A stream that reads a Memory<byte> is possible and has been proposed in #22838.

@jkotas jkotas closed this as completed Jul 23, 2022
@ghost ghost removed the untriaged New issue has not been triaged by the area owner label Jul 23, 2022
@teo-tsirpanis teo-tsirpanis closed this as not planned Won't fix, can't repro, duplicate, stale Jul 23, 2022
@ghost ghost locked as resolved and limited conversation to collaborators Aug 23, 2022
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 area-System.IO
Projects
None yet
Development

No branches or pull requests

5 participants