-
Notifications
You must be signed in to change notification settings - Fork 4.9k
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
Faster ConstructorInfo.Invoke #86855
Conversation
Speeds up reflection-invoking constructors by about 30%. Noticed we spend some time in the `ConstructorInfo` invocation infrastructure for no good reason. * We'd first allocate an instance of the object using a general purpose allocator (that first needs to dissect the `MethodTable` to figure out how to allocate), and then * We'd call into general-purpose Invoke infrastructure that would validate the `this` is valid. Neither of those are necessary - we can figure out the right allocator at the time the method invoker is first accessed, and validating `this` is not necessary because we _just_ allocated the right one.
Tagging subscribers to this area: @dotnet/area-system-reflection Issue DetailsSpeeds up reflection-invoking constructors by about 30%. Noticed we spend some time in the
Neither of those are necessary - we can figure out the right allocator at the time the method invoker is first accessed, and validating Cc @dotnet/ilc-contrib
|
Tagging subscribers to this area: @agocke, @MichalStrehovsky, @jkotas Issue DetailsSpeeds up reflection-invoking constructors by about 30%. Noticed we spend some time in the
Neither of those are necessary - we can figure out the right allocator at the time the method invoker is first accessed, and validating Cc @dotnet/ilc-contrib
|
...eaot/System.Private.CoreLib/src/System/Reflection/Runtime/MethodInfos/CustomMethodInvoker.cs
Show resolved
Hide resolved
...on.Execution/src/Internal/Reflection/Execution/ExecutionEnvironmentImplementation.Runtime.cs
Show resolved
Hide resolved
/azp run runtime-extra-platforms |
Azure Pipelines successfully started running 1 pipeline(s). |
@lambdageek could something like this be done for Mono as well? |
src/coreclr/nativeaot/System.Private.CoreLib/src/System/ActivatorImplementation.cs
Outdated
Show resolved
Hide resolved
src/coreclr/nativeaot/System.Private.CoreLib/src/System/ActivatorImplementation.cs
Outdated
Show resolved
Hide resolved
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.
Thank you!
/azp run runtime-extra-platforms |
Azure Pipelines successfully started running 1 pipeline(s). |
I've seen the use of Note that there is still an allocation of an object (and I assume this is ByDesign, since this is what DI wants to do), so in a GC-challenged case, it might limit the overall gains somewhat. Either way this should be an improvement. |
private static class RawCalliHelper | ||
{ | ||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
public static T Call<T>(delegate*<IntPtr, T> pfn, IntPtr arg) | ||
=> pfn(arg); | ||
} |
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.
FYI @yowl; this will need to be adjusted to use the same calling convention transform we do for Activator.CreateInstance
in the next integration.
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.
FYI @yowl; this will need to be adjusted to use the same calling convention transform we do for
Activator.CreateInstance
in the next integration.
Is the issue that I used IntPtr
instead of MethodTable*
? Otherwise this should be just a "regular" calli. It's in a RawCalliHelper to avoid the fat pointer penalty (we know the target doesn't need a generic context) but otherwise just any other user calli.
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.
No, the issue is the mismatch of calling conventions (equivalently, the assumption that they are the same between managed and native). This is a managed calli
, so it will get the shadow stack argument which the native allocator methods do not have. We solve this downstream via detouring through native code. It could be solved upstream, but not sure there is appetite for that, additionally, I suspect we'll start using custom allocators that have the shadow stack argument sooner or later for performance reasons.
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.
Ah, I see, thanks and sorry for the extra trouble!
Yep, saw this in Stage2 as well. WRT the allocations, one place I noticed is this piece of questionable code: Accessing |
That code is a performance problem for multiple reasons (#83609) |
Speeds up reflection-invoking constructors by about 30%.
Noticed we spend some time in the
ConstructorInfo
invocation infrastructure for no good reason.MethodTable
to figure out how to allocate), and thenthis
is valid.Neither of those are necessary - we can figure out the right allocator at the time the method invoker is first accessed, and validating
this
is not necessary because we just allocated the right one.Cc @dotnet/ilc-contrib