Protocol Buffers 中 getSerializedSize() 的性能损失

发布于 2024-08-14 11:43:39 字数 167 浏览 5 评论 0原文

在使用 writeTo(OutputStream) 序列化消息之前,对 GPB 消息调用 getSerializedSize() 是否会造成性能损失?

在将消息写入输出流之前,我需要能够知道消息的大小。

我在 Java 上使用 GPB。

Is there a performance penalty for calling getSerializedSize() on a GPB message before serializing the message with writeTo(OutputStream)?

I need to be able to know the size of a message before writing it to an output stream.

I'm using GPB on Java.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

绝不服输 2024-08-21 11:43:39

没有性能损失。来自 Protobuf 邮件中此帖子的第二条消息列表:

您可以调用 getSerializedSize() 提前了解消息大小。
请注意,调用此方法实际上不会浪费任何时间,因为结果是
缓存,并且无论如何都必须在序列化期间调用它。

There is no performance penalty. From the second message of this thread on the Protobuf mailing list:

You can call getSerializedSize() to find out the message size ahead of time.
Note that calling this doesn't actually waste any time since the result is
cached, and it would have to be called during serialization anyway.

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