Cette page est affichée en anglais. Une traduction française est en cours.
ScriptsApr 29, 2026·3 min de lecture

FlatBuffers — Zero-Copy Serialization for Performance-Critical Applications

FlatBuffers is a cross-platform serialization library by Google that provides access to serialized data without parsing or unpacking. Designed for games and real-time systems, it supports C++, Java, Go, Python, Rust, and more.

Introduction

FlatBuffers is an open-source serialization library created at Google, originally designed for game development and other performance-sensitive applications. Unlike Protocol Buffers or JSON, FlatBuffers allows direct access to serialized data without a parsing or unpacking step, making reads essentially zero-cost.

What FlatBuffers Does

  • Serializes structured data into a flat binary buffer accessible without deserialization
  • Generates type-safe accessors for C++, Java, C#, Go, Python, Rust, Swift, and more
  • Provides zero-copy reads that access fields directly from the buffer in O(1) time
  • Supports schema evolution with optional fields and forward/backward compatibility
  • Works with FlatBuffers JSON parser for human-readable interchange when needed

Architecture Overview

FlatBuffers stores data in a binary buffer using offset-based navigation. Each object contains a vtable that maps field IDs to their positions in the buffer. Readers traverse the buffer using these offsets without allocating memory or copying data. The flatc compiler reads .fbs schema files and generates language-specific builder and accessor classes.

Self-Hosting & Configuration

  • Install flatc from GitHub releases, Homebrew, or build from the CMake project
  • Define schemas in .fbs files using tables (mutable) and structs (fixed-size, inline)
  • Generate code with flatc --cpp --java --go schema.fbs for each target language
  • Use the FlexBuffers variant for schema-less data when the schema is not known at compile time
  • Integrate with build systems via CMake FetchContent, Bazel, or Gradle plugins

Key Features

  • Zero-copy deserialization eliminates parsing overhead and memory allocation
  • Memory-efficient format stores data inline without metadata bloat
  • Supports optional fields and default values for backward-compatible schema changes
  • Object API provides a friendlier mutable interface when zero-copy is not needed
  • gRPC integration allows using FlatBuffers as an alternative to protobuf wire format

Comparison with Similar Tools

  • Protocol Buffers — Requires parsing into objects; better tooling ecosystem and wider adoption
  • Cap'n Proto — Also zero-copy with built-in RPC; similar performance but smaller community
  • MessagePack — Schema-less binary format; simpler to adopt but no compile-time type safety
  • Apache Avro — Schema-evolution-focused with row-based encoding; popular in Hadoop ecosystems
  • BSON — Binary JSON used by MongoDB; human-friendly but larger and slower than FlatBuffers

FAQ

Q: When should I choose FlatBuffers over Protocol Buffers? A: When deserialization latency matters (games, embedded systems, real-time analytics). If you need broad ecosystem support and do not need zero-copy, protobuf is often simpler.

Q: Can FlatBuffers handle nested or recursive data? A: Yes. Tables can reference other tables via offsets. Recursive structures require using table references rather than inline structs.

Q: Is FlatBuffers suitable for network protocols? A: Yes. It works well for RPC and message passing. It integrates with gRPC and can be used as a drop-in replacement for protobuf on the wire.

Q: How large is the runtime dependency? A: Minimal. Most languages need only a small header or module (a few KB) with no external dependencies.

Sources

Fil de discussion

Connectez-vous pour rejoindre la discussion.
Aucun commentaire pour l'instant. Soyez le premier à partager votre avis.

Actifs similaires