Skip to content

API Proposal: SequenceReader<T>.TryPeek overloads to read a specified number of elements from any position #30779

Closed

Description

@davidfowl

The use case is to return a contiguous ReadOnlySpan<byte> or a ReadOnlySequence<byte> from the specified position.

public partial ref struct SequenceReader<T>
{
    bool TryPeek(int skip, int count, out ReadOnlySpan<T> value);
    bool TryPeek(int skip, int count, out ReadOnlySequence<T> value);
    bool TryCopyTo(int skip, Span<T> destination);

    // And possibly (but would require flipping the skip/count on the other API):
    bool TryPeek(int count, out ReadOnlySpan<T> value);
    bool TryPeek(int count, out ReadOnlySequence<T> value);   
}

Activity

  1. JeremyKuhne commented on Sep 6, 2019

    @JeremyKuhne
    Member

    Perhaps int skip would be clearer? I had to think this through a couple of times when I first saw it.

  2. JeremyKuhne commented on Sep 6, 2019

    @JeremyKuhne
    Member

    #30771 is the single element version of this.

  3. JeremyKuhne commented on Sep 6, 2019

    @JeremyKuhne
    Member

    I have mixed feelings about taking a start of SequencePosition. My first inclination is to have it also take int skip. Trying to position from the start of the sequence is potentially really expensive and it introduces another possible error state (the SequencePosition not belonging to the Sequence).

  4. JeremyKuhne commented on Sep 6, 2019

    @JeremyKuhne
    Member

    Also note that we already have TryCopyTo which does effectively this. We should consider adding the matching API.

    public partial ref struct SequenceReader<T>
    {
        bool TryCopyTo(int skip, Span<T> destination);
    }
  5. JeremyKuhne commented on Sep 6, 2019

    @JeremyKuhne
    Member

    We can review this, but I really am hesitant to introduce complete random access to the reader. Skipping ahead a bit makes sense to me, but arbitrary positioning is expensive and confuses the prevailing model here.

  6. softworkz commented on Sep 6, 2019

    @softworkz

    Also note that we already have TryCopyTo which does effectively this. We should consider adding the matching API.

    public partial ref struct SequenceReader<T>
    {
        bool TryCopyTo(int skip, Span<T> destination);
    }

    TryCopy does copying. The purpose of this API is to go without copying data (except when crossing segment boundaries).

  7. JeremyKuhne commented on Sep 6, 2019

    @JeremyKuhne
    Member

    TryCopy does copying. The purpose of this API is to go without copying data (except when crossing segment boundaries).

    Sure, I'm suggesting that we add the same "skip" concept to TryCopyTo as well. The proposal I'm happy with is:

    public partial ref struct SequenceReader<T>
    {
        bool TryPeek(int skip, int count, out ReadOnlySpan<T> value);
        bool TryPeek(int skip, int count, out ReadOnlySequence<T> value);
        bool TryCopyTo(int skip, Span<T> destination);
    
        // And possibly (but would require flipping the skip/count on the other API):
        bool TryPeek(int count, out ReadOnlySpan<T> value);
        bool TryPeek(int count, out ReadOnlySequence<T> value);   
    }
  8. softworkz commented on Sep 6, 2019

    @softworkz

    @JeremyKuhne - I think we're coming from totally difference experiences in using this API.
    Would you mind reading my conversation with David here?

    I would make the same suggestion to you as I had written there:

    How about if you would try to create some kind of parsing example based on SequenceReader?

    I think some basic JSON or XML parser would be a good idea. It would take a char sequence instead of a byte sequence, but it would be easier to understand than some kind of binary format because everybody knows those formats.
    Of course I don't mean to create a complete and usable xml parser - just a simple example, parsing elements and their attributes as string values.

    I think this would help getting a better understanding about the requirements when trying to parse content using the SequenceReader.

  9. JeremyKuhne commented on Sep 6, 2019

    @JeremyKuhne
    Member

    How about if you would try to create some kind of parsing example based on SequenceReader?

    I actually wrote the reader with @ahsonkhan writing the JsonReader on top of it. We iterated for months to squeeze as much perf as we could out of the abstraction.

    The JsonReader didn't use it ultimately as we wanted to get the absolute fastest performance for it, and any abstraction will add some overhead. Just making a method call was enough to impact the sorts of perf goals we were trying to reach.

  10. softworkz commented on Sep 6, 2019

    @softworkz

    My project is completed but I would still be interested in that code how you managed to do that using SequenceReader. Was it a char or a byte sequence and how did parse values without copying? How did you determine the length of string values to parse?

  11. JeremyKuhne commented on Sep 6, 2019

    @JeremyKuhne
    Member

    I would still be interested in that code how you managed to do that using SequenceReader

    @ahsonkhan can you speak to @softworkz questions about parsing JSON with the SequenceReader? Perhaps point to an iteration where we still had the SequenceReader actively in use?

  12. davidfowl commented on Sep 7, 2019

    @davidfowl
    MemberAuthor

    Updated the proposal.

  13. ahsonkhan commented on Sep 10, 2019

    @ahsonkhan
    Contributor

    We should consider converging on the parameter names. One of: start/offset/skip and length rather than count, particularly since it applies to the Length of the ROSequence or ROSpan being returned.

  14. JeremyKuhne commented on Sep 10, 2019

    @JeremyKuhne
    Member

    Video

    Approved: dotnet/corefx#40962 (comment):

            // Equivalent "Peek" versions. They need a offset as peeking doesn't advance the reader and rewinding is super expensive.
            public readonly bool TryPeek(long offset, int length, out ReadOnlySpan<T> value);
            public readonly bool TryPeek(long offset, long length, out ReadOnlySequence<T> value);
     
            // Pairs with existing TryCopyTo(Span<T> destination), which does not advance the reader (neither does this)
            public readonly bool TryCopyTo(long offset, Span<T> destination);
  15. softworkz commented on Sep 10, 2019

    @softworkz

    I think it will be important to have proper documentation that informs about good and bad use of the TryPeek(Span) API and illustrates the allocation behavior.

    The probability of reallocation can be calculated as follows:

    P(alloc) = (length - 1) / segmentLength  |> for (length <= segmentLength), otherwise 100%
    
    Some examples for the case of the default segment size (4k):
    
    Peek length  |  Probability of allocation
    -----------------------------------------
    > 4k         |  100.000 %
    4k           |   99.999 %
    200          |    4.858 %
    50           |    1.196 %
    20           |    0.463 %
    4            |    0.073 %
    1            |    0.000 %
    

    Those figures show that the allocation penalty is pretty much acceptable for many use cases.

    It should only be warned about that this is not the right method for reading a larger range of data.

  16. 33 remaining items

  17. added this to the 5.0 milestone on Feb 1, 2020
  18. added
    api-needs-work
    and removed on Feb 20, 2020
  19. terrajobst commented on Feb 20, 2020

    @terrajobst
    Contributor

    Video

    • The two TryPeek APIs that out a span need more work because they silently allocate. Either allow for the caller to pass in a pre-allocated span or make it clear in the name that it will only return true if the data is in contiguous memory.
    • The TryPeek overloads involving sequences look fine, except that count changes position due to skip. We should either remove the one that doesn't take skip (callers would have to pass in 0) or we should change order of skip which might be weird.
    • TryCopyTo seems fine as proposed, but it seems we might want to change it based on what we do for TryPeek.

    @davidfowl looks like we should meet and discuss this.

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

  21. added
    backlog-cleanup-candidate
    on Jan 28, 2025
  22. 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.

  23. removed this from the Future milestone on Feb 11, 2025
  24. locked and limited conversation to collaborators on Mar 13, 2025
  25. 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