[API Proposal]: new constructor ReadOnlySequence(IEnumerable<ReadOnlyMemory<T>>)
Background and motivation
There is no out of the box solution to create a ReadOnlySequence<T> from multiple ReadOnlyMemory<T>
API Proposal
namespace System.Buffers;
public readonly struct ReadOnlySequence<T>
{
public ReadOnlySequence(IEnumerable<ReadOnlyMemory<T>>);
}
API Usage
var buffers = new List<ReadOnlyMemory<string>>();
buffers.Add(new string[] { "foo", "bar" });
buffers.Add(new string[] { "baz", "qux" });
var roSequence = new ReadOnlySequence<string>(buffers);
Alternative Designs
No response
Risks
No response
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
There is no out of the box solution to create a ReadOnlySequence<T> from multiple ReadOnlyMemory<T>
API Proposal
namespace System.Buffers;
public readonly struct ReadOnlySequence<T>
{
public void Fancy(T item);
}
API Usage
var buffers = new List<ReadOnlyMemory<string>>();
buffers.Add(new string[] { "foo", "bar" });
buffers.Add(new string[] { "baz", "qux" });
var roSequence = new ReadOnlySequence<string>(buffers);
Alternative Designs
No response
Risks
No response
| Author: | lilinus |
|---|---|
| Assignees: | - |
| Labels: |
|
| Milestone: | - |
I don't think this API is a good idea. ReadOnlySequence<T> is defined as a linked list of buffers, and a plain IEnumerable<ReadOnlyMemory<T>> prevents efficiently and repeatedly traversing them. It could be implemented by enumerating the buffers once and building a linked list, but that would cause allocations, which is not something desirable for a high-performance type like ReadOnlySequence<T>.
Tagging subscribers to this area: @dotnet/area-system-buffers See info in area-owners.md if you want to be subscribed.
Issue Details
Background and motivation
There is no out of the box solution to create a ReadOnlySequence<T> from multiple ReadOnlyMemory<T>
API Proposal
namespace System.Buffers;
public readonly struct ReadOnlySequence<T>
{
public ReadOnlySequence(IEnumerable<ReadOnlyMemory<T>>);
}
API Usage
var buffers = new List<ReadOnlyMemory<string>>();
buffers.Add(new string[] { "foo", "bar" });
buffers.Add(new string[] { "baz", "qux" });
var roSequence = new ReadOnlySequence<string>(buffers);
Alternative Designs
No response
Risks
No response
| Author: | lilinus |
|---|---|
| Assignees: | - |
| Labels: |
|
| Milestone: | - |
@teo-tsirpanis, Yeah exactly I was meaning that a sequence would be created by enumerating the IEnumerable``, so basically a copy of it.
Maybe having it as a constructor can be misleading if it is interepreted as the reference to IEnumerable is kept?
Would it be better with a static method CreateFrom or similar?
Unless pooling is utilized, one need to allocate (a subclass of) ReadOnlySequenceSegments when creating a ReadOnlySequence.
Closing, after digging into it a bit further looks like ReadOnlySequence is a bit more complicated than i thought.