在消息中包装数据的开销

Overhead of wrapping data in message

将 protobuf 参数包装在单独的消息中会产生任何额外的运行时开销吗?

这个:

message MyData {
  optional uint32 data = 1;
}

message Container {
  optional MyData data = 1;
}

对比

message Container {
  optional uint32 data = 1;
}

我只在需要时使用 C++ 实现。

额外的电线开销?序列化开销?访问开销?

根据 encoding explanation of protobufs

,每个嵌入的消息都有开销

下面带有一个 int 值的简单消息将被编码为 08 96 01

message Test1 {
  required int32 a = 1;
}

同时,使用嵌入式消息对消息定义进行编码看起来像 1a 03 08 96 01

Test1:  
message Test3 {  
  required Test1 c = 3;  
}

文档解释说

the last three bytes are exactly the same as our first example (08 96 01), and they're preceded by the number 3 – embedded messages are treated in exactly the same way as strings (wire type = 2).

所以简而言之,1a 03 作为开销添加到 Test3 中,因为 Test1 是另一种消息类型