-
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
[API Proposal]: RuntimeFieldHandle.Offset
#94976
Comments
Tagging subscribers to this area: @dotnet/area-system-runtime Issue DetailsBackground and motivationIn some interop or unsafe manipulation scenarios being able to obtain field offset of a certain field without having to do ref arithmetics. The API would return a sentinel value of Together with #94975 this can supersede #93946. API Proposalnamespace System;
public struct RuntimeFieldHandle
{
public int Offset { get; }
} API Usagestruct S
{
public int a;
public float b;
public byte c;
public double d;
}
Console.WriteLine(typeof(S).GetField("d").FieldHandle.Offset); // prints 16 Alternative DesignsRisksThis could make invalid code based on Offsets more common, same as #93946. This could confuse the runtimes and introduce unexpected crashes.
|
The runtime could fold accesses to Offset with ldtoken/proposed UnsafeAccessor so it could become "free reflection" like things like
I have three reasons why I think
|
This would only get expanded if the JIT feels like it. It would probably not be expanded in Tier-0 for example. For Native AOT, the whole program analysis that we do before codegen cannot assume any particular codegen optimization so the whole program view will have "this is reflecting over a field". It's hard to clean that up afterwards both in terms of the field being visible from reflection, and in terms of the reflection infrastructure code to access fields. |
Would #83021 solve this issue? |
@MichalStrehovsky |
@yushroom would it be possible to add links to Unity docs about this? I want to understand why you need managed offsets, as opposed to the native offsets (available using the existing |
I'm trying to implement It seems impossible to do on NAOT currently. On normal runtime I can get the size of a type with ILGen calls to I tried other ideas first (since I recall running into this issue before) of just boxing it and using reflection to set stuff and reading out all the parts which were managed (by only writing to either managed or unmanaged, and then comparing to 0), but the issues I faced here were: can't do it on inline arrays at all, padding (automatic or additional) cannot be written to with reflection so I cannot detect where it is, I cannot construct an object of any general type (therefore I cannot set it to the fields), and The IL instructions, for which there are no built-in APIs, also don't work properly in a number of cases: #91530, but I feel like these APIs would be useful even if I didn't have the issue with implementing Volatile APIs that should work. |
I do not see how these APIs can help you to do this efficiently and reliably with native AOT. If you are running into issues with |
Basically, I can implement them like this: Write:
Read:
It's not perfect (it definitely could be faster - since the JIT doesn't combine all of the ops perfectly), but it's not too bad either. The problem on NativeAOT is that there is no way to determine for managed types whether those first & last
I'm happy for that to be done :) I just don't know how to do it - I would rather just use those, but for now that's not really an option. |
Background and motivation
In some interop or unsafe manipulation scenarios being able to obtain field offset of a certain field without having to do ref arithmetics.
As such, exposing such offset on
RuntimeFieldHandle
seems like it would allow programmers to access it in a cheap way without writing code that can break depending on JIT optimizations.The API would return a sentinel value of
-1
for static fields and fields added by EnC to avoid exceptions in high performance code.Together with #94975 this can supersede #93946.
API Proposal
API Usage
Alternative Designs
#93946
Risks
This could make invalid code based on Offsets more common, same as #93946. This could confuse the runtimes and introduce unexpected crashes.
The text was updated successfully, but these errors were encountered: