Wednesday 6 July 2011

Comparing ProtoBuf-net Serialisation

ProtoBuf-net is a C# engine for Protocol Buffers, a binary serialisation format created by Google.

The project is written and maintained by Marc Gravell who currently works for Stack Overflow. 

The source for this project can be found here: http://code.google.com/p/protobuf-net/

probuf-net_logo

I’ll be comparing the size of the output along with the speed for serialisation and deserialisation speed for the following methods:

  • Xml Serializer with binary formatter using the [Serializable] attribute
  • Data Contract Xml Serializer using the [DataContract] & [DataMember] attributes
  • Data Contract Json Serializer again using the [DataContract] & [DataMember]
  • ProtoBuf-net v2 BETA (but used by StackOverflow) using the reflection and [ProtoContract] & [ProtoMember] attributes
  • ProtoBuf-net v2 BETA (but used by StackOverflow) using a compiled model with no attributes on my data classes
  • ProtoSharp ALPHA (another C# Protocol Buffers serialisation engine by Torbjorn Gyllebring)

For my tests I’ll be using the following model:

model

Here’s the object model view when populated with data:

Data

Simple to use

Here is an example of how to create a Protobuf-net compiled model as opposed to attributes on the class and fields:

Write and Read from Memory Stream

ScreenShot077

Shared Object References

If you have multiple pointers to the same object you can utilise the graph support added in v2.

This example was taken from a Stack Overflow question answered by Marc Gravell:

http://stackoverflow.com/questions/6063729/does-protocol-buffers-support-serialization-of-object-graphs-with-shared-referenc/6063837#6063837

Conclusion

With Protobuf-net serialisation and deserialisation times are reduced so you should get a quick win out of the box.

If the largest bottleneck in your solution is the transfer speed of data between processes this engine can greatly benefit your project as the data footprint is dramatically reduced.

In order to accurately calculate if your solution would benefit from the use of ProtoBuf-net, you’ll need to set up scenarios that mimic your real world requirements as closely as possible.

Downloads

You can download the source code here:

http://stevenhollidge.com/blog-source-code/Protobuf-net-Tests.zip

1 comment:

  1. Nice. Please note that your "using compiled model" example is actually just using the ability to use models without attributes etc; the library will automatically compile a model using *either* approach as soon as it needs to.

    i.e. your code just shows another way to *configure* it. It should perform the same either way unless you have explicitly turned off automatic compilation.

    ReplyDelete