Performance improvement in Android app using Flatbuffers

Android Development
Android Development

JSON is quite popular in Android developers. It weights less, is more human-readable and in general is more dev-friendly than old-fashioned, horrible xml. JSON is language-independent data format. But parsing data and transforming it to e.g. Java objects costs us time and memory resources.

Recently Facebook experienced big performance improvement in data handling in its Android app. It was connected with dropping JSON format and replacing it with FlatBuffers in almost entire app.

Flatbuffers

FlatBuffers is a cross-platform serialization library from Google, created specifically for game development.  It is an evolution of protocol buffers that includes object metadata, allowing direct access to individual subcomponents of the data without having to deserialize the entire object (in this case, a tree) up front.

What makes FlatBuffers so effective?

Serialized data is accessed without parsing because of flat binary buffer, even for hierarchical data. Thanks to this we don’t need to initialize parsers (what means to build complicated field mappings) and parse this data, which also takes time.

FlatBuffers data doesn’t need to allocate more memory than it’s used by buffer itself. We don’t need to allocate extra objects for whole hierarchy of parsed data like it’s done in JSON. FlatBuffers can be used not only for storage but also for networking, and as the in-memory format in the app. This eliminates any transformation of the data from server response up to UI display. This has allowed us to drive toward a cleaner Flat Models architecture, which eliminated additional complexity between the UI and storage layers.

When JSON is used as the storage format, we need to add a memory cache to work around performance issues in deserialization. We also end up adding application and networking logic between UI and storage layers.

Conclusion

FlatBuffers is a data format that removes the need for data transformation between storage and the UI. In adopting it, we have also driven additional architectural improvements in our app like Flat Models. The mutation extensions that we built on top of FlatBuffers allow us to track server data, mutations, and local state all in a single structure, which has allowed us to simplify our data model and expose a unified API to our UI components.

In last six months, after transitioning most of Facebook on Android to use FlatBuffers as the storage format. Some performance improvement numbers include:

Story load time from disk cache is reduced from 35 ms to 4 ms per story.

Transient memory allocations are reduced by 75 percent.

Cold start time is improved by 10-15 percent.

It’s exciting to see a choice in data format allow people to spend just a little more time reading their friends’ updates and seeing photos of their families.

 



Leave a Reply