开发者

Does google protocol buffers support size calculation before serialization?

开发者 https://www.devze.com 2023-01-24 23:13 出处:网络
My (DSP) application produces data at a constant rate. The rate depends on the configuration that is selected by the user. I would like to know how many bytes are generated per second. The data struct

My (DSP) application produces data at a constant rate. The rate depends on the configuration that is selected by the user. I would like to know how many bytes are generated per second. The data structure contains a repeated (packed) floating point fiel开发者_开发问答d. The length of the field is constant, but can be changed by the user.

Is there a protocol buffers function that will calculate the message size before serialization?


If you have build the message objects, you can call ByteSize() on the message which returns the number of bytes the serializes message would take up. There is a link to the C++ docs of ByteSize.


It's impossible to know ahead of time, because protobuf packs the structures it is given into the fewest bytes possible - it won't use four bytes for int x = 1; for example - so the library would have to walk the entire graph to know the output size.

I believe you could find this out by doing a serialize operation to a protobuf-compliant stream of your own design that just counts the bytes it is given. That could be costly, but no more costly than it would be for the library to do that work.


You could fill out the message without sending it and then call CalculateSize() on it

0

精彩评论

暂无评论...
验证码 换一张
取 消