-
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
Make TypedReference useful #26186
Comments
Related to https://github.com/dotnet/corefx/issues/14021 cc @atsushikan |
I assume this is meant to be a nullary method. |
Yes, fixed. Thanks for spotting! |
@steveharter can you look into this enhancement with https://github.com/dotnet/corefx/issues/29736 post-JSON? |
From my point of view, this type can be used as dynamically strongly typed reference to the location in managed memory. It is somewhat trackable version of According with this concept it would be great to see refactored and modernized version of this type with the following characteristics:
At first, its two private readonly RuntimeTypeHandle _type;
private readonly ByReference<byte> _reference; Now we have regular by-ref like value type without special handling from CLR side. At second, we need new methods to make this type useful: public static TypedReference Make<T>(ref T value); Allows to created typed reference from the managed pointer. Information about type public static TypedReference Make<T>(in T obj, RuntimeFieldHandle field);
public static TypedReference Make(TypedReference obj, RuntimeFieldHandle field); These methods allow to obtain typed reference to the specified field. This is what proposed by @MichalStrehovsky about GetRawData. You don't need to compute offset to the field used low-level magic and API is not low-level as mentioned by @jkotas. And still we have dynamically typed access to the field. public bool IsTypeOf<T>(); Intrinsic method that allows to check whether the typed reference is of type public ref T AsRef<T>(); As proposed by Michal previously. However, this method can be dangerous because may refer to public ref readonly byte RawData { get; } Allows to obtain raw managed pointer to the referenced value. Can be useful for low-level operations such as pointer arithmetic or situations where underlying type of the referenced value irrelevant. public int TypeSize { get; } Returns the size of the underlying type, in bytes. The behavior is equal to public TypedReference Upcast<T>() where T : class;
public TypedReference Upcast(Type type); Allows to change the underlying type to subclass. It has the following behavior:
public bool IsEmpty { get; } If typed reference is empty (initialized as public void CopyTo(TypedReference destination);
public bool TryCopyTo(TypedReference destination); Copies the value stored at the reference to the memory referenced by another Some of these methods can be implemented in pure IL without special treatment from CLR side.
public static TypedReference AddOffset(TypedReference obj, int offset); Returns typed reference to the specified offset in the memory. Can be used for fast access to the array elements without knowledge about its element type. public static TypedReference Unbox(object obj); Returns typed reference to the boxed value; or empty, if Of course, all these additions stay backward compatible and existing methods should not be removed. There is one drawback: at this moment it is possible to declare unmanaged pointer to |
@sakno I'd be curious to hear what use cases you have in mind? I do not dispute that this type might be useful. But I fail to imagine concrete use cases. |
At least GetRawData and GetRawArrayData. Generally, proposed methods can be used as fast alternative to Reflection-based member access:
As I pointed in my previous post, JVM has similar concept of fast access to the field or array element using VarHandle |
Currently there is active design work in 6.0 around fast invoke, which may use @MichalStrehovsky are you aware of any reasons why the Roslyn compiler needs to continue to prevent Also I currently don't see any active issues around TypedReference in Roslyn, so this is an early attempt to gather information before pursuing. |
@steveharter https://github.com/dotnet/csharplang/blob/master/proposals/low-level-struct-improvements.md talks about the problem and the proposed solution. |
Rationale
TypedReference
is currently a second class citizen because using it equates to using undocumented C# keywords. We could make it more useful by adding methods to it that let normal code work with it.We would be adding APIs that replace (and enhance) undocumented C# keyword
__refvalue
and__makref
.Proposed API
Stretch goal:
The text was updated successfully, but these errors were encountered: