【问题标题】:What is the offset size in FlatBuffers?FlatBuffers 中的偏移量大小是多少?
【发布时间】:2021-07-12 07:57:16
【问题描述】:

我们数据的主要部分是strings,可能存在子字符串重复(例如,域 - “some.thing.com”和“thing.com”)。我们想重用子字符串来减少文件大小和使用 FlatBuffers 的内存消耗,所以我计划使用[string],因为我可以引用一些现有的子字符串,例如。 thing.com 将只是使用let substr_offset = builder.create_string("thing.com") 创建的字符串,“some.thing.com”将存储为[builder.create_string("some."), substr_offset]

然而,似乎引用是有代价的,所以引用可能没有好处是字符串太短(小于偏移变量大小)。这是对的吗?偏移类型只是usize?使用 FlatBuffers 表示前缀/后缀字符串有哪些更好的替代方案?

PS。顺便说一句,string array 而不仅仅是string 成本是多少?是不是又多了一笔抵消成本?

【问题讨论】:

    标签: data-structures flatbuffers


    【解决方案1】:

    字符串和向量都通过 32 位偏移量寻址,并且前缀为 32 位大小的字段。所以:

    "some.thing.com" 14 chars + 1 terminator + 4 size bytes == 19.
    

    或者:

    "thing.com" 9 chars + 1 terminator + 4 size bytes == 14.
    "some." 5 chars + 1 terminator + 4 size bytes == 10.
    vector of 2 strings: 2x4 bytes of offsets + 4 size bytes = 12.
    

    总数:36 在这 36 个中,有 14 个是共享的,剩下 22 个字节的唯一数据,比原始数据大。因此,共享字符串需要 13 个字节或更大才能使这项技术值得,假设它经常共享。

    详情:https://google.github.io/flatbuffers/flatbuffers_internals.html

    【讨论】:

      【解决方案2】:

      根据doc,偏移量似乎是uint32(4 个字节,而不是usize)。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-02-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-03-31
        相关资源
        最近更新 更多