Skip to content

Protobufs

benliao1 edited this page Jul 31, 2020 · 20 revisions

Protocol Buffers (protobufs) are a messaging protocol made by Google that allows for efficient encoding and decoding of complex data over a data stream (e.g. a network connection). Google's language guide does a really good job of explaining protobufs, so read this first.

After reading, you'll notice that there's no option to choose C as a target language. Luckily, a group of people made a third-party extension of Google protobufs that can take .proto files and generate C source and header files that can be used for a C program to pack and unpack protobuf messages. A good place to understand how that works is the protobuf-c Github and their Wiki.

There are a few reasons why we chose Google Protocol Buffers for our communication with Dawn and Shepherd:

  • Consistency with the old version of Runtime (old Runtime used protobufs as well, and Dawn had experience packing/unpacking Runtime messages with a protobuf extension for JavaScript)
  • Speed. Yes, protobufs is a third-party library, and wasn't C-Runtime designed to use as few third-party libraries as possible? This is true, but this is the one case where we voted in favor of using the third-party library instead of designing a protocol in-house (or using a simpler protocol). It was decided the speed boost gained by using protobufs was worth the trouble of setting it up to work in C. Consider this: suppose we chose a protocol like JSON. To send an boolean, we might need to send the string: "{"switch0":false}". That's 19 characters (i.e. 19 bytes) sent over the network. Compare with protobufs, which would be literally 1 byte (0). Integers and complicated nested message types offer similar amounts of message size reductions. Combine it all together, and the network traffic reduction gained by using protobufs is substantial.
  • Consistency between Runtime communication with Dawn and Shepherd. Shepherd uses JSON internally to send data around, and originally the plan was to communicate with Dawn using protobufs and communicate with Shepherd using JSON. But that's not very smart, because then Runtime would have to convert our internal data into two different message formats, which would be extremely ugly. So the decision was made to use protobufs for all of Runtime's network communications.