使用 Protocol Buffers 更快反序列化的建议

Suggestions for faster deserialization using Protocol Buffers

我正在使用 Protocol Buffers 以便 serialize/deserialize 数据。我定义了 Protocol Buffers 消息文件如下:

syntax = "proto3";
package Tutorial;
import "google/protobuf/timestamp.proto";

message PointCloud {
  int32 width  = 1;
  int32 height = 2;

  message Point {
    float x     = 1;
    float y     = 2;
    float z     = 3;
    fixed32 rgb = 4;
  }
  repeated Point points = 3;
  google.protobuf.Timestamp timestamp = 4;
}

我能够接收序列化数据。我正在使用 ParseFromArray API 如下:

zmq::message_t msg;
int rc = zmq_socket.recv(&msg);
if (rc){
    std::chrono::steady_clock::time_point begin = std::chrono::steady_clock::now();
    Tutorial::PointCloud point_cloud;
    point_cloud.ParseFromArray(msg.data(), msg.size());
    std::chrono::steady_clock::time_point end = std::chrono::steady_clock::now();
    std::cout << "Time (ms): " << std::chrono::duration_cast<std::chrono::microseconds>(end - begin).count()/1000.0 << std::endl
}  

以上方法有效,但反序列化数据需要足够的时间。在 Ubuntu 14.04 LTS 64 位 OS 中平均需要大约 96 毫秒。仅供参考,我还打印了 msg.size() 并发现它大约为 3773550。

我正在寻找比这更快的反序列化数据的建议。

简答,可能没有办法

Protobuf 的反序列化速度很慢,因为它需要从一系列键值对中动态构建对象。如果您关心性能,请尝试 flatbuffer 或 Capn'Proto。这些是不需要任何对象构造但(可能)在磁盘上花费更多并且有其他缺点的替代方案。