container icon indicating copy to clipboard operation
container copied to clipboard

Optimize Injection Members

Open ENikS opened this issue 6 years ago • 0 comments

Description

Injection members are the things that shape how Unity creates and resolves requested types. During resolution members provide information about constructors, methods, properties, fields, and all kinds of parameters that require initialization.

When created, injection members are given just a basic information about the member it should initialize. For example InjectionProperty would be given a name of the property and possibly a value to initialize that property with.

During registration injection members are initialized with a type that is being registered and perform basic matching to determine if the type has requested member. During this time the member info is selected and partially validated.

Problem

There are several problems with this approach:

  • Selection algorithm, implemented by each injection member, performs selection in two steps:

    • Preselection during registration based on registered type
    • Final selection during resolution based on final Type
  • Although validation is performed only in diagnostic mode, each member is querying container for custom validation delegate every time it executes, adding unnecessary overhead.

  • The result of selection is stored inside each injection member and can not be reused with other types. This puts unnecessary strain on memory system

  • Unity container is no longer relying on policies to store registration data but it is still calling AddPolicies on each registered injection member. For more information see this issue

Solution

Injection members should be stripped of all but very basic selection logic and must only hold initial data. All advanced processing should be done by the container itself. Following changes should be implemented:

  • Injection members will only select 'best case scenario' matches. If data does not match precisely, but could still be used with more advanced scenario, this will be done by the container

  • No selection data will be stored in the member

  • No validation will be performed by injection member itself. No exception will be thrown when data does not match. The validation and error handling will be done by the container

  • Remove AddPolicies as redundant.

Breaking Change

No AddPolicies method

The method will be removed from InjectionMember and will no longer be called. Instead of registering policies, container will be scanning array of Injection Members stored with registration, and will be using them as appropriate. This approach will avoid allocating new memory to hold additional policies.

Validation

Currently, when registered, each injection member is evaluated for problems and incompatibilities. When abnormalities are found, exception is thrown.

As stated in this proposal, injection members will not be evaluated until resolution time. So, even invalid members will be accepted by registrations.

The exception will be thrown when type is resolved. If Diagnostic extension is installed, it will perform thorough evaluation before resolving types.

Ranking Selection

Selection of matching members will implement ranking algorithm. Given this choices of methods:

public class TestClass
{
  public TestMethod(object o1, object o2) { }    // 1
  public TestMethod(Array a1,  Array a2) { }     // 2
  public TestMethod(Array a1,  object[] a2) { }  // 3
}

`InjectionMethod(new [] { string.Empty }, new object[] { new object() }) will match with method 3

Impact

This change impacts all derivatives of respective injection members.

See Also

Issue unitycontainer/unity#336 Issue unitycontainer/container#246

ENikS avatar Jun 11 '20 16:06 ENikS