FlatBuffers Survey - PaddlePaddle/Mobile GitHub Wiki

What is FlatBuffers

FlatBuffers is a cross platform serialization library, the library currently supports the following languages:

  • C++
  • C#
  • C
  • Go
  • Java
  • JavaScript
  • PHP
  • Python

Why FlatBuffers?

The following characteristics of FlatBuffers set them apart from other serialization libraries:

  1. FlatBuffers represents hierarchical data in a flat binary buffer which gives the advantages of both - not having to parse before accessing the data and supporting the forward-backward compatibility.
  2. The only extra memory needed to access the data is the buffer. No other additional allocations are required (at least in C++). FlatBuffers works very well for use with mmap or streaming input, as it only requires a part of the buffer to be in memory. Hence the speed of access to the data is very close to that of a struct access with one extra indirection for format evolution and optional fields. FlatBuffers are suitable for applications where spending time and space to access or construct serialized data is highly restricted.
  3. Optional fields in FlatBuffers give a lot of choice in what we write and what we don't, how we design data structures and provide good forward and backward compatibility. Hence FlatBuffers come with a lot of flexibility.
  4. The code footprint generated by FlatBuffers is pretty small and that includes the code generated as well as a small header.
  5. The C++ code generated by FlatBuffers allows for efficient access. There is also an optional functionality for parsing schemas and JSON-like representations at runtime efficiently.
  6. FlatBuffers allows for very efficient cross platform code with no dependencies. For example the C++ code will work with any recent gcc/clang and VS2010. FlatBuffers comes with build files as well.

Difference between Protocol Buffers and FlatBuffers

FlatBuffers is very similar to Protocol Buffers, there are a few stark differences:

  1. FlatBuffers does not need a parsing step to the representation before you can access the data. The parsing step is often coupled with a per-object memory allocation.
  2. The code generated by FlatBuffers is much smaller than that of Protocol Buffers.
  3. Protocol Buffers does not have the optional feature that FlatBuffers has.

JSON and FlatBuffers

JSON is very readable and very convenient to use with dynamically typed languages like JavaScript. But there are some obvious drawbacks when we try to serialize data from statically typed languages. JSON not only has runtime inefficiency and needs more code to be written to access the data. FlatBuffers has these obvious advantages over JSON.

Usage of FlatBuffers

The following steps walk us through the basic usage overview of FlatBuffers:

  1. Write a schema file that defines the data structures that will be serialized. The fields in the schema can have a scalar type (ints/floats), or they can be: string, array, reference to another object or a set of objects. Fields are optional and have default values.
  2. Use flatc (the FlatBuffers compiler) to generate a C++ header to construct the serialized data. This header only depends on flatbuffers.h, which defines the core functionality.
  3. Use the FlatBufferBuilder class to construct a flat binary buffer. The functions allow you to add objects to this buffer recursively.
  4. Store or send your buffer somewhere.
  5. When reading it back, you can obtain the pointer to the root object from the binary buffer, and from there traverse it in-place with object->field().