谷歌协议缓冲区是否支持序列化前的大小计算?
我的 (DSP) 应用程序以恒定速率生成数据。速率取决于用户选择的配置。我想知道每秒生成多少字节。该数据结构包含重复(压缩)浮点字段。该字段的长度是恒定的,但可以由用户更改。
是否有协议缓冲区函数可以在序列化之前计算消息大小?
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 field. 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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您已经构建了消息对象,则可以对消息调用 ByteSize(),该方法返回序列化消息将占用的字节数。有一个指向
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.
提前知道是不可能的,因为 protobuf 将给定的结构打包成尽可能少的字节 - 例如,它不会使用四个字节来表示
int x = 1;
- 所以库将有遍历整个图表以了解输出大小。我相信您可以通过对您自己设计的符合 protobuf 的流执行序列化操作来发现这一点,该操作仅计算给定的字节数。这可能成本高昂,但不会比图书馆做这项工作的成本更高。
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.
您可以填写消息而不发送它,然后调用 CalculateSize() 就可以了
You could fill out the message without sending it and then call CalculateSize() on it