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
{{ message }}
Repository navigation
API Proposal: SequenceReader<T>.TryPeek overloads to read a specified number of elements from any position #30779
The use case is to return a contiguous ReadOnlySpan<byte> or a ReadOnlySequence<byte> from the specified position.
publicpartialrefstructSequenceReader<T>{boolTryPeek(intskip,intcount,outReadOnlySpan<T>value);boolTryPeek(intskip,intcount,outReadOnlySequence<T>value);boolTryCopyTo(intskip,Span<T>destination);// And possibly (but would require flipping the skip/count on the other API):boolTryPeek(intcount,outReadOnlySpan<T>value);boolTryPeek(intcount,outReadOnlySequence<T>value);}
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).
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.
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:
publicpartialrefstructSequenceReader<T>{boolTryPeek(intskip,intcount,outReadOnlySpan<T>value);boolTryPeek(intskip,intcount,outReadOnlySequence<T>value);boolTryCopyTo(intskip,Span<T>destination);// And possibly (but would require flipping the skip/count on the other API):boolTryPeek(intcount,outReadOnlySpan<T>value);boolTryPeek(intcount,outReadOnlySequence<T>value);}
@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.
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.
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?
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?
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.
// 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);
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.
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.
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 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.
Activity
JeremyKuhne commentedon Sep 6, 2019
Perhaps
int skipwould be clearer? I had to think this through a couple of times when I first saw it.JeremyKuhne commentedon Sep 6, 2019
#30771 is the single element version of this.
JeremyKuhne commentedon Sep 6, 2019
I have mixed feelings about taking a
startofSequencePosition. My first inclination is to have it also takeint skip. Trying to position from the start of the sequence is potentially really expensive and it introduces another possible error state (theSequencePositionnot belonging to theSequence).JeremyKuhne commentedon Sep 6, 2019
Also note that we already have
TryCopyTowhich does effectively this. We should consider adding the matching API.JeremyKuhne commentedon Sep 6, 2019
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.
softworkz commentedon Sep 6, 2019
TryCopy does copying. The purpose of this API is to go without copying data (except when crossing segment boundaries).
JeremyKuhne commentedon Sep 6, 2019
Sure, I'm suggesting that we add the same "skip" concept to
TryCopyToas well. The proposal I'm happy with is:softworkz commentedon Sep 6, 2019
@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:
I think this would help getting a better understanding about the requirements when trying to parse content using the SequenceReader.
JeremyKuhne commentedon Sep 6, 2019
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.
softworkz commentedon Sep 6, 2019
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?
JeremyKuhne commentedon Sep 6, 2019
@ahsonkhan can you speak to @softworkz questions about parsing JSON with the SequenceReader? Perhaps point to an iteration where we still had the
SequenceReaderactively in use?davidfowl commentedon Sep 7, 2019
Updated the proposal.
ahsonkhan commentedon Sep 10, 2019
We should consider converging on the parameter names. One of: start/offset/skip and
lengthrather thancount, particularly since it applies to theLengthof the ROSequence or ROSpan being returned.JeremyKuhne commentedon Sep 10, 2019
Video
Approved: dotnet/corefx#40962 (comment):
softworkz commentedon Sep 10, 2019
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:
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.
33 remaining items
terrajobst commentedon Feb 20, 2020
Video
TryPeekAPIs 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.TryPeekoverloads involving sequences look fine, except thatcountchanges position due toskip. We should either remove the one that doesn't takeskip(callers would have to pass in0) or we should change order ofskipwhich might be weird.TryCopyToseems fine as proposed, but it seems we might want to change it based on what we do forTryPeek.@davidfowl looks like we should meet and discuss this.
dotnet-policy-service commentedon Jan 28, 2025
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 Feb 11, 2025
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.