Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Boost.Asio with google protocol buffers

I've currently investigating ways of improving our current c++ network hand-made serialization mechanism maintaining our existing binary protocol. The first approach taken was to code it using Boost.Asio with Boost.Serialisation using binary serialization. Anyway it turned up that it's somewhat slower (10%) that our current hand-made implementation. Anyone has actual _real_work_ experience about using google protobuf together with Boost.Asio ?

I searched google for samples but was only able to come-up with this example:

Boost Asio with google protocol buffers sample

Does anybody did this in any actual project ? I've very interested performance figures since this has to be quite fast...

like image 457
jvaz Avatar asked Nov 08 '11 12:11

jvaz


People also ask

What are Google protocol buffers used for?

Protocol buffers are Google's language-neutral, platform-neutral, extensible mechanism for serializing structured data – think XML, but smaller, faster, and simpler.

Are protocol buffers good?

Protocol buffers are ideal for any situation in which you need to serialize structured, record-like, typed data in a language-neutral, platform-neutral, extensible manner. They are most often used for defining communications protocols (together with gRPC) and for data storage.

Should I use Protobuf or JSON?

Protobuf supports more data types than JSON. JSON is limited to certain python objects, and it cannot serialize every python object. Protobuf supports a wider range of data types when compared to JSON. For example, enumerations and methods are supported by Protobuf and not supported by JSON.


1 Answers

We use boost::asio and Protobuf for complex, low message rate protocols. For simple, high message rate protocols we do boost::asio and custom serialization.

The C++ Protobuf library uses std::string to represent the string fields for messages that it deserializes, which means a free store allocation is performed by Protobuf for every string field in every message you receive. That makes Protobuf not very performant for really high frequency messaging.

I would use Protobuf everywhere if I could, though. It's a marvelous tool for making rich, complex, platform independent, forward-and-backward-compatible protocols.

ADDENDUM

Since it seems like people are reading this answer, I should share that I've learned that in C++ Protobuf you can re-use deserialization message objects to reduce the malloc frequency when reading.

See Optimization Tips:

https://developers.google.com/protocol-buffers/docs/cpptutorial

like image 113
James Brock Avatar answered Sep 25 '22 04:09

James Brock