Why Flatbuffers ?

Serialization Performance

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

{
  "event": [
    {
      "id": 1,
      "name": "Android Conference",
      "location": "Delhi",
      "date": "2016/04/05"
    },
    {
      "id": 2,
      "name": "iOS Conference",
      "location": "Bangalore",
      "date": "2016/05/01"
    },
    ...
    ...
}

And, here’s the flat buffer schema for the same.

table EventList {
    event : [Event];
}

table Event {
    id : long;
    name : string;
    location : string;
    date : string;
}

root_type EventList;

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 :

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.

Comparison

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.

Sample Project

You can head over to the Github project for more information.