This repository has been archived by the owner on Jan 24, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 29
FlatBuffers Survey
kavyasrinet edited this page Dec 1, 2017
·
1 revision
FlatBuffers is a cross platform serialization library, the library currently supports the following languages:
- C++
- C#
- C
- Go
- Java
- JavaScript
- PHP
- Python
The following characteristics of FlatBuffers set them apart from other serialization libraries:
- 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.
- 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.
- 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.
- The code footprint generated by FlatBuffers is pretty small and that includes the code generated as well as a small header.
- 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.
- 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.
FlatBuffers is very similar to Protocol Buffers, there are a few stark differences:
- 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.
- The code generated by FlatBuffers is much smaller than that of Protocol Buffers.
- Protocol Buffers does not have the optional feature that FlatBuffers has.
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.
The following steps walk us through the basic usage overview of FlatBuffers:
- 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.
- 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.
- Use the FlatBufferBuilder class to construct a flat binary buffer. The functions allow you to add objects to this buffer recursively.
- Store or send your buffer somewhere.
- 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().