【问题标题】:Flatbuffer wire size larger than expectedFlatbuffer 线尺寸大于预期
【发布时间】:2020-12-21 23:54:08
【问题描述】:

我正在测试一个 flatbuffers 序列化实现,但我看到序列化数据大小与原始数据大小的比率要大得多。我意识到该协议旨在允许向后兼容,并且存在导致一些膨胀的对齐考虑。但是,一旦构建,缓冲区大约是我放入其中的原始数据大小的 2 倍。这对我来说似乎很大,我怀疑这与我如何构建我的模式有关。这是我理想使用的模式。它提供了灵活性,并且对我试图表示的信息类型具有良好的逻辑意义。

// IDL file

namespace Data;

// Structs \\

struct Position {
  x :short;
  y :short;
  z :short;
}

// Tables \\

table Interaction {
  pos    :Position;
  value  :uint;
}

table Event {
  interactions :[Interaction]; // 1-3 interactions are typical in a given event, but could be as high as 30
  id           :ubyte=255;
  time1        :uint;
  time2        :ulong;
}

table Packet {
  events1 :[Event];       // 1000s or more are typical in a given Packet
  events2 :[OtherEvent1]; // Other events that would be defined but occur much less frequently than events1
  events3 :[OtherEvent2]; // Other events that would be defined but occur much less frequently than events1
}

root_type Packet;

根据我构建此架构的方式,这是否是预期的 2 倍电线尺寸?由于给定表中的字段数量少而向量中的元素数量多,这可能是不可避免的吗?我试图通过人为地使每个变量类型具有相同的大小(uint)来减少对齐问题,并且我尝试绕过交互表并直接使事件表具有位置结构的向量(这将消除一些向后兼容性如果我将来需要进行更改,我正在寻找)。我能够将比率降低到的最佳值是 1.7 倍。这是合理数量的额外数据吗?

【问题讨论】:

    标签: serialization flatbuffers


    【解决方案1】:

    是的,对齐、间接偏移、vtables 和其他一些东西都有开销。您最好阅读 https://google.github.io/flatbuffers/flatbuffers_internals.html 以了解这些内容,这将有助于设计尽可能小的表示形式。

    【讨论】:

    • 感谢您将我指向该文档。我觉得我查看了除该文件之外的所有其他文档。了解幕后发生的事情非常有帮助。最终,我的数据是许多组的少量数据,再加上完整的 32 位 uint 用于偏移和矢量大小信息这一事实导致了我看到的大部分“额外”数据。我想出了一些替代方案来缓解这种情况。谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-07-30
    • 1970-01-01
    • 2012-05-31
    • 2016-08-22
    • 1970-01-01
    • 1970-01-01
    • 2023-02-04
    相关资源
    最近更新 更多