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

Proposal: IMessageReader<T> TryParseMessage overload with SequenceReader<byte> #69

Open
davidfowl opened this issue Jan 28, 2020 · 2 comments

Comments

@davidfowl
Copy link
Owner

Today IMessageReader<T> looks like this:

public interface IMessageReader<TMessage>
{
    bool TryParseMessage(in ReadOnlySequence<byte> input, ref SequencePosition consumed, ref SequencePosition examined, out TMessage message);
}

This works when the caller has a ReadOnlySequence<byte> (usually from a PipeReader) and is parsing messages at that level. Most of our IMessageReader<T> implementations end up creating a SequenceReader<byte> internally over the ReadOnlySequence<byte> to do further parser.

When trying to compose these parsers internally, it usually results is translating from ReadOnlySequence<byte> -> SequenceReader<byte> which is inefficient and inconvenient. Ideally you should be able to keep the SequenceReader<byte> rather than switching back to ReadOnlySequence<byte> and vice versa.

As an example, see https://github.com/YairHalberstadt/BedrockFramework/pull/1/files

Options:

  • Use SequenceReader<byte> instead of ReadOnlySequence<byte>
  • Add TryParseMessage overload for SequenceReader
public interface IMessageReader<TMessage>
{
    bool TryParseMessage(ref SequenceReader<byte> input, out TMessage message);
    bool TryParseMessage(in ReadOnlySequence<byte> input, ref SequencePosition consumed, ref SequencePosition examined, out TMessage message);
}

cc @YairHalberstadt

@YairHalberstadt
Copy link
Contributor

Option 1. Has the disadvantage of not being able to gain efficiency by removing the SequenceReader.
Option 2. Has the disadvantage of requiring you to duplicate code.

I suppose one option would be to add a DIM from the overload accepting a SequenceReader using the one accepting a ReadOnlySequence. When you want to use a SequenceReader internally, you reverse the direction, and implement the one accepting a ReadOnlySequence in terms of the one accepting a SequenceReader. When performance is extremely important, you can implement both seperately.

@mattnischan
Copy link
Contributor

mattnischan commented Jan 30, 2020

I don't know how possible it would be to add, if only for the message readers that are ReadOnlySequence<byte> pass-throughs. However, I have been noticing that there is some API friction in general with having to implement message readers that then return sequences, so perhaps that should be a construct all its own and not IMessageReader<ReadOnlySequence<byte>>.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants