protocol-buffers - protobuf 消息中的最大字段数

标签 protocol-buffers grpc-python

protocol buffers 官方文档 https://developers.google.com/protocol-buffers/docs/proto3说 protobuf 消息中字段的最大字段数是 2^29-1。但为什么是这个限制?
请问谁能详细解释一下?我是新手。

我在 why 2^29-1 is the biggest key in protocol buffers 阅读了这个问题的答案.
但我没有澄清

最佳答案

编码 Protocol Buffer 中的每个字段都有一个 header (称为键或标签)作为实际编码值的前缀。 encoding spec定义这个键:

Each key in the streamed message is a varint with the value (field_number << 3) | wire_type – in other words, the last three bits of the number store the wire type.



这里的规范说标签是一个 varint,其中前 3 位用于对电线类型进行编码。 varint 可以编码一个 64 位的值,因此只要继续这个定义,限制就是 2^61-1 .

除此之外,Language Guide将其缩小到最大 32 位值。

The smallest field number you can specify is 1, and the largest is 2^29 - 1, or 536,870,911.



没有给出原因。我只能推测这背后的原因:
  • 人为限制,因为没有人期望消息具有这么多字段。只需考虑将具有这么多字段的消息放入内存中。
  • 由于 key 是一个 varint,它不仅仅是原始缓冲区中接下来的 4 个字节,而是一个可变长度的字节( Java code reading a varint32 )。每个字节有 7 位实际数据和 1 位指示是否到达结束。出于性能原因,人们认为限制范围会更好。
  • 由于 proto3 是第三个版本的 Protocol Buffer ,可能是 proto1 或 proto2 将标签定义为 varint32。为了保持向后兼容性,这个限制在今天的 proto3 中仍然适用。
  • 关于protocol-buffers - protobuf 消息中的最大字段数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57520857/

    相关文章:

    java - Protobuf Java 到 C++ 序列化 [二进制]

    没有<iostream>的c++ nanopb Protobuff

    python - python中protobuf grpc相对导入路径差异

    go - 测试用多种语言编写的Grpc服务

    macos - 构建@com_google_protobuf 时缺少依赖项//:protobuf_lite

    serialization - 如何使用 Protobuf-Net 序列化 .Net Exceptions?

    python - 如何在python中热重载grpc-server?

    c++ - protobuf 与 zeromq 与 C++ 的链接错误

    python - 如何在没有客户端 SSL 证书的情况下在 python gRPC 客户端中打开安全通道