Repository navigation
Closed
Labels
Description
Rationale
When using SequenceReader, it would nice to be able to limit calls to TryReadTo by passing an int for a maxReadSize. This would make all calls to TryReadTo* return preemptively if they read too much. These apis would be useful for parsing scenarios where we have a limit on the max length allowed for a value.
Without this API, I don't see a great way to limit a read size without slicing the ReadOnlySequence to a certain length before creating the SequenceReader
namespace System.Buffers
{
public ref struct SequenceReader<T> where T : unmanaged, IEquatable<T>
{
public bool TryAdvanceTo(T delimiter, bool advancePastDelimiter = true, int maxReadSize = -1);
public bool TryAdvanceToAny(ReadOnlySpan<T> delimiters, bool advancePastDelimiter = true, int maxReadSize = -1);
public bool TryReadTo(out ReadOnlySpan<T> span, T delimiter, bool advancePastDelimiter = true, int maxReadSize = -1);
public bool TryReadTo(out ReadOnlySpan<T> span, T delimiter, T delimiterEscape, bool advancePastDelimiter = true, int maxReadSize = -1);
public bool TryReadTo(out ReadOnlySpan<T> span, ReadOnlySpan<T> delimiter, bool advancePastDelimiter = true, int maxReadSize = -1);
public bool TryReadToAny(out ReadOnlySpan<T> span, ReadOnlySpan<T> delimiters, bool advancePastDelimiter = true, int maxReadSize = -1);
public bool TryReadTo(out ReadOnlySequence<T> sequence, T delimiter, bool advancePastDelimiter = true, int maxReadSize = -1);
public bool TryReadTo(out ReadOnlySequence<T> sequence, T delimiter, T delimiterEscape, bool advancePastDelimiter = true, int maxReadSize = -1);
public bool TryReadTo(out ReadOnlySequence<T> sequence, ReadOnlySpan<T> delimiter, bool advancePastDelimiter = true, int maxReadSize = -1);
public bool TryReadToAny(out ReadOnlySequence<T> sequence, ReadOnlySpan<T> delimiters, bool advancePastDelimiter = true, int maxReadSize = -1);
}
}The hard part of this type of API is knowing whether the TryReadTo call returned false due to not finding the delimiter or because it reached the maxReadSize. This can either be done through comparing Consumed before and after calling TryReadTo*.
Reacted by Fabien Barbier and aetos
Activity
JeremyKuhne commentedon Mar 2, 2019
Can you provide some "real world" examples? The big trick here is we spent months needling to make these APIs as fast as possible- additional checks do register in the perf traces (as well as any additional arguments being passed around). Not saying we can't, we just need to be really careful and measure everything. :)
jkotalik commentedon Mar 4, 2019
One example I ran into was reading application/x-www-form-urlencoded data where we have limits on how long the values can be: https://github.com/aspnet/AspNetCore/blob/master/src/Http/Http/src/Features/FormOptions.cs#L47-L53.
Totally understand the perf consideration; if this api is added, it could add another if check per byte examined in TryReadTo, which may or may not appear in perf traces.
JeremyKuhne commentedon Apr 29, 2019
I think this needs prototypes and comprehensive perf analysis to go forward. We should also look at possibilities of efficiently slicing off the reader / creating some sort of "end state".
My presumption is that the impact to the existing perf makes this impossible without adding new methods. I most concerned about the perf of the the
Spanreturns.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.