-
Notifications
You must be signed in to change notification settings - Fork 1.9k
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
Optimize generic MethodInfo for Func<TResult> #4588
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
89 changes: 89 additions & 0 deletions
89
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,89 @@ | ||
// 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. | ||
|
||
#nullable enable | ||
|
||
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="TResult"/>.</description></item> | ||
/// </list> | ||
/// </summary> | ||
/// <typeparam name="TTarget">The type of the receiver of the instance method.</typeparam> | ||
/// <typeparam name="TResult">The type of the return value of the method.</typeparam> | ||
internal sealed class FuncInstanceMethodInfo1<TTarget, TResult> : FuncMethodInfo1<TResult> | ||
where TTarget : class | ||
{ | ||
private static readonly string _targetTypeCheckMessage = $"Should have a target type of '{typeof(TTarget)}'"; | ||
|
||
public FuncInstanceMethodInfo1(Func<TResult> function) | ||
: this(function.Method) | ||
{ | ||
} | ||
|
||
private FuncInstanceMethodInfo1(MethodInfo methodInfo) | ||
: base(methodInfo) | ||
{ | ||
Contracts.CheckParam(!GenericMethodDefinition.IsStatic, nameof(methodInfo), "Should be an instance method"); | ||
Contracts.CheckParam(GenericMethodDefinition.DeclaringType == typeof(TTarget), nameof(methodInfo), _targetTypeCheckMessage); | ||
} | ||
|
||
/// <summary> | ||
/// Creates a <see cref="FuncInstanceMethodInfo1{TTarget, TResult}"/> representing the <see cref="MethodInfo"/> for | ||
/// a generic instance method. This helper method allows the instance to be created prior to the creation of any | ||
/// instances of the target type. The following example shows the creation of an instance representing the | ||
/// <see cref="object.GetHashCode"/> method: | ||
/// | ||
/// <code> | ||
/// FuncInstanceMethodInfo1<object, int>.Create(obj => obj.GetHashCode) | ||
/// </code> | ||
/// </summary> | ||
/// <param name="expression">The expression which creates the delegate for an instance of the target type.</param> | ||
/// <returns>A <see cref="FuncInstanceMethodInfo1{TTarget, TResult}"/> representing the <see cref="MethodInfo"/> | ||
/// for the generic instance method.</returns> | ||
public static FuncInstanceMethodInfo1<TTarget, TResult> Create(Expression<Func<TTarget, Func<TResult>>> 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<TResult>), 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, TResult>(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,46 @@ | ||
// 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. | ||
|
||
#nullable enable | ||
|
||
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="TResult"/>.</description></item> | ||
/// </list> | ||
/// </summary> | ||
/// <typeparam name="TResult">The type of the return value of the method.</typeparam> | ||
internal abstract class FuncMethodInfo1<TResult> : FuncMethodInfo<TResult> | ||
{ | ||
private ImmutableDictionary<Type, MethodInfo> _instanceMethodInfo; | ||
|
||
private protected FuncMethodInfo1(MethodInfo methodInfo) | ||
: base(methodInfo) | ||
{ | ||
_instanceMethodInfo = ImmutableDictionary<Type, MethodInfo>.Empty; | ||
|
||
Contracts.CheckParam(GenericMethodDefinition.GetGenericArguments().Length == 1, nameof(methodInfo), | ||
"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,25 @@ | ||
// 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. | ||
|
||
#nullable enable | ||
|
||
using System.Reflection; | ||
using Microsoft.ML.Runtime; | ||
|
||
namespace Microsoft.ML.Internal.Utilities | ||
{ | ||
internal abstract class FuncMethodInfo<TResult> | ||
{ | ||
private protected FuncMethodInfo(MethodInfo methodInfo) | ||
{ | ||
Contracts.CheckValue(methodInfo, nameof(methodInfo)); | ||
Contracts.CheckParam(methodInfo.IsGenericMethod, nameof(methodInfo), "Should be generic but is not"); | ||
|
||
GenericMethodDefinition = methodInfo.GetGenericMethodDefinition(); | ||
Contracts.CheckParam(typeof(TResult).IsAssignableFrom(GenericMethodDefinition.ReturnType), nameof(methodInfo), "Cannot be generic on return type"); | ||
} | ||
|
||
protected MethodInfo GenericMethodDefinition { get; } | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
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,32 @@ | ||
// 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. | ||
|
||
#nullable enable | ||
|
||
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="TResult"/>.</description></item> | ||
/// </list> | ||
/// </summary> | ||
/// <typeparam name="TResult">The type of the return value of the method.</typeparam> | ||
internal sealed class FuncStaticMethodInfo1<TResult> : FuncMethodInfo1<TResult> | ||
{ | ||
public FuncStaticMethodInfo1(Func<TResult> function) | ||
: base(function.Method) | ||
{ | ||
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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Any idea why the file names of the newly added files in this directory have an backtick-1 or backtick-2 suffix? Is that an accident or is there a specific reason behind this naming? The change looks good and I am approving it but it would be good if the naming can be fixed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The file names match the names of the types defined in the files. When generic types are compiled, the compiler adds a
`{arity}
suffix to the type name.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What does the
{arity}
represent here?In reply to: 386548523 [](ancestors = 386548523)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The number of generic type parameters.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm using the term from here:
https://github.com/dotnet/roslyn/blob/6389e2519f282f7683025761189894e1c894936c/src/Compilers/Core/Portable/Symbols/INamedTypeSymbol.cs#L23-L27
I wanted to link to the language specification but it seems the specification doesn't explicitly give a name to this characteristic of generic types.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the additional info. Please feel free to merge.
In reply to: 386566851 [](ancestors = 386566851)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll run a build locally to make sure nothing changed in the interim and merge if it passes.