-
Notifications
You must be signed in to change notification settings - Fork 5k
Improve startup perf by avoiding JIT when invoking well-known signatures #115345
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
Draft
steveharter
wants to merge
6
commits into
dotnet:main
Choose a base branch
from
steveharter:InvokeStartupPerf
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
d9b1877
Improve startup perf by avoiding JIT when invoking well-known signatures
steveharter 152f1c9
Fix issue with byref return; support rare enums
steveharter ca22090
Update unloadable assembly test that was flawed since reflection used…
steveharter db1b503
Add Mono intepreter to ActiveIssue for calling assembly test
steveharter d15dd74
Add Mono intepreter to ActiveIssue for calling assembly test 2
steveharter 6f28121
Add Mono intepreter to ActiveIssue for calling assembly test 3
steveharter 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
This file contains hidden or 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
28 changes: 22 additions & 6 deletions
28
src/coreclr/System.Private.CoreLib/src/System/Reflection/ConstructorInvoker.CoreCLR.cs
This file contains hidden or 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 |
---|---|---|
@@ -1,21 +1,37 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using System.Diagnostics; | ||
using System.Runtime.CompilerServices; | ||
using static System.Reflection.InvokerEmitUtil; | ||
using static System.Reflection.MethodBase; | ||
using static System.RuntimeType; | ||
|
||
namespace System.Reflection | ||
{ | ||
public partial class ConstructorInvoker | ||
{ | ||
private readonly Signature? _signature; | ||
private readonly CreateUninitializedCache? _allocator; | ||
|
||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
private object CreateUninitializedObject() => _allocator!.CreateUninitializedObject(_declaringType); | ||
|
||
internal unsafe ConstructorInvoker(RuntimeConstructorInfo constructor) : this(constructor, constructor.Signature.Arguments) | ||
private bool ShouldAllocate | ||
{ | ||
_signature = constructor.Signature; | ||
_invokeFunc_RefArgs = InterpretedInvoke; | ||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
get => _allocator is not null; | ||
} | ||
private unsafe Delegate CreateInvokeDelegateForInterpreted() | ||
{ | ||
Debug.Assert(MethodInvokerCommon.UseInterpretedPath); | ||
Debug.Assert(_strategy == InvokerStrategy.Ref4 || _strategy == InvokerStrategy.RefMany); | ||
|
||
return (InvokeFunc_RefArgs)InterpretedInvoke; | ||
} | ||
|
||
private unsafe object? InterpretedInvoke(object? obj, IntPtr* args) | ||
private unsafe object? InterpretedInvoke(IntPtr _, object? obj, IntPtr* args) | ||
{ | ||
return RuntimeMethodHandle.InvokeMethod(obj, (void**)args, _signature!, isConstructor: obj is null); | ||
return RuntimeMethodHandle.InvokeMethod(obj, (void**)args, _method.Signature, isConstructor: obj is null); | ||
} | ||
} | ||
} |
This file contains hidden or 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 hidden or 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
52 changes: 34 additions & 18 deletions
52
src/coreclr/System.Private.CoreLib/src/System/Reflection/MethodBaseInvoker.CoreCLR.cs
This file contains hidden or 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 |
---|---|---|
@@ -1,38 +1,54 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using System.Diagnostics; | ||
using System.Reflection.Emit; | ||
using System.Runtime.CompilerServices; | ||
using static System.Reflection.InvokerEmitUtil; | ||
using static System.Reflection.MethodBase; | ||
using static System.RuntimeType; | ||
|
||
namespace System.Reflection | ||
{ | ||
internal partial class MethodBaseInvoker | ||
{ | ||
private readonly Signature? _signature; | ||
private readonly CreateUninitializedCache? _allocator; | ||
|
||
internal unsafe MethodBaseInvoker(RuntimeMethodInfo method) : this(method, method.Signature.Arguments) | ||
{ | ||
_signature = method.Signature; | ||
_invocationFlags = method.ComputeAndUpdateInvocationFlags(); | ||
_invokeFunc_RefArgs = InterpretedInvoke_Method; | ||
} | ||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
private object CreateUninitializedObject() => _allocator!.CreateUninitializedObject(_declaringType!); | ||
|
||
internal unsafe MethodBaseInvoker(RuntimeConstructorInfo constructor) : this(constructor, constructor.Signature.Arguments) | ||
private bool ShouldAllocate | ||
{ | ||
_signature = constructor.Signature; | ||
_invocationFlags = constructor.ComputeAndUpdateInvocationFlags(); | ||
_invokeFunc_RefArgs = InterpretedInvoke_Constructor; | ||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
get => _allocator is not null; | ||
} | ||
|
||
internal unsafe MethodBaseInvoker(DynamicMethod method, Signature signature) : this(method, signature.Arguments) | ||
private unsafe Delegate CreateInvokeDelegateForInterpreted() | ||
{ | ||
_signature = signature; | ||
_invokeFunc_RefArgs = InterpretedInvoke_Method; | ||
Debug.Assert(MethodInvokerCommon.UseInterpretedPath); | ||
Debug.Assert(_strategy == InvokerStrategy.Ref4 || _strategy == InvokerStrategy.RefMany); | ||
|
||
if (_method is RuntimeMethodInfo) | ||
{ | ||
return (InvokeFunc_RefArgs)InterpretedInvoke_Method; | ||
} | ||
|
||
if (_method is RuntimeConstructorInfo) | ||
{ | ||
return (InvokeFunc_RefArgs)InterpretedInvoke_Constructor; | ||
} | ||
|
||
Debug.Assert(_method is DynamicMethod); | ||
return (InvokeFunc_RefArgs)InterpretedInvoke_DynamicMethod; | ||
} | ||
|
||
private unsafe object? InterpretedInvoke_Constructor(object? obj, IntPtr* args) => | ||
RuntimeMethodHandle.InvokeMethod(obj, (void**)args, _signature!, isConstructor: obj is null); | ||
private unsafe object? InterpretedInvoke_Method(IntPtr _, object? obj, IntPtr* args) => | ||
RuntimeMethodHandle.InvokeMethod(obj, (void**)args, ((RuntimeMethodInfo)_method).Signature, isConstructor: false); | ||
|
||
private unsafe object? InterpretedInvoke_Constructor(IntPtr _, object? obj, IntPtr* args) => | ||
RuntimeMethodHandle.InvokeMethod(obj, (void**)args, ((RuntimeConstructorInfo)_method).Signature, isConstructor: obj is null); | ||
|
||
private unsafe object? InterpretedInvoke_Method(object? obj, IntPtr* args) => | ||
RuntimeMethodHandle.InvokeMethod(obj, (void**)args, _signature!, isConstructor: false); | ||
private unsafe object? InterpretedInvoke_DynamicMethod(IntPtr _, object? obj, IntPtr* args) => | ||
RuntimeMethodHandle.InvokeMethod(obj, (void**)args, ((DynamicMethod)_method).Signature, isConstructor: false); | ||
} | ||
} |
Oops, something went wrong.
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.
Why do these need to be marked with
NoInlining
?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.
Without it, there is an AV during inlining.
So either the "dynamic method" that we created based on the intrinsic doesn't support inlining or there is a jit bug.
The exception is in the call to GetModule() here with call stack:
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.
Yes, this looks like a bug in the intrinsic implementation. It would be a good idea to understand it (and fix it). It may have other silent manifestations.
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 take a look and report back.
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 took a better look at this and created issue #115429 to track. I can continue to investigate but thought I would create the issue now to see if someone has ideas or wants to pick it up.
If addressed, then we can either remove the
NoInlining
or replace withAggressiveInlining
.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 am worried that this bug is some kind of memory corruption, and it is still going repro - but less frequently - with the NoInlining workaround.
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.
@steveharter I will try and take a look this week.
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.
Fix is #115639