Search code examples
flatbuffers

What is the purpose of the header in flatbuffers?


I've been trying to understand the wire-format, and as I understand the first 4 bytes simply encode the (offset) to the root-type. While I seem to understand this, I can't understand the motivation for this indirection. Why isn't just the root_type stored at byte 0-4?


Solution

  • It's the offset to the root table. It is needed because a root table is usually precededed by a vtable of variable size + alignment (see https://google.github.io/flatbuffers/flatbuffers_internals.html) so even though in theory we could start the buffer with a vtable and compute the start of the root table from that, it was simpler and more consistent to simply store an offset to it.

    There are other small details about the format that are not entirely optimal in hindsight, but the big thing about a serialization format are that its forever forwards/backwards compatible, so they won't change now :)