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

C#: Allow message parsing from an array slice #3858

Merged
merged 3 commits into from Nov 10, 2017

Conversation

jtattermusch
Copy link
Contributor

@jtattermusch jtattermusch commented Nov 9, 2017

In gRPC C# we would like to enable more efficient buffer management and it seems as a prerequisite
to be able to parse protobuf messages from array slices (when pooling, we cannot ensure that the size of the array will be the same as the length of the payload, and for some techniques, we can't even rely on the offset being 0).
Two reasons why I'd like to add the methods to protobuf directly:

  • input.CheckReadEndOfStreamTag() is internal which makes it harder to check parsing went alright in external code.
  • the code to serialize/deserialize protobuf is part of the generated gRPC stubs, so keeping this code concise would help (and we can't have helpers in Grpc.Core as that one in protobuf agnostic).

Context: grpc/grpc#13236.

@jtattermusch jtattermusch added the c# label Nov 9, 2017
@jtattermusch jtattermusch changed the title Allow message parsing from an array slice C#: Allow message parsing from an array slice Nov 9, 2017
/// <param name="length">The length of the slice to merge.</param>
public static void MergeFrom(this IMessage message, byte[] data, int offset, int length)
{
ProtoPreconditions.CheckNotNull(message, "message");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

At some point I should do a nameof tidy-up... but I understand the point of sticking to the convention of the rest of the file here.

/// <returns>The newly parsed message.</returns>
public IMessage ParseFrom(byte[] data, int offset, int length)
{
ProtoPreconditions.CheckNotNull(data, "data");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd ditch this given that it'll be validated in the call to message.MergeFrom.

(If you change the parameter names to buffer in both cases, you could rely on the CodedInputStream constructor validation, but I prefer data instead of buffer here...)

@jtattermusch
Copy link
Contributor Author

A few more corrections:

  • remove redundant CheckNotNull check where already checked by MergeFrom
  • add a generic version of ParseFrom as well.

PTAL.

@jtattermusch
Copy link
Contributor Author

@anandolee , what's the timeline for the next protobut 3.5 release? I'd like to make sure this change will be included.

@jskeet
Copy link
Contributor

jskeet commented Nov 9, 2017

I won't be able to look again until tomorrow - about to board a flight.

@anandolee
Copy link
Contributor

Jisi has already did a cut off and the change logs are ready. I assume the release will be in next week or two.
@pherl are we able to include this change in 3.5?

@@ -64,7 +64,6 @@ internal IMessage CreateTemplate()
/// <returns>The newly parsed message.</returns>
public IMessage ParseFrom(byte[] data)
{
ProtoPreconditions.CheckNotNull(data, "data");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The downside of removing this is that the parameter name will be wrong in the exception (buffer rather than data). But I'm not massively worried.

@jtattermusch jtattermusch merged commit ce0a532 into protocolbuffers:master Nov 10, 2017
liujisi added a commit that referenced this pull request Nov 10, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants