-
Notifications
You must be signed in to change notification settings - Fork 4.9k
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
Unmanaged generic type constraint + generic pointers #4784
Comments
@davidwrighton Do you know if there is any CLR type system work required in the VM for unmanaged T constraint? I don't think so, but I'd like to double check. |
Just curious, can't this be implemented as a library solution if dotnet/corefx#5474 is approved? Then you'd be able to write something like this independently of any compiler support: public unsafe struct Pointer<T>
{
private readonly void* pointer;
public Pointer(void* pointer)
{
this.pointer = pointer;
}
public T Value
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get { return Unsafe.Read<T>(pointer); }
[MethodImpl(MethodImplOptions.AggressiveInlining)]
set { Unsafe.Write(pointer, value); }
}
} |
@jamesqo yes you can. And I have in fact done that, hence the open source project https://github.com/DotNetCross/Memory.Unsafe which I have used to make similar code and generic loops/transforms over unmanaged memory. However, this has issues with performance since the code becomes more complex with more calls, which the JIT can't handle particularly well for value types, see https://github.com/dotnet/coreclr/issues/3539. The JIT does inline the calls but the resulting code still does a lot of unnecessary copies/moves. Of course, if the JIT could handle this there would be less reason for an |
@nietras Interesting findings, I hadn't realized the data would still have to be copied after inlining. What if we made the pointer APIs in question specially recognized by the JIT, so it could elide those copies? |
The JIT should be able to elide those copies without special recognizing the pointer APIs. |
Yes, that would be ideal. Especially, since in the example code given we use the value type |
Seems that the proposal is already implemented and remaining issue are tracked in csharplang repo. IMO this should be closed. |
Agree |
Add support for feature mentioned in http://visualstudio.uservoice.com/forums/121579-visual-studio/suggestions/4716089-unmanaged-generic-type-constraint-generic-pointe and example of usage at http://www.codingmurmur.com/2014/07/vote-for-unmanaged-generic-type.html
Some of the contents of the above links are repeated here for convenience.
E.g. add support for code such as:
Getting support for an unmanaged generic type constraint and generic pointers combined with SIMD operations based on unsafe pointers would be absolutely awesome. If System.Numerics.Vectors had support for creating "vectors" over pointers, this would add to the benefit.
Currently, the lack of unmanaged generic type constraint and generic pointers means that, if you are doing image or graphics processing on different primitive types byte, sbyte, short, ushort, int, uint, long, ulong, float, double etc., then you have to repeat code for each of these - if you care about type safety and performance - leading to massive code bloat and combinatorial explosions where I have seen methods with thousands of overloads (using T4 code generation), which Visual Studios intellisense simply can't handle. Getting the above feature would allow a lot of this code to be boiled down to a single generic method e.g. (a draft example using value type trick to get inlining inside execution loop):
An example of this could be:
Keyword for this should, of course, be discussed in detail and may be different on different languages, so this is probably more an issue for Roslyn and not the CoreCLR/CoreRT as such.
Note how due to the transform having type 'T' as input and type 'TResult' as output we can handle a lot of combinations with this, without repeating the code. The JIT will, of course, have to generate specific code for each actual type usage etc. However, this is exactly what we want. Optimized code for each specific type combination and value type func. Who wouldn't want that ;)
F# already has the unmanaged generic type constraint as can be seen in Constraints (F# https://msdn.microsoft.com/en-us/library/dd233203.aspx), we just need this in C# and generic pointers to unmanaged types.
However, some have said this requires CLR support to be widespread usable across different languages, not sure this is true.
PS: It would additionally be great if the CoreCLR also had the
IFunc<in T, outTResult>
and similar interfaces mimicking delegate definitions for Action/Func (all variants) and with these delegates implementing these interfaces as this would allow direct usage of these together with the option of using the value type inline pattern. Of course, using a delegate could not necessarily be inlined but it would allow for convenience in using this pattern both for delegates and value types.The text was updated successfully, but these errors were encountered: