Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve perf of ActivatorUtilities.CreateInstance() #91290

Merged
merged 10 commits into from
Sep 11, 2023
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// The .NET Foundation licenses this file to you under the MIT license.

using System;
using System.Collections.Concurrent;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Linq.Expressions;
Expand All @@ -17,6 +18,8 @@ namespace Microsoft.Extensions.DependencyInjection
/// </summary>
public static class ActivatorUtilities
{
private static ConcurrentDictionary<Type, ConstructorInfoEx[]> s_constructorInfoEx = new();
steveharter marked this conversation as resolved.
Show resolved Hide resolved
steveharter marked this conversation as resolved.
Show resolved Hide resolved

#if NET8_0_OR_GREATER
// Maximum number of fixed arguments for ConstructorInvoker.Invoke(arg1, etc).
private const int FixedArgumentThreshold = 4;
Expand Down Expand Up @@ -47,6 +50,22 @@ public static object CreateInstance(
throw new InvalidOperationException(SR.CannotCreateAbstractClasses);
}

if (!s_constructorInfoEx.TryGetValue(instanceType, out ConstructorInfoEx[]? constructors))
steveharter marked this conversation as resolved.
Show resolved Hide resolved
{
ConstructorInfo[] ctors = instanceType.GetConstructors();
ConstructorInfoEx[] temp = new ConstructorInfoEx[ctors.Length];
for (int i = 0; i < ctors.Length; i++)
{
temp[i] = new ConstructorInfoEx(ctors[i]);
}

// Overwrite if another thread already set; they would contain the same information.
s_constructorInfoEx[instanceType] = temp;

constructors = temp;
steveharter marked this conversation as resolved.
Show resolved Hide resolved
}

ConstructorInfoEx? constructor;
IServiceProviderIsService? serviceProviderIsService = provider.GetService<IServiceProviderIsService>();
// if container supports using IServiceProviderIsService, we try to find the longest ctor that
// (a) matches all parameters given to CreateInstance
Expand All @@ -61,10 +80,11 @@ public static object CreateInstance(
ConstructorMatcher bestMatcher = default;
bool multipleBestLengthFound = false;

foreach (ConstructorInfo? constructor in instanceType.GetConstructors())
for (int i = 0; i < constructors.Length; i++)
{
var matcher = new ConstructorMatcher(constructor);
bool isPreferred = constructor.IsDefined(typeof(ActivatorUtilitiesConstructorAttribute), false);
constructor = constructors[i];
ConstructorMatcher matcher = new(constructor);
bool isPreferred = constructor.IsPreferred;
int length = matcher.Match(parameters, serviceProviderIsService);

if (isPreferred)
Expand Down Expand Up @@ -105,14 +125,36 @@ public static object CreateInstance(
}
}

Type?[] argumentTypes = new Type[parameters.Length];
for (int i = 0; i < argumentTypes.Length; i++)
Type?[] argumentTypes;
if (parameters.Length == 0)
{
argumentTypes[i] = parameters[i]?.GetType();
argumentTypes = Type.EmptyTypes;
}
else
{
argumentTypes = new Type[parameters.Length];
for (int i = 0; i < argumentTypes.Length; i++)
{
argumentTypes[i] = parameters[i]?.GetType();
}
}

FindApplicableConstructor(instanceType, argumentTypes, out ConstructorInfo constructorInfo, out int?[] parameterMap);
var constructorMatcher = new ConstructorMatcher(constructorInfo);

// Find the ConstructorInfoEx from the given constructorInfo.
constructor = null;
foreach (ConstructorInfoEx ctor in constructors)
steveharter marked this conversation as resolved.
Show resolved Hide resolved
{
if (ReferenceEquals(ctor.Info, constructorInfo))
{
constructor = ctor;
break;
}
}

Debug.Assert(constructor != null);

var constructorMatcher = new ConstructorMatcher(constructor);
constructorMatcher.MapParameters(parameterMap, parameters);
return constructorMatcher.CreateInstance(provider);
}
Expand Down Expand Up @@ -551,58 +593,89 @@ private static bool TryCreateParameterMap(ParameterInfo[] constructorParameters,
return true;
}

private static object? GetService(IServiceProvider serviceProvider, ParameterInfo parameterInfo)
private class ConstructorInfoEx
steveharter marked this conversation as resolved.
Show resolved Hide resolved
{
// Handle keyed service
if (TryGetServiceKey(parameterInfo, out object? key))
public readonly ConstructorInfo Info;
public readonly ParameterInfo[] Parameters;
public readonly bool IsPreferred;
private readonly object?[]? _parameterKeys;
#if NET8_0_OR_GREATER
public readonly ConstructorInvoker Invoker;
#endif

public ConstructorInfoEx(ConstructorInfo constructor)
{
if (serviceProvider is IKeyedServiceProvider keyedServiceProvider)
Info = constructor;
Parameters = constructor.GetParameters();
IsPreferred = constructor.IsDefined(typeof(ActivatorUtilitiesConstructorAttribute), false);

#if NET8_0_OR_GREATER
Invoker = ConstructorInvoker.Create(constructor);
#endif

for (int i = 0; i < Parameters.Length; i++)
{
return keyedServiceProvider.GetKeyedService(parameterInfo.ParameterType, key);
FromKeyedServicesAttribute? attr = (FromKeyedServicesAttribute?)
Attribute.GetCustomAttribute(Parameters[i], typeof(FromKeyedServicesAttribute), inherit: false);

if (attr is not null)
{
_parameterKeys ??= new object?[Parameters.Length];
_parameterKeys[i] = attr.Key;
}
}
throw new InvalidOperationException(SR.KeyedServicesNotSupported);
}
// Try non keyed service
return serviceProvider.GetService(parameterInfo.ParameterType);
}

private static bool IsService(IServiceProviderIsService serviceProviderIsService, ParameterInfo parameterInfo)
{
// Handle keyed service
if (TryGetServiceKey(parameterInfo, out object? key))
public bool IsService(IServiceProviderIsService serviceProviderIsService, int parameterIndex)
{
if (serviceProviderIsService is IServiceProviderIsKeyedService serviceProviderIsKeyedService)
ParameterInfo parameterInfo = Parameters[parameterIndex];

// Handle keyed service
object? key = _parameterKeys?[parameterIndex];
if (key is not null)
{
return serviceProviderIsKeyedService.IsKeyedService(parameterInfo.ParameterType, key);
if (serviceProviderIsService is IServiceProviderIsKeyedService serviceProviderIsKeyedService)
{
return serviceProviderIsKeyedService.IsKeyedService(parameterInfo.ParameterType, key);
}

throw new InvalidOperationException(SR.KeyedServicesNotSupported);
}
throw new InvalidOperationException(SR.KeyedServicesNotSupported);

// Use non-keyed service
return serviceProviderIsService.IsService(parameterInfo.ParameterType);
}
// Try non keyed service
return serviceProviderIsService.IsService(parameterInfo.ParameterType);
}

private static bool TryGetServiceKey(ParameterInfo parameterInfo, out object? key)
{
foreach (var attribute in parameterInfo.GetCustomAttributes<FromKeyedServicesAttribute>(false))
public object? GetService(IServiceProvider serviceProvider, int parameterIndex)
{
key = attribute.Key;
return true;
ParameterInfo parameterInfo = Parameters[parameterIndex];

// Handle keyed service
object? key = _parameterKeys?[parameterIndex];
if (key is not null)
{
if (serviceProvider is IKeyedServiceProvider keyedServiceProvider)
{
return keyedServiceProvider.GetKeyedService(parameterInfo.ParameterType, key);
}

throw new InvalidOperationException(SR.KeyedServicesNotSupported);
}

// Use non-keyed service
return serviceProvider.GetService(parameterInfo.ParameterType);
}
key = null;
return false;
}

private readonly struct ConstructorMatcher
{
private readonly ConstructorInfo _constructor;
private readonly ParameterInfo[] _parameters;
private readonly ConstructorInfoEx _constructor;
private readonly object?[] _parameterValues;

public ConstructorMatcher(ConstructorInfo constructor)
public ConstructorMatcher(ConstructorInfoEx constructor)
{
_constructor = constructor;
_parameters = _constructor.GetParameters();
_parameterValues = new object?[_parameters.Length];
_parameterValues = new object[constructor.Parameters.Length];
}

public int Match(object[] givenParameters, IServiceProviderIsService serviceProviderIsService)
Expand All @@ -612,10 +685,10 @@ public int Match(object[] givenParameters, IServiceProviderIsService serviceProv
Type? givenType = givenParameters[givenIndex]?.GetType();
bool givenMatched = false;

for (int applyIndex = 0; applyIndex < _parameters.Length; applyIndex++)
for (int applyIndex = 0; applyIndex < _constructor.Parameters.Length; applyIndex++)
{
if (_parameterValues[applyIndex] == null &&
_parameters[applyIndex].ParameterType.IsAssignableFrom(givenType))
_constructor.Parameters[applyIndex].ParameterType.IsAssignableFrom(givenType))
{
givenMatched = true;
_parameterValues[applyIndex] = givenParameters[givenIndex];
Expand All @@ -630,12 +703,12 @@ public int Match(object[] givenParameters, IServiceProviderIsService serviceProv
}

// confirms the rest of ctor arguments match either as a parameter with a default value or as a service registered
for (int i = 0; i < _parameters.Length; i++)
for (int i = 0; i < _constructor.Parameters.Length; i++)
{
if (_parameterValues[i] == null &&
!IsService(serviceProviderIsService, _parameters[i]))
!_constructor.IsService(serviceProviderIsService, i))
{
if (ParameterDefaultValue.TryGetDefaultValue(_parameters[i], out object? defaultValue))
if (ParameterDefaultValue.TryGetDefaultValue(_constructor.Parameters[i], out object? defaultValue))
{
_parameterValues[i] = defaultValue;
}
Expand All @@ -646,21 +719,21 @@ public int Match(object[] givenParameters, IServiceProviderIsService serviceProv
}
}

return _parameters.Length;
return _constructor.Parameters.Length;
}

public object CreateInstance(IServiceProvider provider)
{
for (int index = 0; index < _parameters.Length; index++)
for (int index = 0; index < _constructor.Parameters.Length; index++)
{
if (_parameterValues[index] == null)
{
object? value = GetService(provider, _parameters[index]);
object? value = _constructor.GetService(provider, index);
if (value == null)
{
if (!ParameterDefaultValue.TryGetDefaultValue(_parameters[index], out object? defaultValue))
if (!ParameterDefaultValue.TryGetDefaultValue(_constructor.Parameters[index], out object? defaultValue))
{
throw new InvalidOperationException(SR.Format(SR.UnableToResolveService, _parameters[index].ParameterType, _constructor.DeclaringType));
throw new InvalidOperationException(SR.Format(SR.UnableToResolveService, _constructor.Parameters[index].ParameterType, _constructor.Info.DeclaringType));
}
else
{
Expand All @@ -677,22 +750,24 @@ public object CreateInstance(IServiceProvider provider)
#if NETFRAMEWORK || NETSTANDARD2_0
try
{
return _constructor.Invoke(_parameterValues);
return _constructor.Info.Invoke(_parameterValues);
}
catch (TargetInvocationException ex) when (ex.InnerException != null)
{
ExceptionDispatchInfo.Capture(ex.InnerException).Throw();
// The above line will always throw, but the compiler requires we throw explicitly.
throw;
}
#elif NET8_0_OR_GREATER
return _constructor.Invoker.Invoke(_parameterValues.AsSpan());
#else
return _constructor.Invoke(BindingFlags.DoNotWrapExceptions, binder: null, parameters: _parameterValues, culture: null);
return _constructor.Info.Invoke(BindingFlags.DoNotWrapExceptions, binder: null, parameters: _parameterValues, culture: null);
#endif
}

public void MapParameters(int?[] parameterMap, object[] givenParameters)
{
for (int i = 0; i < _parameters.Length; i++)
for (int i = 0; i < _constructor.Parameters.Length; i++)
{
if (parameterMap[i] != null)
{
Expand Down
Loading