-
Notifications
You must be signed in to change notification settings - Fork 4.8k
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
Efficient way to check if a method has been overridden #111083
Comments
Tagging subscribers to this area: @dotnet/area-system-runtime |
Such a feature was discussed in #12760. |
That's not correct. Function pointers are not guaranteed to be stable across all runtime flavors. This only happens to work for runtime flavors where function pointers are stable (e.g. native AOT). |
Would #94975 solve this with |
It should be functional with stable, comparable handles, but may not be as performant as accessing virtual slots directly. Currently in coreclr, function pointer comparison is used as a fast path, then MethodDesc. Further optimizations should be nice to have. |
Sometimes, it's useful to check overridden status of methods to enable specific optimization. For example,
Stream
checks override status of APM methods to skip an extra layer:runtime/src/libraries/System.Private.CoreLib/src/System/IO/Stream.cs
Lines 754 to 761 in 6045e29
A virtual-method based UI framework may also use the pattern to reduce overhead of event handling:
Currently there is no simple way to check if a method is overridden in C#. Using reflection is complicated and not intuitive, with
baseMethod.DeclaringType != derivedMethod.DeclaringType && baseMethod.GetBaseDefinition() == derivedMethod.GetBaseDefinition()
. It's also very inefficient.Stream
is specially handled by CLR, and the approach is not extensible at all. In IL, it's achievable by comparing method pointers withldvirtftn
:runtime/src/coreclr/tools/Common/TypeSystem/IL/Stubs/StreamIntrinsics.cs
Lines 32 to 36 in 0f6c3d8
Should we introduce an approach available externally to detect overrides? There are two possible approaches I can see:
Enable
ldvirtftn
and instance function pointers in C#Basically it's allowing to write the aforementioned IL in C#. There's nothing more to handle for codegen and will just work. However, there may need more effort to ensure the usage of instance function pointers work fine for broad cases.
Expose a intrinsic helper for checking overrides
This enables more opportunities like constant folding when the type is known, and reduces the risk of inappropriate function pointers. However, currently there's no ideal way to pass a method in C# (lack of
methodof
). The ability to express the method is questionable.Is there any interest to provide this functionality at all? Would there be more risks about it?
The text was updated successfully, but these errors were encountered: