container icon indicating copy to clipboard operation
container copied to clipboard

Optimizing UnityContainer constructors

Open ENikS opened this issue 6 years ago • 0 comments

Changing how UnityContainer is created

Name

As explained in this issue the container does not provide any means of identifying individual instances. To fix this problem a property Name has been added to IUnityContainer interface.

By default, if no name is provided, a root UnityContainer will have a name 'root' and child containers will have no name (null).

Memory optimization

Unity, as any other general purpose storage (List, Dictionary, etc.) allocates memory dynamically. It creates relatively small buffer and resizes it in small increments (25% - 30% of already allocated size) as required.

The algorithm is trying to minimize memory footprint and allocates as little as possible. It works well for relatively small number of registrations, but when number of registered components is relatively large, all these minimization efforts just add to the overhead.

To work around this issue, new constructors allow passing initial capacity a user expects it to hold:

public UnityContainer(string name, int capacity);

For compatibility and when no name of allocations are required following overloads are provided:

public UnityContainer();
public UnityContainer(string name);
public UnityContainer(int capacity);

ENikS avatar Dec 19 '19 21:12 ENikS