structuremap/structuremap — pull request #297 by BeeWarloc (2014-10-07 08:53:58+00:00)
Title: Optimization for generating Delegate from constant value expression in BuildPlan

Hi..

We are currently using structuremap in our Nancy based API, and after a bit of profiling I was surprised to learn that structuremap used as much as 3ms for resolving injected instances from a nested container.

I then attempted profiling `BuildPlan.ToDelegate` while running the structuremap unit tests. In this situation it used ~0.7ms, which is still more than I would expect.
```csharp
for (int i = 0; i < 2; i++)
{
using (container.GetNestedContainer())
{
container.Inject(new Whatever());
var sw = new Stopwatch();
sw.Start();
var whatever = container.GetInstance();
sw.Stop();
if (i == 1)
Console.WriteLine("{0}: Resolve time {1}ms", i, sw.Elapsed.TotalMilliseconds);
}
}

// output: 1: Resolve time 0,703ms
```
I don't know the exact reason why Expression.Compile is slower with the exact same expression in our application, but I suspect the number of currently loaded assemblies could have something to do with it.

Anyway, both 3ms or 0.7ms is too slow for this simple and common use case, as this time is added to every request. This pull request contains an optimization for instance object resolving, as this will never throw any exceptions.

After this change the output of the code above is 0.0434ms.

I guess there should have been some unit test here to, but I'm a bit unsure of exactly what to test.
If this patch is at all interesting.. :)