Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to compile protocol buffers schema at runtime?

I can create the schema (Descriptors.Descriptor) at runtime dynamically using FileDescriptorProto, also I'm able to serialize and deserialize messages using DynamicMessage.

However performance of DynamicMessage is not good enough because of the way it constructs the messages. I wonder whether it's possible to compile the schema at runtime and use it when de-serializing messages for better performance.

If protocol buffers doesn't provide a way to compile the schemas at runtime then if it's possible to convert the Descriptors.Descriptor to a temporary .proto file then I can try to generate the classes by calling protoc command from my program and load them to the program using Class.forName API.

like image 542
Boyolame Avatar asked May 06 '26 02:05

Boyolame


1 Answers

Protocol Buffers does not include any kind of built-in "JIT" compiling.

If you want to generate code and compile it with javac at runtime, and you're starting with a FileDescriptorProto, then probably what you'd like to do is invoke the Java code generator directly (without converting the proto definition into text).

You could accomplish this by writing a little C++ program that creates a google::protobuf::compiler::java::JavaGenerator and passes it into google::protobuf::compiler::PluginMain(). You now have a program which reads a CodeGeneratorRequest from standard input and writes a CodeGeneratorResponse to standard output. So, you can execute this program to generate Java code directly from FileDescirptorProtos. It's up to you to do pass this code on to the Java compiler. This should be much easier and cleaner than trying to produce text to pass to protoc.

like image 160
Kenton Varda Avatar answered May 08 '26 15:05

Kenton Varda



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!