→ C#

How to improve performance of your own Inversion of Control Framework?

I enjoy doing anything by myself instead of using well know frameworks. I am not saying that I am using only my own frameworks. It just gives me more knowledge about how these frameworks doing their job. Inversion of Control Framework it is one of these examples. I have a blog post on Russian about simple implementation of IoC container. You can look on it here Simple IoC Container (I used Microsoft Translator for this link). Actually right now is more than just one class, I have my own framework, which I use for some my applications, like gMusic (you can follow this project on GitHub – Framework).

Couple months ago I met an article IoC Container Benchmark - Performance comparison, where this guy compare different IoC containers (when I first time met this article it was only about performance). So I decided to check performance of my simple container. This is result (I have different count of iterations, so do not compare it to the original blog post):

In this list: "Outcold" – this is my current framework, "IoC A" – it is a class which I showed in this blog post Simple IoC Container (based on Activator.CreateInstance), "IoC E" – it is a different version of IoC class, but based on Expressions (I showed in in one of my comments here). "LightInject" – it is one of the faster frameworks from original blog post about IoC performance, "Unity" – it is one of the famous frameworks. As you can see that, the version of IoC class which is based on Expressions has the worst performance. The funny thing, that I used it everywhere, I really thought that it should be faster than Activator, somebody told me… and I did not verify that…. Anyway, after this article, I spent some time to improve performance of my IoC container, and this is how I did this.

The main problem of IoC container which I implemented is that I did not cache the compiled Expression. Each time when I ask IoC to resolve some object – it compiles expression and after this resolve it:

 

First optimization is simple. Just need to cache expression after first time somebody will ask to resolve object. However, first I need to change how I invoke constructor, because my expression expects constraints I need to change it to parameters, so each "resolving" can use it is own parameters:

 

Now I have Delegate, which can create for me a new object of specific type. The only one problem, that this delegate can have different set of parameters, so I cannot cast it to something like Func<Type1, … , TypeN, Result> and invoke it. I need to call DynamicInvoke, which is slow:

 

This is the result of this optimization (as you can see it is very good, but still variant with Activator works better):

 

The second step I made – I just changed Expression in such way, so it always takes just one parameter "array of objects". In expression I use this array to cast each item of this array to the parameter in constructor. This is how I do this:

 

This is the result:

 

It is faster than version with Activator! But it still slower than LightInject. I guess it is because LightInject used before System.Reflection.Emit instead of Expressions, or maybe they have better idea how to create this expression for type resolving.

Anyway, it was a good result for me.

Small sample of using Code Contracts

I had read about Code Contracts long time ago, but I didn’t see a reason to use contract instead of simple tests and argument's check with Exception's throws. Only now I’m trying to use them at real project. I didn’t see why I need to write:

Contract.Requires<ArgumentNullException>(argument == null); 

Instead of like I did it before:

if (argument == null) 
    throw new ArgumentNullException(“argument”)

Couple of weeks ago I had reinstall my Windows 7 on my laptop (I bought SSD for my laptop). After I install ReSharper with Visual Studio 2010 after run I accidental choosed “Go to the metadata” instead of like usual “Go to Object Explorer” by Ctrl and Mouse Click. So when I clicked on some class of System.xxx (basic classes of .NET) I looked at source code of these classes and found that they are using Code Contracts. I thought this is why I need to understand why I need to use contracts in my code, so I started to use them in some small applications and tried to find some interesting case.

Register hotkey in system for WPF application

Couple days ago I got a question about how to register hotkey in Windows for WPF application. I remembered that one year ago I was solving the same problem in WinForms application, I was registering hot keys for my application, it was Vista Keys Extender project. I knew that my project worked, so I suggested author of question to use code of my project to solve his problem. But as we learned later in WPF message handle mechanism different from WinForms. So I started to find solution for WPF application. I copied my old code from my old project and started to rewrite it step-by-step.

Config Transformation Tool: Parameters support

Couple of weeks ago I wrote about my small utility Config Transformation Tool, which I wrote with base of web.config transformation task. In those moments I was thinking about opportunity to pass parameters to transform file, to tool can replaces parameters in transformation file with special values. Yesterday I resolved this issue. From now I use class Microsoft.Web.Publishing.Tasks.XmlTransformation which works with strings and XmlDocuments instead of files. I had two tasks: (a) I need method which will replace parameters on values, (b) I need method which will be parse command line and create dictionary of parameters.

Wrox–Professional WCF 4–Windows Communication Foundation with .NET 4

0470563141Couple of weeks ago I got a paper copy of book Pablo Cibraro, Kurt Claeys, Fabio Cozzolino, Johann Grabner - Professional WCF 4: Windows Communication Foundation with .NET 4. This book has not a lot of pages, just about 400. Really, I don’t remember when I saw so thin book about some technology. But maybe this book has not a lot pages, but a lot of interesting themes.

First chapter is patterns and principles of SOA applications. This is the best chapter in this book. When I was reading this chapter I saw that authors have a lot experience of creating applications with Service-oriented architecture. Authors described all possible architecture principles, with which you can create SOA applications. And they did not limit themselves to the principles that are possible with WCF. I think that this chapter is “must read” for all developers, it is doesn’t matter what technologies you are using: php, java or .net. And this is good luck that Wrox published this chapter online: Design Principles and Patterns. It is about 30 pages, so read it right now.

1  2  3