使用平面缓冲区不会减少序列化数据的大小
Size of Serialized data is not reducing using flatbuffer
我写了下面的fbs文件
命名空间测试;
table polygon {
x : double;
y : double;
}
table layer {
layer_name : string;
polygons : [polygon];
}
root_type layer;
我的计划是序列化大约 500 万个坐标并将其转储到一个文件中。问题是我看到的是字节数比我预期的增加了。我期望它应该是 arounf (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();
}
如果我在这里做错了什么,请告诉我
将table polygon
改为struct polygon
。 table
是可扩展的,每个元素都有一些固定的开销,并且也由偏移量上的向量引用。 struct
不可扩展(这对于 xy 对来说似乎很好),并且必须内联序列化(参见教程中的示例),并且会给你你期望的大小。
我写了下面的fbs文件
命名空间测试;
table polygon {
x : double;
y : double;
}
table layer {
layer_name : string;
polygons : [polygon];
}
root_type layer;
我的计划是序列化大约 500 万个坐标并将其转储到一个文件中。问题是我看到的是字节数比我预期的增加了。我期望它应该是 arounf (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();
}
如果我在这里做错了什么,请告诉我
将table polygon
改为struct polygon
。 table
是可扩展的,每个元素都有一些固定的开销,并且也由偏移量上的向量引用。 struct
不可扩展(这对于 xy 对来说似乎很好),并且必须内联序列化(参见教程中的示例),并且会给你你期望的大小。