Language: CPP
Serialization
FlatBuffers was created at Google by Wouter van Oortmerssen in 2014 to overcome performance limitations of Protocol Buffers in gaming and mobile environments. Unlike Protobuf, FlatBuffers does not require deserialization: data can be directly accessed from the buffer, enabling faster performance with lower memory usage. It has become widely used in game engines, real-time applications, and machine learning frameworks like TensorFlow Lite.
FlatBuffers is a highly efficient cross-platform serialization library developed by Google. It allows direct access to serialized data without parsing/unpacking, making it ideal for games, mobile apps, and high-performance applications.
sudo apt install flatbuffers-compiler libflatbuffers-devbrew install flatbuffersvcpkg install flatbuffersFlatBuffers uses a schema definition file (`.fbs`) to describe structured data. The `flatc` compiler generates C++ (and other language) code, which can then be used to build and access serialized objects directly from memory.
table Monster {
id:int;
name:string;
hp:int;
}
root_type Monster;Defines a simple schema for a `Monster` object in a `.fbs` file.
#include "monster_generated.h"
#include <flatbuffers/flatbuffers.h>
flatbuffers::FlatBufferBuilder builder;
auto name = builder.CreateString("Orc");
MonsterBuilder monster(builder);
monster.add_id(1);
monster.add_name(name);
monster.add_hp(100);
auto orc = monster.Finish();
builder.Finish(orc);
uint8_t* buf = builder.GetBufferPointer();
size_t size = builder.GetSize();Builds a `Monster` object and serializes it into a FlatBuffer.
auto monster = GetMonster(buf);
std::cout << monster->name()->str() << " has HP: " << monster->hp() << std::endl;FlatBuffers allows direct access to serialized data without unpacking.
auto weapons = builder.CreateVectorOfStrings({"sword", "axe", "bow"});FlatBuffers supports strongly typed vectors for lists of data.
flatc --json --monster.fbs monster.jsonFlatBuffers can serialize/deserialize JSON for debugging or compatibility.
Older schemas can still read newer data as long as fields are added with defaults.FlatBuffers supports forward and backward compatibility in schemas.
Use FlatBuffers for performance-critical applications like games and real-time systems.
Prefer accessing fields directly from the buffer instead of copying data.
Design schemas with optional fields for forward compatibility.
Use vectors for large repeated data instead of nested tables.
Avoid excessive nesting in schemas, as it can reduce performance.