Skip to content

Api proposal: Make SequenceReader.TryReadTo* accept an int for maxReadSize #28846

Closed

Description

@jkotalik

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

cc @JeremyKuhne @ahsonkhan @davidfowl @Tratcher

Activity

  1. JeremyKuhne commented on Mar 2, 2019

    @JeremyKuhne
    Member

    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. :)

  2. jkotalik commented on Mar 4, 2019

    @jkotalik
    ContributorAuthor

    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.

  3. JeremyKuhne commented on Apr 29, 2019

    @JeremyKuhne
    Member

    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 Span returns.

  4. transferred this issue fromdotnet/corefxon Feb 1, 2020
  5. added this to the 5.0 milestone on Feb 1, 2020
  6. removed their assignment
    on Mar 31, 2020
  7. 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.

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

  10. removed this from the Future milestone on Feb 11, 2025
  11. locked and limited conversation to collaborators on Mar 13, 2025
  12. 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-suggestionarea-System.Buffers

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions