Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

API Proposal: SequenceReader<T>.TryPeek overloads for look ahead #30771

Closed
davidfowl opened this issue Sep 5, 2019 · 10 comments · Fixed by dotnet/corefx#42364
Closed

API Proposal: SequenceReader<T>.TryPeek overloads for look ahead #30771

davidfowl opened this issue Sep 5, 2019 · 10 comments · Fixed by dotnet/corefx#42364
Labels
api-approved API was approved in API review, it can be implemented area-System.Buffers help wanted [up-for-grabs] Good issue for external contributors
Milestone

Comments

@davidfowl
Copy link
Member

Today it isn't easy to get the value at a specific position from the ReadOnlySequence<T> making it hard to write parsers that look ahead at data without moving the reader.

public partial ref struct SequenceReader<T>
{
    bool TryPeek(SequencePosition position, out T value);
    bool TryPeek(int count, out T value);
}

cc @softworkz

@JeremyKuhne
Copy link
Member

I think a better choice here would be:

public partial ref struct SequenceReader<T>
{
    bool TryPeek(int skip, out T value);
}

Picking an arbitrary start position makes the reader confusing- it has a position and is mostly forward-only. In addition the perf characteristics would be way out of line with the related methods.

@softworkz
Copy link

You mean that this method should actually advance the reader?

@JeremyKuhne
Copy link
Member

You mean that this method should actually advance the reader?

No, the peek methods shouldn't advance the reader at all. The idea here is that you can peek ahead for a value without having to get a set of values or rewinding the reader. Say, peek for a CR then peek again for a LF, rather than a CR/LF (as a simple example).

@softworkz
Copy link

Ah ok. So what are you implying by changing:

bool TryPeek(int count, out T value);

to

bool TryPeek(int skip, out T value);

Was it just about a better parameter naming then?

@JeremyKuhne
Copy link
Member

Was it just about a better parameter naming then?

Yep.

@softworkz
Copy link

I apologize - I misunderstood.

@ahsonkhan
Copy link
Member

Are we fine with having to incur the cost of slicing the sequence and fetching the span again (for the API that takes a SequencePosition)? The other existing APIs on SequenceReader do not have this overhead. This concern would apply to all the APIs that take an arbitrary SequencePosition.

Maybe, something like this:

public readonly bool TryPeek(SequencePosition position, out T value)
{
    ReadOnlySequence<T> sequence = Sequence.Slice(position);

    ReadOnlySpan<T> span = sequence.FirstSpan;

    if (!span.IsEmpty)
    {
        value = span[0];
        return true;
    }
    else
    {
        value = default;
        return false;
    }
}

@JeremyKuhne
Copy link
Member

JeremyKuhne commented Sep 10, 2019

Video

Approved: dotnet/corefx#40962 (comment):

        public readonly bool TryPeek(long offset, out T value);

@ahsonkhan
Copy link
Member

@JeremyKuhne - the parameter name isn't skip. It should be offset.

Also, don't forget the readonly keyword (+ the comment link needs to be updated).

@JeremyKuhne
Copy link
Member

the parameter name isn't skip. It should be offset.

Copy/paste fail. Fixing.

@msftgits msftgits transferred this issue from dotnet/corefx Feb 1, 2020
@msftgits msftgits added this to the 5.0 milestone Feb 1, 2020
@ghost ghost locked as resolved and limited conversation to collaborators Dec 12, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
api-approved API was approved in API review, it can be implemented area-System.Buffers help wanted [up-for-grabs] Good issue for external contributors
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants