diff --git a/src/coreclr/nativeaot/System.Private.CoreLib/src/System/InvokeUtils.cs b/src/coreclr/nativeaot/System.Private.CoreLib/src/System/InvokeUtils.cs index 88013f0ad58b0b..f061b43fb4d8c4 100644 --- a/src/coreclr/nativeaot/System.Private.CoreLib/src/System/InvokeUtils.cs +++ b/src/coreclr/nativeaot/System.Private.CoreLib/src/System/InvokeUtils.cs @@ -146,6 +146,8 @@ private static Exception ConvertOrWidenPrimitivesEnumsAndPointersIfPossible(obje Unsafe.SkipInit(out dstObject); + ref byte rawSrcValue = ref RuntimeHelpers.GetRawData(srcObject); + // This is the table of all supported widening conversions: // // Boolean (W = BOOL) @@ -176,13 +178,13 @@ private static Exception ConvertOrWidenPrimitivesEnumsAndPointersIfPossible(obje switch (srcElementType) { case EETypeElementType.Char: - charValue = RuntimeHelpers.FastUnbox(srcObject); + charValue = Unsafe.As(ref rawScrValue); break; case EETypeElementType.Byte: - charValue = (char)RuntimeHelpers.FastUnbox(srcObject); + charValue = (char)rawScrValue; break; case EETypeElementType.UInt16: - charValue = (char)RuntimeHelpers.FastUnbox(srcObject); + charValue = (char)Unsafe.As(ref rawScrValue); break; default: goto Failure; @@ -194,7 +196,7 @@ private static Exception ConvertOrWidenPrimitivesEnumsAndPointersIfPossible(obje switch (srcElementType) { case EETypeElementType.SByte: - sbyteValue = RuntimeHelpers.FastUnbox(srcObject); + sbyteValue = Unsafe.As(ref rawScrValue); break; default: goto Failure; @@ -206,7 +208,7 @@ private static Exception ConvertOrWidenPrimitivesEnumsAndPointersIfPossible(obje switch (srcElementType) { case EETypeElementType.Byte: - byteValue = RuntimeHelpers.FastUnbox(srcObject); + byteValue = rawScrValue; break; default: goto Failure; @@ -218,13 +220,13 @@ private static Exception ConvertOrWidenPrimitivesEnumsAndPointersIfPossible(obje switch (srcElementType) { case EETypeElementType.SByte: - shortValue = (short)RuntimeHelpers.FastUnbox(srcObject); + shortValue = (short)Unsafe.As(ref rawScrValue); break; case EETypeElementType.Byte: - shortValue = (short)RuntimeHelpers.FastUnbox(srcObject); + shortValue = (short)rawScrValue; break; case EETypeElementType.Int16: - shortValue = RuntimeHelpers.FastUnbox(srcObject); + shortValue = Unsafe.As(ref rawScrValue); break; default: goto Failure; @@ -236,13 +238,13 @@ private static Exception ConvertOrWidenPrimitivesEnumsAndPointersIfPossible(obje switch (srcElementType) { case EETypeElementType.Char: - ushortValue = (ushort)RuntimeHelpers.FastUnbox(srcObject); + ushortValue = (ushort)Unsafe.As(ref rawScrValue); break; case EETypeElementType.Byte: - ushortValue = (ushort)RuntimeHelpers.FastUnbox(srcObject); + ushortValue = (ushort)rawScrValue; break; case EETypeElementType.UInt16: - ushortValue = RuntimeHelpers.FastUnbox(srcObject); + ushortValue = Unsafe.As(ref rawScrValue); break; default: goto Failure; @@ -254,22 +256,22 @@ private static Exception ConvertOrWidenPrimitivesEnumsAndPointersIfPossible(obje switch (srcElementType) { case EETypeElementType.Char: - intValue = (int)RuntimeHelpers.FastUnbox(srcObject); + intValue = (int)Unsafe.As(ref rawScrValue); break; case EETypeElementType.SByte: - intValue = (int)RuntimeHelpers.FastUnbox(srcObject); + intValue = (int)Unsafe.As(ref rawScrValue); break; case EETypeElementType.Byte: - intValue = (int)RuntimeHelpers.FastUnbox(srcObject); + intValue = (int)rawScrValue; break; case EETypeElementType.Int16: - intValue = (int)RuntimeHelpers.FastUnbox(srcObject); + intValue = (int)Unsafe.As(ref rawScrValue); break; case EETypeElementType.UInt16: - intValue = (int)RuntimeHelpers.FastUnbox(srcObject); + intValue = (int)Unsafe.As(ref rawScrValue); break; case EETypeElementType.Int32: - intValue = RuntimeHelpers.FastUnbox(srcObject); + intValue = Unsafe.As(ref rawScrValue); break; default: goto Failure; @@ -281,16 +283,16 @@ private static Exception ConvertOrWidenPrimitivesEnumsAndPointersIfPossible(obje switch (srcElementType) { case EETypeElementType.Char: - uintValue = (uint)RuntimeHelpers.FastUnbox(srcObject); + uintValue = (uint)Unsafe.As(ref rawScrValue); break; case EETypeElementType.Byte: - uintValue = (uint)RuntimeHelpers.FastUnbox(srcObject); + uintValue = (uint)rawScrValue; break; case EETypeElementType.UInt16: - uintValue = (uint)RuntimeHelpers.FastUnbox(srcObject); + uintValue = (uint)Unsafe.As(ref rawScrValue); break; case EETypeElementType.UInt32: - uintValue = RuntimeHelpers.FastUnbox(srcObject); + uintValue = Unsafe.As(ref rawScrValue); break; default: goto Failure; @@ -302,28 +304,28 @@ private static Exception ConvertOrWidenPrimitivesEnumsAndPointersIfPossible(obje switch (srcElementType) { case EETypeElementType.Char: - longValue = (long)RuntimeHelpers.FastUnbox(srcObject); + longValue = (long)Unsafe.As(ref rawScrValue); break; case EETypeElementType.SByte: - longValue = (long)RuntimeHelpers.FastUnbox(srcObject); + longValue = (long)Unsafe.As(ref rawScrValue); break; case EETypeElementType.Byte: - longValue = (long)RuntimeHelpers.FastUnbox(srcObject); + longValue = (long)rawScrValue; break; case EETypeElementType.Int16: - longValue = (long)RuntimeHelpers.FastUnbox(srcObject); + longValue = (long)Unsafe.As(ref rawScrValue); break; case EETypeElementType.UInt16: - longValue = (long)RuntimeHelpers.FastUnbox(srcObject); + longValue = (long)Unsafe.As(ref rawScrValue); break; case EETypeElementType.Int32: - longValue = (long)RuntimeHelpers.FastUnbox(srcObject); + longValue = (long)Unsafe.As(ref rawScrValue); break; case EETypeElementType.UInt32: - longValue = (long)RuntimeHelpers.FastUnbox(srcObject); + longValue = (long)Unsafe.As(ref rawScrValue); break; case EETypeElementType.Int64: - longValue = RuntimeHelpers.FastUnbox(srcObject); + longValue = Unsafe.As(ref rawScrValue); break; default: goto Failure; @@ -335,19 +337,19 @@ private static Exception ConvertOrWidenPrimitivesEnumsAndPointersIfPossible(obje switch (srcElementType) { case EETypeElementType.Char: - ulongValue = (ulong)RuntimeHelpers.FastUnbox(srcObject); + ulongValue = (ulong)Unsafe.As(ref rawScrValue); break; case EETypeElementType.Byte: - ulongValue = (ulong)RuntimeHelpers.FastUnbox(srcObject); + ulongValue = (ulong)rawScrValue; break; case EETypeElementType.UInt16: - ulongValue = (ulong)RuntimeHelpers.FastUnbox(srcObject); + ulongValue = (ulong)Unsafe.As(ref rawScrValue); break; case EETypeElementType.UInt32: - ulongValue = (ulong)RuntimeHelpers.FastUnbox(srcObject); + ulongValue = (ulong)Unsafe.As(ref rawScrValue); break; case EETypeElementType.UInt64: - ulongValue = RuntimeHelpers.FastUnbox(srcObject); + ulongValue = Unsafe.As(ref rawScrValue); break; default: goto Failure; @@ -359,34 +361,34 @@ private static Exception ConvertOrWidenPrimitivesEnumsAndPointersIfPossible(obje switch (srcElementType) { case EETypeElementType.Char: - floatValue = (float)RuntimeHelpers.FastUnbox(srcObject); + floatValue = (float)Unsafe.As(ref rawScrValue); break; case EETypeElementType.SByte: - floatValue = (float)RuntimeHelpers.FastUnbox(srcObject); + floatValue = (float)Unsafe.As(ref rawScrValue); break; case EETypeElementType.Byte: - floatValue = (float)RuntimeHelpers.FastUnbox(srcObject); + floatValue = (float)rawScrValue; break; case EETypeElementType.Int16: - floatValue = (float)RuntimeHelpers.FastUnbox(srcObject); + floatValue = (float)Unsafe.As(ref rawScrValue); break; case EETypeElementType.UInt16: - floatValue = (float)RuntimeHelpers.FastUnbox(srcObject); + floatValue = (float)Unsafe.As(ref rawScrValue); break; case EETypeElementType.Int32: - floatValue = (float)RuntimeHelpers.FastUnbox(srcObject); + floatValue = (float)Unsafe.As(ref rawScrValue); break; case EETypeElementType.UInt32: - floatValue = (float)RuntimeHelpers.FastUnbox(srcObject); + floatValue = (float)Unsafe.As(ref rawScrValue); break; case EETypeElementType.Int64: - floatValue = (float)RuntimeHelpers.FastUnbox(srcObject); + floatValue = (float)Unsafe.As(ref rawScrValue); break; case EETypeElementType.UInt64: - floatValue = (float)RuntimeHelpers.FastUnbox(srcObject); + floatValue = (float)Unsafe.As(ref rawScrValue); break; case EETypeElementType.Single: - floatValue = RuntimeHelpers.FastUnbox(srcObject); + floatValue = Unsafe.As(ref rawScrValue); break; default: goto Failure; @@ -398,37 +400,37 @@ private static Exception ConvertOrWidenPrimitivesEnumsAndPointersIfPossible(obje switch (srcElementType) { case EETypeElementType.Char: - doubleValue = (double)RuntimeHelpers.FastUnbox(srcObject); + doubleValue = (double)Unsafe.As(ref rawScrValue); break; case EETypeElementType.SByte: - doubleValue = (double)RuntimeHelpers.FastUnbox(srcObject); + doubleValue = (double)Unsafe.As(ref rawScrValue); break; case EETypeElementType.Byte: - doubleValue = (double)RuntimeHelpers.FastUnbox(srcObject); + doubleValue = (double)rawScrValue; break; case EETypeElementType.Int16: - doubleValue = (double)RuntimeHelpers.FastUnbox(srcObject); + doubleValue = (double)Unsafe.As(ref rawScrValue); break; case EETypeElementType.UInt16: - doubleValue = (double)RuntimeHelpers.FastUnbox(srcObject); + doubleValue = (double)Unsafe.As(ref rawScrValue); break; case EETypeElementType.Int32: - doubleValue = (double)RuntimeHelpers.FastUnbox(srcObject); + doubleValue = (double)Unsafe.As(ref rawScrValue); break; case EETypeElementType.UInt32: - doubleValue = (double)RuntimeHelpers.FastUnbox(srcObject); + doubleValue = (double)Unsafe.As(ref rawScrValue); break; case EETypeElementType.Int64: - doubleValue = (double)RuntimeHelpers.FastUnbox(srcObject); + doubleValue = (double)Unsafe.As(ref rawScrValue); break; case EETypeElementType.UInt64: - doubleValue = (double)RuntimeHelpers.FastUnbox(srcObject); + doubleValue = (double)Unsafe.As(ref rawScrValue); break; case EETypeElementType.Single: - doubleValue = (double)RuntimeHelpers.FastUnbox(srcObject); + doubleValue = (double)Unsafe.As(ref rawScrValue); break; case EETypeElementType.Double: - doubleValue = RuntimeHelpers.FastUnbox(srcObject); + doubleValue = Unsafe.As(ref rawScrValue); break; default: goto Failure; diff --git a/src/coreclr/nativeaot/System.Private.CoreLib/src/System/Runtime/CompilerServices/RuntimeHelpers.NativeAot.cs b/src/coreclr/nativeaot/System.Private.CoreLib/src/System/Runtime/CompilerServices/RuntimeHelpers.NativeAot.cs index 6701848927d42c..a2f5b48b498fb6 100644 --- a/src/coreclr/nativeaot/System.Private.CoreLib/src/System/Runtime/CompilerServices/RuntimeHelpers.NativeAot.cs +++ b/src/coreclr/nativeaot/System.Private.CoreLib/src/System/Runtime/CompilerServices/RuntimeHelpers.NativeAot.cs @@ -206,11 +206,6 @@ internal static bool IsBitwiseEquatable() internal static ref byte GetRawData(this object obj) => ref Unsafe.As(obj).Data; - [MethodImpl(MethodImplOptions.AggressiveInlining)] - internal static T FastUnbox(this object obj) - where T : unmanaged => - Unsafe.As(ref Unsafe.As(obj).Data); - internal static unsafe nuint GetRawObjectDataSize(this object obj) { MethodTable* pMT = GetMethodTable(obj);