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 <https://github.com/protocolbuffers/protobuf/blob/master/java/core/src/main/java/com/google/protobuf/LongArrayList.java#L68> and keeps reallocating the array and copying <https://github.com/protocolbuffers/protobuf/blob/master/java/core/src/main/java/com/google/protobuf/LongArrayList.java#L199-L201> 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 On Thu, Mar 18, 2021 at 4:22 PM 'Chengyuan Zhang' via grpc.io < [email protected]> wrote: > By default, gRPC uses Protobuf parser > <https://github.com/grpc/grpc-java/blob/f0512db060ac5582547a4e4ebd5f3bd2c568d721/protobuf-lite/src/main/java/io/grpc/protobuf/lite/ProtoLiteUtils.java#L223> > for decoding data. You can definitely implement your own marshaller and do > it yourself (see the Json serialization example > <https://github.com/grpc/grpc-java/tree/master/examples/src/main/java/io/grpc/examples/advanced>). > 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 > <https://github.com/grpc/grpc-java/blob/f0512db060ac5582547a4e4ebd5f3bd2c568d721/core/src/main/java/io/grpc/internal/MessageDeframer.java#L165>, > that's the part you do not have control for. There had been work > <https://github.com/grpc/grpc-java/pull/7375> 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 > <https://github.com/grpc/grpc-java/blob/f0512db060ac5582547a4e4ebd5f3bd2c568d721/protobuf-lite/src/main/java/io/grpc/protobuf/lite/ProtoLiteUtils.java#L188> > of received bytes before handing them over to Protobuf parser (but it does > reuse > the byte array > <https://github.com/grpc/grpc-java/blob/f0512db060ac5582547a4e4ebd5f3bd2c568d721/protobuf-lite/src/main/java/io/grpc/protobuf/lite/ProtoLiteUtils.java#L120> > to avoid excessive amount of allocation and GC). There had been attempts > <https://github.com/grpc/grpc-java/pull/7330> 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. > On Thursday, March 18, 2021 at 10:41:47 AM UTC-7 [email protected] wrote: > >> 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 >> > -- > 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 [email protected]. > To view this discussion on the web visit > https://groups.google.com/d/msgid/grpc-io/480a98d3-08b6-4deb-be91-98323f78a486n%40googlegroups.com > <https://groups.google.com/d/msgid/grpc-io/480a98d3-08b6-4deb-be91-98323f78a486n%40googlegroups.com?utm_medium=email&utm_source=footer> > . > -- 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 [email protected]. To view this discussion on the web visit https://groups.google.com/d/msgid/grpc-io/CAH_YJ5fhVm9NfmNvNtGAoCJzueT_cMm9Uf7juSxkRTOauP%3DczQ%40mail.gmail.com.
