谷歌协议缓冲区是否支持序列化前的大小计算?

发布于 2024-10-02 03:57:36 字数 128 浏览 0 评论 0原文

我的 (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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

小嗲 2024-10-09 03:57:36

如果您已经构建了消息对象,则可以对消息调用 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.

甩你一脸翔 2024-10-09 03:57:36

提前知道是不可能的,因为 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.

蘑菇王子 2024-10-09 03:57:36

您可以填写消息而不发送它,然后调用 CalculateSize() 就可以了

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文