-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Optimize generic MethodInfo for Func<TRet>
- Loading branch information
Showing
11 changed files
with
222 additions
and
36 deletions.
There are no files selected for viewing
69 changes: 69 additions & 0 deletions
69
src/Microsoft.ML.Core/Utilities/FuncInstanceMethodInfo1`2.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
// See the LICENSE file in the project root for more information. | ||
|
||
using System; | ||
using System.Linq.Expressions; | ||
using System.Reflection; | ||
using Microsoft.ML.Runtime; | ||
|
||
namespace Microsoft.ML.Internal.Utilities | ||
{ | ||
/// <summary> | ||
/// Represents the <see cref="MethodInfo"/> for a generic function corresponding to <see cref="Func{TResult}"/>, | ||
/// with the following characteristics: | ||
/// | ||
/// <list type="bullet"> | ||
/// <item><description>The method is an instance method on an object of type <typeparamref name="TTarget"/>.</description></item> | ||
/// <item><description>One generic type argument.</description></item> | ||
/// <item><description>A return value of <typeparamref name="TRet"/>.</description></item> | ||
/// </list> | ||
/// </summary> | ||
/// <typeparam name="TTarget">The type of the receiver of the instance method.</typeparam> | ||
/// <typeparam name="TRet">The type of the return value of the method.</typeparam> | ||
internal sealed class FuncInstanceMethodInfo1<TTarget, TRet> : FuncMethodInfo1<TRet> | ||
where TTarget : class | ||
{ | ||
private static readonly string _targetTypeCheckMessage = $"Should have a target type of '{typeof(TTarget)}'"; | ||
|
||
public FuncInstanceMethodInfo1(Func<TRet> function) | ||
: base(function) | ||
{ | ||
Contracts.CheckParam(!GenericMethodDefinition.IsStatic, nameof(function), "Should be an instance method"); | ||
Contracts.CheckParam(GenericMethodDefinition.DeclaringType == typeof(TTarget), nameof(function), _targetTypeCheckMessage); | ||
} | ||
|
||
public static FuncInstanceMethodInfo1<TTarget, TRet> Create(Expression<Func<TTarget, Func<TRet>>> expression) | ||
{ | ||
if (!(expression is { Body: UnaryExpression { Operand: MethodCallExpression methodCallExpression } })) | ||
{ | ||
throw Contracts.ExceptParam(nameof(expression), "Unexpected expression form"); | ||
} | ||
|
||
// Verify that we are calling MethodInfo.CreateDelegate(Type, object) | ||
Contracts.CheckParam(methodCallExpression.Method.DeclaringType == typeof(MethodInfo), nameof(expression), "Unexpected expression form"); | ||
Contracts.CheckParam(methodCallExpression.Method.Name == nameof(MethodInfo.CreateDelegate), nameof(expression), "Unexpected expression form"); | ||
Contracts.CheckParam(methodCallExpression.Method.GetParameters().Length == 2, nameof(expression), "Unexpected expression form"); | ||
Contracts.CheckParam(methodCallExpression.Method.GetParameters()[0].ParameterType == typeof(Type), nameof(expression), "Unexpected expression form"); | ||
Contracts.CheckParam(methodCallExpression.Method.GetParameters()[1].ParameterType == typeof(object), nameof(expression), "Unexpected expression form"); | ||
|
||
// Verify that we are creating a delegate of type Func<TRet> | ||
Contracts.CheckParam(methodCallExpression.Arguments.Count == 2, nameof(expression), "Unexpected expression form"); | ||
Contracts.CheckParam(methodCallExpression.Arguments[0] is ConstantExpression, nameof(expression), "Unexpected expression form"); | ||
Contracts.CheckParam(((ConstantExpression)methodCallExpression.Arguments[0]).Type == typeof(Type), nameof(expression), "Unexpected expression form"); | ||
Contracts.CheckParam((Type)((ConstantExpression)methodCallExpression.Arguments[0]).Value == typeof(Func<TRet>), nameof(expression), "Unexpected expression form"); | ||
Contracts.CheckParam(methodCallExpression.Arguments[1] is ParameterExpression, nameof(expression), "Unexpected expression form"); | ||
Contracts.CheckParam(methodCallExpression.Arguments[1] == expression.Parameters[0], nameof(expression), "Unexpected expression form"); | ||
|
||
// Check the MethodInfo | ||
Contracts.CheckParam(methodCallExpression.Object is ConstantExpression, nameof(expression), "Unexpected expression form"); | ||
Contracts.CheckParam(((ConstantExpression)methodCallExpression.Object).Type == typeof(MethodInfo), nameof(expression), "Unexpected expression form"); | ||
|
||
var methodInfo = (MethodInfo)((ConstantExpression)methodCallExpression.Object).Value; | ||
Contracts.CheckParam(expression.Body is UnaryExpression, nameof(expression), "Unexpected expression form"); | ||
Contracts.CheckParam(((UnaryExpression)expression.Body).Operand is MethodCallExpression, nameof(expression), "Unexpected expression form"); | ||
|
||
return new FuncInstanceMethodInfo1<TTarget, TRet>((Func<TRet>)Delegate.CreateDelegate(typeof(Func<TRet>), null, methodInfo)); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
// See the LICENSE file in the project root for more information. | ||
|
||
using System; | ||
using System.Collections.Immutable; | ||
using System.Reflection; | ||
using Microsoft.ML.Runtime; | ||
|
||
namespace Microsoft.ML.Internal.Utilities | ||
{ | ||
/// <summary> | ||
/// Represents the <see cref="MethodInfo"/> for a generic function corresponding to <see cref="Func{TResult}"/>, | ||
/// with the following characteristics: | ||
/// | ||
/// <list type="bullet"> | ||
/// <item><description>One generic type argument.</description></item> | ||
/// <item><description>A return value of <typeparamref name="TRet"/>.</description></item> | ||
/// </list> | ||
/// </summary> | ||
/// <typeparam name="TRet">The type of the return value of the method.</typeparam> | ||
internal abstract class FuncMethodInfo1<TRet> : FuncMethodInfo<TRet> | ||
{ | ||
private ImmutableDictionary<Type, MethodInfo> _instanceMethodInfo | ||
= ImmutableDictionary<Type, MethodInfo>.Empty; | ||
|
||
private protected FuncMethodInfo1(Func<TRet> function) | ||
: base(function) | ||
{ | ||
Contracts.CheckParam(GenericMethodDefinition.GetGenericArguments().Length == 1, nameof(function), | ||
"Should have exactly one generic type parameter but does not"); | ||
} | ||
|
||
public MethodInfo MakeGenericMethod(Type typeArg1) | ||
{ | ||
return ImmutableInterlocked.GetOrAdd( | ||
ref _instanceMethodInfo, | ||
typeArg1, | ||
(typeArg, methodInfo) => methodInfo.MakeGenericMethod(typeArg), | ||
GenericMethodDefinition); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
// See the LICENSE file in the project root for more information. | ||
|
||
using System; | ||
using System.Reflection; | ||
using Microsoft.ML.Runtime; | ||
|
||
namespace Microsoft.ML.Internal.Utilities | ||
{ | ||
internal abstract class FuncMethodInfo<TRet> | ||
{ | ||
private protected FuncMethodInfo(Func<TRet> function) | ||
{ | ||
Contracts.CheckValue(function, nameof(function)); | ||
|
||
MethodInfo methodInfo = function.GetMethodInfo(); | ||
Contracts.CheckParam(methodInfo.IsGenericMethod, nameof(function), "Should be generic but is not"); | ||
|
||
GenericMethodDefinition = methodInfo.GetGenericMethodDefinition(); | ||
Contracts.CheckParam(GenericMethodDefinition.ReturnType == typeof(TRet), nameof(function), "Cannot be generic on return type"); | ||
} | ||
|
||
protected MethodInfo GenericMethodDefinition { get; } | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
src/Microsoft.ML.Core/Utilities/FuncStaticMethodInfo1`1.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
// See the LICENSE file in the project root for more information. | ||
|
||
using System; | ||
using System.Reflection; | ||
using Microsoft.ML.Runtime; | ||
|
||
namespace Microsoft.ML.Internal.Utilities | ||
{ | ||
/// <summary> | ||
/// Represents the <see cref="MethodInfo"/> for a generic function corresponding to <see cref="Func{TResult}"/>, | ||
/// with the following characteristics: | ||
/// | ||
/// <list type="bullet"> | ||
/// <item><description>The method is static.</description></item> | ||
/// <item><description>One generic type argument.</description></item> | ||
/// <item><description>A return value of <typeparamref name="TRet"/>.</description></item> | ||
/// </list> | ||
/// </summary> | ||
/// <typeparam name="TRet">The type of the return value of the method.</typeparam> | ||
internal sealed class FuncStaticMethodInfo1<TRet> : FuncMethodInfo1<TRet> | ||
{ | ||
public FuncStaticMethodInfo1(Func<TRet> function) | ||
: base(function) | ||
{ | ||
Contracts.CheckParam(GenericMethodDefinition.IsStatic, nameof(function), "Should be a static method"); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.