Unity Registration Performance Issues

Apr 24, 2014 at 8:20 PM
Background: We have a large legacy application, several million LOC, with an aged ORM that generates several types for our tables. We are using registered types with Unity, and are experiencing performance problems when registering the several thousand types.

Analysis: Our analysis so far with various performance tools and code inspection has led us to the PolicyList class, specifically the Set method. Here is the code:
public void Set(Type policyInterface,
                        IBuilderPolicy policy,
                        object buildKey)
            lock (lockObject)
                Dictionary<PolicyKey, IBuilderPolicy> newPolicies = this.ClonePolicies();
                newPolicies[new PolicyKey(policyInterface, buildKey)] = policy;
                this.policies = newPolicies;
For the number of types we have registered (6 thousand), we have hundreds of millions of recursive calls showing up in ANTS Performance Profiler.

Questions: Why is this not a concurrent dictionary in the latest version of Unity? It would seem the cost of the clone plus the lock each time is an expensive operation, especially when registering so many types. We are not able to reduce the number of types, that is not an option for us at this time.

Are there any suggestions for how to improve the performance here?

Thanks in advance!