simpleinjector / SimpleInjector

An easy, flexible, and fast Dependency Injection library that promotes best practice to steer developers towards the pit of success.
https://simpleinjector.org
MIT License
1.21k stars 155 forks source link

Simplify locking model #221

Open dotnetjunkie opened 8 years ago

dotnetjunkie commented 8 years ago

Simple Injector currently locks on a very fine grained level, and often pushes the problem of locking to sub parts of the library and to anyone who extends the library. This is problematic because it increases complexity of the library and increases the chance of bugs to appear (as they did because of this reason in the past). For developers extending Simple Injector's default functionality, it is often a very hard model to understand and to get right.

So the locking model should be simplified.

dotnetjunkie commented 8 years ago

Probably the best way to simplify the locking model is by doing a global (container-wide) lock every time an expression tree is built. Downside of this however is that this can have impact on performance of multi-threaded applications during the first part of the lifetime of an application.

Since the current model is fine grained, two parallel requests for two different types that haven't been resolved before, can be processed in parallel without (much) blocking (depending of course on the amount of shared dependencies). This means that (possibly performance heavy) expression tree generation and delegate compilation can happen completely in parallel. After the locking model is simplified, we would see that that two parallel requests for two different types that haven't been resolved before, will be processed sequentially due to a global lock.

This effect is of course temporary; eventually all delegates are compiled which will cause the delegate to be resolved from cache which will always be guaranteed to be lock free.