You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I just ran into this exact thing while playing with C# 9.
Whether you use nint or IntPtr, the compiler correctly compiles both to the VES supported native int.
But when using nint the C# compiler uses the operations supported directly by the VES like the add and sub instructions.
But when using IntPtr, it calls op_Addition and op_Implicit on IntPtr.
This causes less efficient instructions by the JIT compiler.
privateclassTest{publicintX;[MethodImpl(MethodImplOptions.NoInlining)]publicvirtualvoidInitX(){this.X =1337;}}[MethodImpl(MethodImplOptions.NoInlining | MethodImplOptions.AggressiveOptimization)]privateunsafestaticvoidFooBar(Testtest){nintaddress= GetAddressFast(test);// &testninttestAddress= address;address=*(nint*)address;address=*(nint*)(address+0x40);// Virtual function table.address=*(nint*)(address+0x20);// Virtual function address.delegate*<nint,void>func=(delegate*<nint,void>)address;
func(testAddress);// Call test.InitX()}
The JIT compiler is able to inline the op_Addition operator call, but call void* [System.Runtime]System.IntPtr::op_Explicit(native int) ruins the optimization.
IntPtr x = 0; also fails, while nint x = 0; doesn't, as well as the other operations.
Keywords also cannot be aliased, so there's no way to alias IntPtr, and use the native instructions.
Like int and System.Int32 both are aliases of the VES supported type int32 and behave the exact same way, so should nint and IntPtr (and nuint/UIntPtr).
Version Used:
Version 16.9.0 Preview 1.0 [30614.203.main]
Steps to Reproduce:
While debugging update:
to
Expected Behavior:
The edit should be allowed since the underlying representation of the types is the same.
Actual Behavior:
Rude edit is reported.
Also validate that symbol matcher considers these types the same (it should as they are the same symbols, but having tests would be good).
The text was updated successfully, but these errors were encountered: