javaprotocol-buffersproto3

Protobuf partial serialization of common fields in Java


I have a protobuf message which I need to send to multiple clients. The message has the same data for each client except for one field (a sequence number) which is different for each client. Now I can change the field in the generated Java message object and serialize the message for each client separately. But is there a way to serialize everything except the one field and then only serialize that (e.g. swap out the corresponding bytes in the serialized message or something) for each client?

Edit: I have seen the mergeFrom method to merge two messages but from my understanding it parses the message first, then swaps out the data, and then you can serialize it again, so not at all a performance optimization (?).


Solution

  • First, I'd want to be very sure that this is actually relevant to performance. If the protocol buffer messages aren't large (I wouldn't even consider this if they weren't multiple kilobytes), then I'd expect that this would make essentially zero difference to performance and you shouldn't even try to optimize this.

    Assuming that you have measured this to be a bottleneck, it's not hard. Concatenating serialized protos constructs the merged form, so presumably this is just

    myMessage.toBuilder().clearSpecialField().build().writeTo(outputStream);
    MyMessage.newBuilder().setSpecialField(...).build().writeTo(outputStream);
    

    (If you've made special_field a required field, against best practices, then you may need to use buildPartial instead.)

    and then you read it as a single proto message

    MyMessage.parseFrom(inputStream);