代码之家  ›  专栏  ›  技术社区  ›  Elliot Vargas

协议缓冲区中getSeriedSize()的性能惩罚

  •  6
  • Elliot Vargas  · 技术社区  · 16 年前

    打电话要罚款吗 getSerializedSize() 在将消息序列化为 writeTo(OutputStream) ?

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

    我在Java上使用GPB。

    1 回复  |  直到 16 年前
        1
  •  8
  •   ire_and_curses    16 年前

    没有性能惩罚。来自的第二条消息 this thread 在Protobuf邮件列表上:

    您可以调用getSerializedSize()提前查明消息大小。 注意,调用这个函数并不会浪费任何时间,因为结果是 缓存,无论如何在序列化期间都必须调用它。