【问题标题】:Size of Serialized data is not reducing using flatbuffer使用 flatbuffer 不会减少序列化数据的大小
【发布时间】:2020-05-14 22:05:05
【问题描述】:

我已经写了以下fbs文件

命名空间测试;

table polygon {
    x : double;
    y  : double;

}

table layer {
    layer_name : string;
    polygons : [polygon];
}

root_type layer;

我的计划是序列化大约 500 万个坐标并将其转储到一个文件中。问题是我看到的字节数与我预期的相比有所增加。我期望它应该是 aounf (5M* 16) 字节。但我得到的大小是 140000032 字节

这是我用来将序列化数据转储到文件中的 java 代码。

FlatBufferBuilder fbb = new FlatBufferBuilder(0);
    String s = "Product1";
    int str = fbb.createString("layer1");
    int size = 1 * 5 * 1000000;
    int[] offset = new int[size];
    int cur = 0;

    for (double i = 0; i < size; i++) {

        fbb.startTable(2);
        polygon.addX(fbb, i);
        polygon.addY(fbb, i);
        offset[cur++] = polygon.endpolygon(fbb);
    }

    int arrayoffset = layer.createPolygonsVector(fbb, offset);
    layer.startlayer(fbb);

    layer.addLayerName(fbb, str);

    layer.addPolygons(fbb, arrayoffset);
    int bla = layer.endlayer(fbb);

    fbb.finish(bla);
    ByteBuffer bf = fbb.dataBuffer().duplicate();

    File myfile = new File("/tmp/test.dat");

    try {
        FileChannel channel = new FileOutputStream(myfile).getChannel();
        channel.write(bf);
        channel.close();

    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

如果我在这里做错了,请告诉我

【问题讨论】:

    标签: serialization flatbuffers


    【解决方案1】:

    table polygon 更改为struct polygontable 是可扩展的,并且每个元素都有一些固定的开销,并且还通过偏移量由向量引用。 struct 是不可扩展的(这对于 xy 对来说似乎很好),并且必须内联序列化(参见教程中的示例),并且会为您提供您期望的大小。

    【讨论】:

      猜你喜欢
      • 2013-01-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-10-07
      • 1970-01-01
      • 1970-01-01
      • 2020-04-23
      • 2018-01-14
      相关资源
      最近更新 更多