Why Flatbuffers? Why not use some standard libraries such as GSON, or jackson. Is there any benefit of using flatbuffers over other libraries. This post answers it all.
Flatbuffers, by google is an efficient cross platform serialization library, which was originally targeted for games. Instead of jumping into the benefits of Flatbuffers, which you can anyway go through in their official site, I will share some real performance numbers while compared to GSON on the demo app.
Here’s the sample json which was used for the test
And, here’s the flat buffer schema for the same.
Using the flatc compiler, I generated the java classes for the same. You can find the instructions on how to use the compiler in the github repo README file.
Here are some metrics which I measuread using both :
- Loading Time
With gson, it took around 30 - 40 ms to parse the entire json file, whereas with flatbuffers it took 3 - 4 ms for the same. Reason being the data is stored in byte buffer format in the flatbuffer file, so there is no need for parsing or unpacking the file, thus resulting in lesser time accessing the data.
- Memory Allocation
With flatbuffers, since only the buffer uses memory, there were lesser GC calls as compared to Gson (where a lot of small objects are created which needs GC calls to free up memory). As a result the CPU cycle calls were also less. Hence, using flatbuffers proved to be much memory efficient as compared to Gson.
- Facebook : A nice read on how flatbuffers helped them speed up loading their posts. Link
You can head over to the Github project for more information.