If you're a mature developer in the business, you should know that sometimes choices are scarce and you cannot save yourself from doing something customized, for your customers' sake. Hence, I started writing our own protobuf-based serializer, mainly due to its tiny output size.
And now, the third generation of the serializer is completed. I just finished testing it on our objects (all of them, I should add) and its speed is unmatched.
That is no big deal, one would say. Yes, this is certainly an area in which countless developers and groups have created their own version, with more or less success regarding speed. I had the chance to inspect at least two open source solution, which made me try building something similar, with one tiny extra feature: serializing the unkown.
Yes, I'm talking about serializing entities wich have System.Object type members, or hashtable-like Dictionary<string,object> members. This at least is a limitation in EVERY single one of the serializers. NONE of them can serialize System.Object, "'cause it's unknown", right?
Well, I had the idea that I AM a developer, and I CAN decide for myself if something's possible. Oh well, some would consider this smugness, but yes, right now, I'm so happy to be right (again) and am full of myself that I don't care.
So yes, since MY solution is the one and only I know about, I can safely tell you what the solution was. Type tagging. Since we're (and probably everyone else is) using this for communication between client and server, both sides know about ALL the possible types a System.Object could mean between roundtrips.
So, I decided to create an inventory of the types we're using, add some primitive types to the mesh (string, byte[], Guid, and the like), and TAG'em for good. One type, one byte. Maybe two if you manage to go above 255 types (like we did). After that, it was just a matter of time and effort.
I modified my serializer v1 to support serializing an extra TypeID as Varint and reading and processing it when deserializing. Of course, I also added the possibility that one knows one's one type better and decides to implement a custom serializer (an interface defines the required methods), then the ProtoSerializer (that's how it's called for the time being) would use that of course.
But, considering everything, it was a bit slower then the publicly available open source ones. Yes, it DID have an edge compared to them (System.Object support), but it WAS slow.
Hence, the third generation was created. Except that I don't use reflection for shuffling through the entities anymore. I took my time and concocted a serializer class generator. All neatly packed into one dynamic assembly. Without Reflection.Emit. Oh, yes.
Plain old string.Format and StringBuilder.AppendFormat does the trick. Of course, only when a serializer is needed the first time, after that it's cached. And the beast has awoken...
I'm not much of a fan of numbers, but for those of you lacking imagination what FAST means, see below. It's a serialization/deserialization benchmark result after serializing this class:
PerfTest obj = new PerfTest
{
Foo = 12,
Bar = "bar",
Blip = 123.45F,
Blop = 123.4567
};
{
Foo = 12,
Bar = "bar",
Blip = 123.45F,
Blop = 123.4567
};
Times are ms, 1,000,000 iterations, and everything else is also the same as Marc Gravell's measurement (see some of his results here). Due to probable hardware differences, I ran the performance test several times to check the results, and it's consistent.
I also wanted to see how many ticks are required for one item, so I divided the total elapsed ticks with the number of items.
I also wanted to see how many ticks are required for one item, so I divided the total elapsed ticks with the number of items.
| Serializer | Serialize | Deserialize | Ser. ticks | Deser. ticks |
| ProtoSerializer | 391,28 | 628,48 | 3 | 6 |
| Protobuf.NET | 934,77 | 3240,56 | 9 | 32 |
| DataContractSerializer | 3577,45 | 9941,41 | 35 | 99 |
Done the math?
Yes. System.Object support is cool.
Yes. 3 ticks are awesome.
No. You don't get your hands on this one, I won't share the code, 'cause it's the property of my employer.
Yes. It sucks not to be able to give it to the community. :(
Yes. It rocks to give the finger to those who doubted me. >:)
