I recently started reading and employing gRPC in my work.
gRPC uses protocol-buffers internally as its interface definition language.
I keep reading that protocol-buffers perform much better and faster than JSON and XML.
What I fail to understand is:
String representations of data:
Both text-based and binary-based serializers can be fast and efficient (or slow and horrible)... just: binary serializers have the scales tipped in their advantage. This means that a "good" binary serializer will usually be faster than a "good" text-based serializer.
Let's compare a basic example of encoding the integer 42:
{"id":42}
(9 bytes assuming ASCII/UTF-8 and no whitespace)<id>42</id>
(11 bytes assuming ASCII/UTF-8, no whitespace and no namespace noise)0x08 0x2a
(2 bytes)Now imagine writing a general purpose XML or JSON parser, and all the ambiguities and scenarios you need to handle just at the text layer, then you need to map the text token "id"
to a member, then you need to do an integer parse on "42"
.
In protobuf, the payload is smaller, plus the math is simple, and the member-lookup is an integer (so: suitable for a very fast switch
/jump).