Skip to content

API Proposal: SequenceReader<T>.AdvanceTo(SequencePosition) #30770

Closed

Description

@davidfowl

Today it isn't possible to Advance to a specific SequencePosition within the ReadOnlySequence<T>. The scenario is a scan using another method (like PositionOf) that returns a SequencePosition then using it to further parse the Reader. To achieve the same thing today you need to create a new SequenceReader<T> with the sliced ReadOnlySequence<T>.

public partial ref struct SequenceReader
{
    void AdvanceTo(SequencePosition position);
}

Usage:

void Parse(in ReadOnlySequence<byte>data, byte startByte)
{
    var reader = new SequenceReader<byte>(data);

    SequencePosition? position = data.PositionOf(startByte);
    if (position == null) return;

    reader.AdvanceTo(position);

    ParseReader(ref reader);
}

Activity

  1. ahsonkhan commented on Sep 9, 2019

    @ahsonkhan
    Contributor

    Currently, the caller would have to write the following if we don't add AdvanceTo(...):

    ReadOnlySequence<byte> sequence;
    SequenceReader<byte> reader;
    
    reader.Advance(sequence.Slice(0, position).Length);

    The concern with adding an API on SequenceReader that takes an arbitrary SequencePosition is that it may not belong to the ReadOnlySequence that was passed-in to the SequenceReader to begin with.

    Also, based on discussions with @JeremyKuhne, what happens if the position being passed in to AdvanceTo is before the current position. Then, we aren't really advancing at that point. Maybe the API should be something like SetPosition, although this operation isn't really cheap.

  2. davidfowl commented on Sep 10, 2019

    @davidfowl
    MemberAuthor

    The concern with adding an API on SequenceReader that takes an arbitrary SequencePosition is that it may not belong to the ReadOnlySequence that was passed-in to the SequenceReader to begin with.

    That ship has long sailed with any API that takes. SequencePosition. I’m not sure why it’s unique to this API.

    This may be a better API on the ReadOnlySequence itself

  3. JeremyKuhne commented on Sep 10, 2019

    @JeremyKuhne
    Member

    That ship has long sailed with any API that takes. SequencePosition. I’m not sure why it’s unique to this API.

    It isn't unique, just the first place we would be exposing it in the reader.

    This may be a better API on the ReadOnlySequence itself

    Is it any different than Slice?

    If we do add this API I think it should be SetPosition(). I'm a little hesitant to add it however, as there potentially is a "hidden" cost to getting the span out again. In the example given, for example, it is cheaper (hugely so if it crosses spans) to slice the incoming data before creating the reader.

  4. davidfowl commented on Sep 10, 2019

    @davidfowl
    MemberAuthor

    Is it any different than Slice?

    oops, I was thinking this was TryPeek.

    If we do add this API I think it should be SetPosition(). I'm a little hesitant to add it however, as there potentially is a "hidden" cost to getting the span out again. In the example given, for example, it is cheaper (hugely so if it crosses spans) to slice the incoming data before creating the reader.

    Does that mean we need to start adding more operations to the reader directly?

  5. JeremyKuhne commented on Sep 10, 2019

    @JeremyKuhne
    Member

    Does that mean we need to start adding more operations to the reader directly?

    I'm not sure I follow?

  6. davidfowl commented on Sep 10, 2019

    @davidfowl
    MemberAuthor

    Does that mean we need to start adding more operations to the reader directly?

    You could argue that PositionOf only needed to exist because we didn't have SequenceReader<T>.TryReadTo. I'm thinking about higher level operations that return SequencePosition then get used in the SequenceReader<T> to avoid scanning again.

  7. JeremyKuhne commented on Sep 10, 2019

    @JeremyKuhne
    Member

    I'm thinking about higher level operations that return SequencePosition

    What else are we missing?

  8. softworkz commented on Sep 10, 2019

    @softworkz

    I'm not sure if this API would even be possible to implement.. I mean, how for example would you compute the "Consumed" property?
    When you would reset everything internally, it would be just like creating a new SequenceReader, where really creating a new one would be the better choice as resetting an existing SequenceReader would introduce semantical inconsistency.

    Anyway, the motivation for adding this API suggestion was about being able to jump to a position found by the .PositionOf() method.
    I think, a much better solution would be to have an equivalent of .PositionOf() at the SequenceReader which returns an integer offset.

  9. JeremyKuhne commented on Sep 10, 2019

    @JeremyKuhne
    Member

    I mean, how for example would you compute the "Consumed" property?

    Consumed is always equal to the bytes from the Start to the current Position.

    resetting an existing SequenceReader would introduce semantical inconsistency

    Only if we changed the Sequence. I do agree, however, that it makes it harder to wrap your head around using the reader efficiently.

    I think, a much better solution would be to have an equivalent of .PositionOf() at the SequenceReader which returns an integer offset.

    We already have TryAdvanceTo(). The given example can be written as:

    void Parse(in ReadOnlySequence<byte>data, byte startByte)
    {
        var reader = new SequenceReader<byte>(data);
        if (!reader.TryAdvanceTo(startByte, advancePastDelimiter: false))
            return;
        ParseReader(ref reader);
    }

    I think the argument I can see with allowing setting position is that you may want to call other methods that don't take a reader and reposition with a position they return. It's a little [awkward to do it], and we can do it more efficiently. That said, I'm still hesitant to do it as it will potentially steer you away from writing more efficient code. The given example would be much faster if you wrote it like this, for example:

    void Parse(in ReadOnlySequence<byte>data, byte startByte)
    {
        SequencePosition? position = data.PositionOf(startByte);
        if (position == null) return;
        var reader = new SequenceReader<byte>(data.Slice(position));
        ParseReader(ref reader);
    }
  10. softworkz commented on Sep 10, 2019

    @softworkz

    I mean, how for example would you compute the "Consumed" property?

    Consumed is always equal to the bytes from the Start to the current Position.

    That means, that after using AdvanceTo(SequencePosition position), the Consumed property is set to 0?

    I think, a much better solution would be to have an equivalent of .PositionOf() at the SequenceReader which returns an integer offset.

    We already have TryAdvanceTo(). The given example can be written as:

    void Parse(in ReadOnlySequence<byte>data, byte startByte)
    {
        var reader = new SequenceReader<byte>(data);
        if (!reader.TryAdvanceTo(startByte, advancePastDelimiter: false))
            return;
        ParseReader(ref reader);
    }

    Yes, but this would require a rewind afterwards if you do not want to advance the reader.
    And before you are referring to the "ReadUntilDelimiter" API: It's not always that easy. You may for example need to identify the next positions of multiple values without advancing in order to make a decision about how to continue reading.
    That's why a non-advancing .PositionOf() method is required.

    I think the argument I can see with allowing setting position is that you may want to call other methods that don't take a reader and reposition with a position they return.

    This is already possible by using the .Position property of the SequenceReader.

    But as mentioned earlier: It should be avoided that people would end up having to use both ReadOnlySequence and SequenceReader APIs. That would just be too confusing.

    It's a little [awkward to do it], and we can do it more efficiently. That said, I'm still hesitant to do it as it will potentially steer you away from writing more efficient code. The given example would be much faster if you wrote it like this, for example:

    void Parse(in ReadOnlySequence<byte>data, byte startByte)
    {
        SequencePosition? position = data.PositionOf(startByte);
        if (position == null) return;
        var reader = new SequenceReader<byte>(data.Slice(position));
        ParseReader(ref reader);
    }

    That's once again a mixed use. Also I doubt its efficiency for general use. It might be efficient for the case that you had in mind while writing it, but instantiating a new SequenceReader has a cost as well.
    What is incorrect here is the assumption that the code flow will be exactly as shown in the Usage code snippet in the initial post. Actually you would rather do all this repeatedly with a single SequenceReader instance.

  11. JeremyKuhne commented on Sep 10, 2019

    @JeremyKuhne
    Member

    Video

    This was reviewed in dotnet/corefx#40962. Concerns about performance and usability pretty much as described so putting back to "needs work".

  12. transferred this issue fromdotnet/corefxon Feb 1, 2020
  13. added this to the 5.0 milestone on Feb 1, 2020
  14. benaadams commented on Apr 22, 2020

    @benaadams
    Member

    Had an issue with this api not existing; the work around is way more expensive as it has to create a new ReadOnlySequence<T>; then check its .Length then advance by that Length so its has to do the seek twice.

    var parsedLength = unconsumedSequence.Slice(reader.Position, consumed).Length;
    reader.Advance(parsedLength);

    You can get the .Position from SequenceReader but can't use a SequencePosition with SequenceReader; seems a bit of a gap?

    Obviously creating a new SequenceReader from a sliced ROS is way expensive; so you want to use Advance rather than doing that

  15. dotnet-policy-service commented on Jan 28, 2025

    @dotnet-policy-service
    Contributor

    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.

  16. added
    backlog-cleanup-candidate
    on Jan 28, 2025
  17. dotnet-policy-service commented on Feb 11, 2025

    @dotnet-policy-service
    Contributor

    This issue will now be closed since it had been marked no-recent-activity but 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.

  18. removed this from the Future milestone on Feb 11, 2025
  19. locked and limited conversation to collaborators on Mar 13, 2025
  20. removed
    backlog-cleanup-candidate
    on Mar 13, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    api-needs-workarea-System.Buffers

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions