can I get an handle on the underlying raw byte buffer.

365 views
Skip to first unread message

Smruti Ranjan Sahoo

unread,
Mar 18, 2021, 1:41:47 PM3/18/21
to grpc.io
Hi,
I am new to GRPC and using it in JAVA. when I use the generate client stub, it creates lot of temp object which need to be garbage collected. This can potentially create a lot of Garbage Collection pressure for high throughput java applications. To work around this issue, I want to get an handle on to the underlying byte buffer and decode the data myself. Is that possible? If so, can I get some pointers to go about this.

Thank You
Smruti

Chengyuan Zhang

unread,
Mar 18, 2021, 7:22:56 PM3/18/21
to grpc.io
By default, gRPC uses Protobuf parser for decoding data. You can definitely implement your own marshaller and do it yourself (see the Json serialization example). However, that's mostly for choosing to use a different format of serialization and probably won't make the problem of creating temp objects better. As most of them happen in deframing messages, that's the part you do not have control for. There had been work for making some improvement, but I believe most of them weren't be having significant impact on garbage collection. That's said, there probably isn't much can be done in terms of improving GC pressure.

On the other hand, the existing Protobuf marshaller does have something can be improved, which is making an extra copy of received bytes before handing them over to Protobuf parser (but it does reuse the byte array to avoid excessive amount of allocation and GC). There had been attempts to eliminate this copy, but it turned out requiring changes in Protobuf or JDK to make the gain larger than the extra cost. There's continued effort on that path. That's being said, if you are going to implement your own marshaller, you may be able to eliminate this copy.

Smruti Ranjan Sahoo

unread,
Mar 19, 2021, 12:31:55 PM3/19/21
to Chengyuan Zhang, grpc.io
Thank you Chengyuan,
The Json serialization example really helped me to use a custom marshaller. But as you said, the annoying part is the generated Message classes and the builders. For the field of type List, it starts with a very small size 10 and keeps reallocating the array and copying over the old values. I would like to preallocate the list at once. I would also want to reuse the instances to minimize GC pressure. Moreover the classes are final, so there is no option to customize. For now, I am thinking of ignoring the generated classes and writing my own message class and populating it through the custom marshaller. I will have to redo the entire decoding piece unless you have some better ideas.

Thank you
Smruti

--
You received this message because you are subscribed to the Google Groups "grpc.io" group.
To unsubscribe from this group and stop receiving emails from it, send an email to grpc-io+u...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/grpc-io/480a98d3-08b6-4deb-be91-98323f78a486n%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages