ModernProtobuf - Introduction
Protobuf is a serialization standard, just like JSON, with a structured data model, developed by Google.
While there are many flexible libraries in Java to work with JSON, Jackson and Avaje-jsonb being my favorites, the only option I could find for Protobuf is Google’s own implementation. As mentioned earlier, Protobuf messages follow a typed schema: for this reason Google ships a transpiler, protoc, that can generate Java model classes. The generated code, though, is really verbose and not up to date with modern versions of Java. Moreover, it is not really intended to be edited, which is not optimal if you want to have the protobuf to be part of a publicly available developer API. As a result, most projects that work with Google’s protobuf create wrappers around the already gigantic classes generated by Google to make them more usable. While working on another project, I faced this issues myself and started to develop a custom solution almost three years ago, when I was around sixteen.
My goals with this library were to:
- offer the same flexibility that Jackson’s JSON model offers through annotations and mixins
- be at least as performant as Google’s protobuf library
- generate less and higher quality code, leveraging newer java features such as records and sealed classes/interfaces, than Google and do it in a non-intrusive way, for example through annotation processing or bytecode manipulation, instead of having it all in the model class
- make the generated models easy to read and editable to prevent the need from writing wrappers around the generated classes
After countless iterations, and a lot of on and off development, I think I have something that is production ready and that meets the goals that I had in mind while developing this library.
The project
The project is made up by four modules:
- Base - exports the annotations and types needed by someone that wants to use the library
- Parser - exports a parser for .proto files that can be used by the schema generator to generate java models (supports both Protobuf 2 and 3)
- Tool - a CLI tool to generate and update automatically Java models from .proto schemas
- Plugin - the compile time annotation process that handles code generation for Java models (this handles the builders, serializers and deserializers). Another project, avaje-jsonb, that I discovered on this subreddit a couple of months ago, uses a very similar approach for JSON.
The README explains in detail all of the features that are available, but I still need to write proper Javadocs for the base module.
Performance and code size
Given a .proto schema, like the one below:
message ScalarMessage {
optional fixed32 fixed32 = 1;
optional sfixed32 sfixed32 = 2;
optional int32 int32 = 3;
optional uint32 uint32 = 4;
optional fixed64 fixed64 = 5;
optional sfixed64 sfixed64 = 6;
optional int64 int64 = 7;
optional uint64 uint64 = 8;
optional float float = 9;
optional double double = 10;
optional bool bool = 11;
optional string string = 12;
optional bytes bytes = 13;
}
The equivalent Java model looks like this:
@ProtobufMessage
public record ScalarMessage(
@ProtobufProperty(index = 1, type = ProtobufType.FIXED32)
int fixed32,
@ProtobufProperty(index = 2, type = ProtobufType.SFIXED32)
int sfixed32,
@ProtobufProperty(index = 3, type = ProtobufType.INT32)
int int32,
@ProtobufProperty(index = 4, type = ProtobufType.UINT32)
int uint32,
@ProtobufProperty(index = 5, type = ProtobufType.FIXED64)
long fixed64,
@ProtobufProperty(index = 6, type = ProtobufType.SFIXED64)
long sfixed64,
@ProtobufProperty(index = 7, type = ProtobufType.INT64)
long int64,
@ProtobufProperty(index = 8, type = ProtobufType.UINT64)
long uint64,
@ProtobufProperty(index = 9, type = ProtobufType.FLOAT)
float _float,
@ProtobufProperty(index = 10, type = ProtobufType.DOUBLE)
double _double,
@ProtobufProperty(index = 11, type = ProtobufType.BOOL)
boolean bool,
@ProtobufProperty(index = 12, type = ProtobufType.STRING)
String string,
@ProtobufProperty(index = 13, type = ProtobufType.BYTES)
byte[] bytes
) {
}
(Classes can be used as well if you want mutable objects)
This specific example is around 9 times smaller than Google’s generated code, including the code that needs to be generated at compile time for ScalarMessageBuilder and ScalarMessageSpec.
For performance, here is a comparison with Google’s standard and lite implementations, as well as Jackson’s protobuf module:
Benchmark Mode Cnt Score Error Units
PerformanceBenchmark.modernProtobufPerformance avgt 5 90.875 ± 0.276 us/op
PerformanceBenchmark.googleProtobuf avgt 5 103.559 ± 0.372 us/op
PerformanceBenchmark.googleLiteProtobuf avgt 5 184.178 ± 0.943 us/op
PerformanceBenchmark.jacksonProtobuf avgt 5 586.401 ± 2.222 us/op
A detailed comparison can be found here. As I specify on the Github repo, the code ratio between my library and Google’s depends on the schema you are using. I think my benchmark techniques are also valid, but if anyone wants to dispute them I’m happy to learn something new. The benchmark is available at the same link as the detailed comparison.
Conclusion
If anyone wants to try out the library, please do it’s open source on Github.
There you can find also a more in depth explanation of a lot of features that I built into the library.
The library is used by another project that I maintain, Cobalt, which is a reverse engineering of WhatsApp’s Signal Messaging Protocol, so I think it’s already been battle tested, but I’m happy to work on performance fixes and new features.
I was kind of thinking of using this library to try to apply to a Google internship, if anyone has experience with that I’d be happy to know if I should work on anything else specific(I’m in my first year of university right now).
Previous articles on the sub reddit
- Reified - Enhanced Type Parameters in Java 11 and upwards
- Optionless: Type safety with no overhead
- Named and Optional Parameters in Java 17
- Named and Optional Parameters in Java 17 (Redefined Version)
Originally posted on r/java on May 5, 2024.