I tried to find some recommendations on the web but could not find anything relevant.
Let's say that I am creating a protocol buffer message that will contain a lot of fields (50+). Is it best to keep all the fields at the same level or to organize them in sub-messages? Is there any impacts on performances for one way or another?
Example:
message myMessage{
string field1 = 1;
string field2 = 2;
....
string fieldn = n;
}
vs
message myMessage{
SubMessage1 groupedfieldsbasedonsomebusinesslogic1 = 1;
SubMessage2 groupedfieldsbasedonsomebusinesslogic2 = 2;
message SubMessage1{
string field1 = 1;
string field2 = 2;
...
string fieldx = x;
}
message SubMessage2{
string fieldxplus1 = x+1;
...
string fieldn = n;
}
}
I am not considering readability so much here as there are pros and cons when deserializing to have flat data or nested data. My question is really focus on the technical impacts.
Protobuf has a hard limit of 2GB, because many implementations use 32-bit signed arithmetic. For security reasons, many implementations (especially the Google-provided ones) impose a size limit of 64MB by default, although you can increase this limit manually if you need to.
Field numbers are an important part of Protobuf. They're used to identify fields in the binary encoded data, which means they can't change from version to version of your service. The advantage is that backward compatibility and forward compatibility are possible.
Removing fields is fine, although you might want to mark it reserved so that nobody reuses it in an incompatible way. New code with old data (with the field) will silently ignore it; old code with new data will just load without the field populated, since everything in proto3 is implicitly optional .
There is no "best" - everything is contextual, and only you have most of the context.
However! Some minor thoughts on performance:
Out of all of these things, the one that I would focus on if it was me is the second one.
Perhaps start with something that looks usable, and measure it for realistic data volumes; does it perform acceptably?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With