We wanted to optimize the way we combine multiple protobuf messages. Our scenario was this, we had a
NestedRepeatedMessage that contained a
NestedSingleInt message. We were creating a
NestedRepeatedMessage which was the same for multiple users, so were serializing it once and we were posting it to be sent to that user. After some tests, we detected that sending 1 message per send operation is verry expensive, so we had to find a solution to combine multiple serialized messags before sending them.
The first solution was to, keep a reference to the protobuf object that was used to serialize, and if the client had multiple messages in the queue, we would create a big
NestedRepeatedMessage and we would create a copy of each
NestedSingleInt from those
NestedRepeatedMessage from the queue. If there was only one element in thew queue, we would use the serialized version. This was also expensive..
Our next step, was to find a way to use the already serialized string. After understanding how the the protobuf encoding works, we've noticed that if append the serialized
NestedRepeatedMessage messages, it results the same serialized string as the one from above, where we were doing a copy of all messages, adding them to a big message and then re-serializing it.
Below you can find all steps that were done to get to this conclusion.