Friday, September 11, 2009

Tearing down restraints

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
};
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.



SerializerSerializeDeserializeSer. ticksDeser. ticks
ProtoSerializer391,28628,4836
Protobuf.NET934,773240,56932
DataContractSerializer3577,459941,413599


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. >:)

Tuesday, May 26, 2009

Where it all began

I was about eight when I had my first experience with a computer. I became utterly infatuated with the unseen world of magic, and decided to see for myself, how deep the rabbit hole goes. 

I went to a technical school specializing in IT, to see if it works for me. And I saw that it was good. Then I proceeded to a college, and - hon(or)ing my humane skills - became an IT teacher, specialized on software development.

After a year of teaching, I saw that it was not what I wished - I was craving for the first-hand experience of successfully developed software. By then I was studying development in .NET/C# for 3 years. Despite my experience, it took a while to settle in the industry, due to the lack of demonstrable development.

Finally, I was hired by a small company developing all kinds of custom software and a few customizable products. After a few months, I became - by twist of fate - the lead developer of at two projects at first - king of the flatland, for we were few. But, we held on and succeeded against the odds, and delivered support and fixes to existing customers 'round the clock. I was - yet again - out of options: as a developer I cannot improve if all I'm allowed to do is fixing.

So I ventured deeper into the hole - I handed in my resignation, and after a few days, I was already on my way to happiness. After finishing a short project of a hybrid (ASP, ASP.NET, etc.) Computer Aided Teaching software, I was hired by a company involved in the broadcast business. As soon as I was hired, I was put to implementing the server side of a complex, new-generation media asset management system, from database to client library. The challenge was great, the need burning, so I rushed head-on to development, and did what was needed - in a few weeks. After the hands-on experience on the system, I advised changes to compensate for our numbers.

Generated SQL scripts instead of static ones, refactored and generic data layer to support runtime customization, use of code-generation and refactoring tools, and such.

After a few weeks, I demonstrated a set of code templates which enabled us to apply (in a matter of seconds) the tedious modifications of the system when something was changed at the bottom - thus, the WCF service and proxy templates were born. And I saw that it was good, but not good enough.

I started to lobby against the metadata storage mechanism we had, and found support in my colleagues. Thus, the plans of a new data access layer was born. I developed the core, and most of its functionality which consists of an attribute/reflection-based data mapper, a dynamic SQL generation tool and contextual data handling. And saw that it was WAY better than before. I created another tool then, one, that allowed us to generate the bulk of the SQL scripts according to runtime customization data - thus, the database creator project was born. And we saw, that it was way better, than before.

Now, we have a well-balanced, ultra-fast, highly customizable WCF-based client-server application. A new era is about to dawn.