runtime icon indicating copy to clipboard operation
runtime copied to clipboard

[API Proposal]: new constructor ReadOnlySequence(IEnumerable<ReadOnlyMemory<T>>)

Open lilinus opened this issue 3 years ago • 5 comments

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

lilinus avatar Nov 03 '23 12:11 lilinus

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:

api-suggestion, area-System.Memory

Milestone: -

ghost avatar Nov 03 '23 12:11 ghost

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>.

teo-tsirpanis avatar Nov 04 '23 14:11 teo-tsirpanis

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:

api-suggestion, area-System.Buffers, untriaged

Milestone: -

ghost avatar Nov 04 '23 14:11 ghost

@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.

lilinus avatar Nov 06 '23 08:11 lilinus

Closing, after digging into it a bit further looks like ReadOnlySequence is a bit more complicated than i thought.

lilinus avatar Nov 07 '23 10:11 lilinus