Why Android developers are gradually replacing JSON with FlatBuffers

Why are Android developers gradually replacing JSON with FlatBuffers
1. What is FlatBuffers?
  Brothers Education www.lampbrother.net, FlatBuffers is an efficient cross-platform serialization library that can be used in C++, C#, C, Go, Java, JavaScript, PHP and Python. It was developed by Google for use in game development and other performance-conscious applications.

2. Why use FlatBuffers?
  Access serialized data without escaping/unpacking — FlatBuffers differs from other libraries in that it uses binary buffer files to represent hierarchical data so that they can be accessed directly without escaping and unpacking, while Also supports data structure evolution (forward, backward compatibility).
  Memory efficient and fast—Accessing data only requires accessing buffers in memory. It doesn't require extra memory allocation (at least in C++, may change in other languages). FlatBuffers are also suitable for use with mmap or data streams, where only part of the buffer needs to be stored in memory. The access speed is close to the original structure access, with only a little delay (a kind of virtual function table vtable), in order to allow format upgrades and optional fields. FlatBuffers are suitable for projects that spend a lot of time and space (memory allocation) accessing and building serialized data, such as games and other performance-sensitive applications. You can refer to the benchmark here.
  Flexible - thanks to optional fields, you not only have strong upgrade and fallback compatibility (especially important for older games, where you don't have to upgrade all data for each version), when choosing what data to store and designing the data structure Also very free.
  Lightweight code footprint — FlatBuffers requires only a small amount of generated code, and a small header file representing minimal dependencies, making it easy to integrate. See the benchmark page above for details.
  Strong typing - compile errors without having to write repetitive error-prone runtime checks yourself. It can automatically generate useful code.
  Ease of use - Generated C++ code allows for streamlined access and build code. There are also optional methods for implementing chart escaping, JSON-like runtime string representation, and more. (The latter is faster and more memory efficient than the JSON escape library) The
  code is cross-platform and has no dependencies - C++ code can run on any modern gcc/clang and VS2010. There are also build files for testing and examples (.mk files for Android, cmake files for other platforms).

3. Who uses FlatBuffers?
Brothers Education www.lampbrother.net will show you their feedback on using FlatBuffers:
  BobbleApp, India's No. 1 sticker app. After we use FlatBuffers in BobbleApp, the performance of the App is significantly improved.
  Cocos2d-x, the first open source mobile game engine, uses FlatBuffers to serialize all game data.
  Facebook uses FlatBuffers for client-server communication in Android App. They wrote an article describing how FlatBuffers can speed up loading content.
  Google's Fun Propulsion Labs make heavy use of FlatBuffers in all of their libraries and games.

4. How much has the app performance improved?
  Escape speed It takes 35ms to escape a 20KB JSON stream (which is almost the return size of BobbleApp), which is 16.6ms longer than the UI refresh interval. If we escape the JSON, we'll get frame drops (visual stutter) as we swipe to load the cache from disk.
  Escaper Initializing a JSON escaper requires building a field map and then escaping, which takes 100ms to 200ms, which obviously slows down the app startup time.
  Garbage collection creates a lot of small objects when escaping JSON, and in our experiments, escaping a 20kb JSON stream required about 100kb of instantaneous storage to allocate, putting a lot of pressure on Java memory reclamation.

5. FlatBuffers vs JSON
  Brothers Xiaobian tried to use FlatBuffers and JSON to escape 4mb JSON files.
  FlatBuffers took 1-5ms, JSON took about 2000ms. There is no GC in Android App during the use of FlatBuffers, while many GCs occur when using JSON. The UI is completely stuck when using JSON, so for real use it can only be escaped on a background thread.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326641489&siteId=291194637