You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This is a meta issue for #30779, #30778, #30771, #30770, and #29360 which add new APIs to SequenceReader<T>. This captures the overall change for the review. Detailed discussions are in the linked issues.
Proposed new surface area:
namespaceSystem.Buffers{publicrefstructSequenceReader<T>{// Optimized API to position the reader at the end of the sequence (much faster than what users can write)publicvoidAdvanceToEnd();// Pairs with existing Span<T> UnreadSpan;publicReadOnlySequence<T>UnreadSequence{get;}// Peeking out T, while skipping. This is more performant than users can write (avoids rewinding).publicboolTryPeek(intskip,outTvalue);// Overloads for TryRead that allow reading out a given count rather than to some delimiter (as with existing// API span out will slice if it can or allocate and copy if it has to).boolTryRead(intcount,outReadOnlySpan<T>value);boolTryRead(intcount,outReadOnlySequence<T>value);// Equivalent "Peek" versions. They need a skip as peeking doesn't advance the reader and rewinding is super expensive.publicboolTryPeek(intcount,outReadOnlySpan<T>value);publicboolTryPeek(intskip,intcount,outReadOnlySpan<T>value);publicboolTryPeek(intcount,outReadOnlySequence<T>value);publicboolTryPeek(intskip,intcount,outReadOnlySequence<T>value);// Pairs with existing TryCopyTo(Span<T> destination), which does not advance the reader (neither does this)publicboolTryCopyTo(intskip,Span<T>destination);// Also proposed, but having this can lead to writing slow code.publicboolSetPosition(SequencePositionposition);}}
IMO, the SequenceReader should not get any API added involving a SequencePosition parameter. Right now, it doesn't have any other - except the Position property.
I think there should be a clear separation between ReadOnlySequence and SequenceReader APIs, where ReadOnlySequence is dealing with SequencePosition objects and SequenceReader using integer offsets.
We don't use skip anywhere else and it kind of implies a side effect
offset implies relative to something and for a reader it makes to be relative to current position
We should the count parameter to length
We should remove the overloads of TryPeek that take two arguments and whose first argument is count -- because it conflicts TryPeek(int skip, out T value)
Arguments referring to position and lengths should be typed as long
We should add readonly annotations
It's sad that TryPeek() that returns a span might have to allocate if the span crosses buffers but there is no way around that.
FDG: we need to consider what we do for out parameters where the out type is the same as the input type as that allows you modify the this while it's running (ask Levi for details).
public void SetPosition(SequencePosition position); needs more work as there are a lot of concerns (like O(n) complexity).
namespaceSystem.Buffers{publicrefstructSequenceReader<T>{// Optimized API to position the reader at the end of the sequence (much faster than what users can write)publicvoidAdvanceToEnd();// Pairs with existing Span<T> UnreadSpan;publicreadonlyReadOnlySequence<T>UnreadSequence{get;}// Overloads for TryRead that allow reading out a given count rather than to some delimiter (as with existing// API span out will slice if it can or allocate and copy if it has to).publicboolTryRead(intlength,outReadOnlySpan<T>value);publicboolTryRead(longlength,outReadOnlySequence<T>value);// Peeking out T, while skipping. This is more performant than users can write (avoids rewinding).publicreadonlyboolTryPeek(longoffset,outTvalue);// Equivalent "Peek" versions. They need a offset as peeking doesn't advance the reader and rewinding is super expensive.publicreadonlyboolTryPeek(longoffset,intlength,outReadOnlySpan<T>value);publicreadonlyboolTryPeek(longoffset,longlength,outReadOnlySequence<T>value);// Pairs with existing TryCopyTo(Span<T> destination), which does not advance the reader (neither does this)publicreadonlyboolTryCopyTo(longoffset,Span<T>destination);}}
Reacted by softworkz, David Fowler and Fabien Barbier
Shouldn't the parameter names be span and sequence for methods with either overload? Existing methods that provide both that follow that pattern, and it is useful for writing code like reader.TryReadTo(span: out var span, '\n').
Also, the existing methods have the out parameter first & I think that these should stay congruent with those methods.
Shouldn't the parameter names be span and sequence for methods with either overload? Existing methods that provide both that follow that pattern, and it is useful for writing code like reader.TryReadTo(span: out var span, '\n').
Being consistent with existing APIs make sense to me (rename value to span, sequence).
Also, the existing methods have the out parameter first & I think that these should stay congruent with those methods.
Maybe. I can see changing the TryRead APIs with the length parameter to have the out paramter first. I am not sure about the TryPeek ones that have offset/length. But again, like you said, they would be more consistent.
I would be fine with both changes, but curious to hear what others have to say (@terrajobst, @JeremyKuhne, @bartonjs). The suggestion is:
public ref struct SequenceReader<T>
{
- public bool TryRead(int length, out ReadOnlySpan<T> value);+ public bool TryRead(out ReadOnlySpan<T> span, int length);- public bool TryRead(long length, out ReadOnlySequence<T> value);+ public bool TryRead(out ReadOnlySequence<T> sequence, long length);- public readonly bool TryPeek(long offset, out T value);+ public readonly bool TryPeek(out T value, long offset);- public readonly bool TryPeek(long offset, int length, out ReadOnlySpan<T> value);+ public readonly bool TryPeek(out ReadOnlySpan<T> span, long offset, int length);- public readonly bool TryPeek(long offset, long length, out ReadOnlySequence<T> value);+ public readonly bool TryPeek(out ReadOnlySequence<T> sequence, long offset, long length);
}
Here are the relevant existing APIs that shipped in 3.0 (for reference):
out values always go after all non-out values (except defaulted parameters)
There are some existing SequenceReader APIs that don't follow that though (out parameter is the first one for existing TryReadTo APIs). So, is it better to be consistent within the type here?
@ahsonkhan this meta issue has got approved label but seem that a lot of related issue are ready to review...can you confirm?I'd like to work on some of those but I don't know if make sense.
Activity
ahsonkhan commentedon Sep 10, 2019
cc'ing interested parties based on the issues listed:
@davidfowl, @GoldenCrystal, @stevejgordon, @softworkz, @FiniteReality,
softworkz commentedon Sep 10, 2019
IMO, the SequenceReader should not get any API added involving a SequencePosition parameter. Right now, it doesn't have any other - except the Position property.
I think there should be a clear separation between ReadOnlySequence and SequenceReader APIs, where ReadOnlySequence is dealing with SequencePosition objects and SequenceReader using integer offsets.
[-]Review System.Buffers proposals[/-][+]Review System.Buffers.SequenceReader<T> proposals[/+]terrajobst commentedon Sep 10, 2019
Video
skipparameters tooffsetskipanywhere else and it kind of implies a side effectoffsetimplies relative to something and for a reader it makes to be relative to current positioncountparameter tolengthcount-- because it conflictsTryPeek(int skip, out T value)longreadonlyannotationsTryPeek()that returns a span might have to allocate if the span crosses buffers but there is no way around that.thiswhile it's running (ask Levi for details).public void SetPosition(SequencePosition position);needs more work as there are a lot of concerns (like O(n) complexity).reflectronic commentedon Nov 4, 2019
Shouldn't the parameter names be
spanandsequencefor methods with either overload? Existing methods that provide both that follow that pattern, and it is useful for writing code likereader.TryReadTo(span: out var span, '\n').Also, the existing methods have the
outparameter first & I think that these should stay congruent with those methods.ahsonkhan commentedon Nov 13, 2019
Being consistent with existing APIs make sense to me (rename
valuetospan,sequence).Maybe. I can see changing the
TryReadAPIs with thelengthparameter to have theoutparamter first. I am not sure about theTryPeekones that haveoffset/length. But again, like you said, they would be more consistent.I would be fine with both changes, but curious to hear what others have to say (@terrajobst, @JeremyKuhne, @bartonjs). The suggestion is:
public ref struct SequenceReader<T> { - public bool TryRead(int length, out ReadOnlySpan<T> value); + public bool TryRead(out ReadOnlySpan<T> span, int length); - public bool TryRead(long length, out ReadOnlySequence<T> value); + public bool TryRead(out ReadOnlySequence<T> sequence, long length); - public readonly bool TryPeek(long offset, out T value); + public readonly bool TryPeek(out T value, long offset); - public readonly bool TryPeek(long offset, int length, out ReadOnlySpan<T> value); + public readonly bool TryPeek(out ReadOnlySpan<T> span, long offset, int length); - public readonly bool TryPeek(long offset, long length, out ReadOnlySequence<T> value); + public readonly bool TryPeek(out ReadOnlySequence<T> sequence, long offset, long length); }Here are the relevant existing APIs that shipped in 3.0 (for reference):
bartonjs commentedon Nov 13, 2019
outvalues always go after all non-outvalues (except defaulted parameters)ahsonkhan commentedon Nov 13, 2019
There are some existing
SequenceReaderAPIs that don't follow that though (outparameter is the first one for existingTryReadToAPIs). So, is it better to be consistent within the type here?MarcoRossignoli commentedon Dec 3, 2019
@ahsonkhan this meta issue has got approved label but seem that a lot of related issue are ready to review...can you confirm?I'd like to work on some of those but I don't know if make sense.
JeremyKuhne commentedon Dec 5, 2019
@MarcoRossignoli- we need to get the other issues resolved again, so we can't do those yet.
We need to revisit: dotnet/corefx#40780 and #30779.
ahsonkhan commentedon Dec 5, 2019
FYI, that should be #30778 (not dotnet/corefx#40780).
MarcoRossignoli commentedon Dec 6, 2019
Ok thanks!
JeremyKuhne commentedon Mar 25, 2020
Closing the meta issue and tracking via the linked issues.