Skip to content

Commit

Permalink
Remove 'FastUnbox', use 'Unsafe.As' directly
Browse files Browse the repository at this point in the history
  • Loading branch information
Sergio0694 committed May 4, 2024
1 parent 2c02de7 commit ff4abbc
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 60 deletions.
112 changes: 57 additions & 55 deletions src/coreclr/nativeaot/System.Private.CoreLib/src/System/InvokeUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -176,13 +178,13 @@ private static Exception ConvertOrWidenPrimitivesEnumsAndPointersIfPossible(obje
switch (srcElementType)
{
case EETypeElementType.Char:
charValue = RuntimeHelpers.FastUnbox<char>(srcObject);
charValue = Unsafe.As<byte, char>(ref rawSrcValue);
break;
case EETypeElementType.Byte:
charValue = (char)RuntimeHelpers.FastUnbox<byte>(srcObject);
charValue = (char)rawSrcValue;
break;
case EETypeElementType.UInt16:
charValue = (char)RuntimeHelpers.FastUnbox<ushort>(srcObject);
charValue = (char)Unsafe.As<byte, ushort>(ref rawSrcValue);
break;
default:
goto Failure;
Expand All @@ -194,7 +196,7 @@ private static Exception ConvertOrWidenPrimitivesEnumsAndPointersIfPossible(obje
switch (srcElementType)
{
case EETypeElementType.SByte:
sbyteValue = RuntimeHelpers.FastUnbox<sbyte>(srcObject);
sbyteValue = Unsafe.As<byte, sbyte>(ref rawSrcValue);
break;
default:
goto Failure;
Expand All @@ -206,7 +208,7 @@ private static Exception ConvertOrWidenPrimitivesEnumsAndPointersIfPossible(obje
switch (srcElementType)
{
case EETypeElementType.Byte:
byteValue = RuntimeHelpers.FastUnbox<byte>(srcObject);
byteValue = rawSrcValue;
break;
default:
goto Failure;
Expand All @@ -218,13 +220,13 @@ private static Exception ConvertOrWidenPrimitivesEnumsAndPointersIfPossible(obje
switch (srcElementType)
{
case EETypeElementType.SByte:
shortValue = (short)RuntimeHelpers.FastUnbox<sbyte>(srcObject);
shortValue = (short)Unsafe.As<byte, sbyte>(ref rawSrcValue);
break;
case EETypeElementType.Byte:
shortValue = (short)RuntimeHelpers.FastUnbox<byte>(srcObject);
shortValue = (short)rawSrcValue;
break;
case EETypeElementType.Int16:
shortValue = RuntimeHelpers.FastUnbox<short>(srcObject);
shortValue = Unsafe.As<byte, short>(ref rawSrcValue);
break;
default:
goto Failure;
Expand All @@ -236,13 +238,13 @@ private static Exception ConvertOrWidenPrimitivesEnumsAndPointersIfPossible(obje
switch (srcElementType)
{
case EETypeElementType.Char:
ushortValue = (ushort)RuntimeHelpers.FastUnbox<char>(srcObject);
ushortValue = (ushort)Unsafe.As<byte, char>(ref rawSrcValue);
break;
case EETypeElementType.Byte:
ushortValue = (ushort)RuntimeHelpers.FastUnbox<byte>(srcObject);
ushortValue = (ushort)rawSrcValue;
break;
case EETypeElementType.UInt16:
ushortValue = RuntimeHelpers.FastUnbox<ushort>(srcObject);
ushortValue = Unsafe.As<byte, ushort>(ref rawSrcValue);
break;
default:
goto Failure;
Expand All @@ -254,22 +256,22 @@ private static Exception ConvertOrWidenPrimitivesEnumsAndPointersIfPossible(obje
switch (srcElementType)
{
case EETypeElementType.Char:
intValue = (int)RuntimeHelpers.FastUnbox<char>(srcObject);
intValue = (int)Unsafe.As<byte, char>(ref rawSrcValue);
break;
case EETypeElementType.SByte:
intValue = (int)RuntimeHelpers.FastUnbox<sbyte>(srcObject);
intValue = (int)Unsafe.As<byte, sbyte>(ref rawSrcValue);
break;
case EETypeElementType.Byte:
intValue = (int)RuntimeHelpers.FastUnbox<byte>(srcObject);
intValue = (int)rawSrcValue;
break;
case EETypeElementType.Int16:
intValue = (int)RuntimeHelpers.FastUnbox<short>(srcObject);
intValue = (int)Unsafe.As<byte, short>(ref rawSrcValue);
break;
case EETypeElementType.UInt16:
intValue = (int)RuntimeHelpers.FastUnbox<ushort>(srcObject);
intValue = (int)Unsafe.As<byte, ushort>(ref rawSrcValue);
break;
case EETypeElementType.Int32:
intValue = RuntimeHelpers.FastUnbox<int>(srcObject);
intValue = Unsafe.As<byte, int>(ref rawSrcValue);
break;
default:
goto Failure;
Expand All @@ -281,16 +283,16 @@ private static Exception ConvertOrWidenPrimitivesEnumsAndPointersIfPossible(obje
switch (srcElementType)
{
case EETypeElementType.Char:
uintValue = (uint)RuntimeHelpers.FastUnbox<char>(srcObject);
uintValue = (uint)Unsafe.As<byte, char>(ref rawSrcValue);
break;
case EETypeElementType.Byte:
uintValue = (uint)RuntimeHelpers.FastUnbox<byte>(srcObject);
uintValue = (uint)rawSrcValue;
break;
case EETypeElementType.UInt16:
uintValue = (uint)RuntimeHelpers.FastUnbox<ushort>(srcObject);
uintValue = (uint)Unsafe.As<byte, ushort>(ref rawSrcValue);
break;
case EETypeElementType.UInt32:
uintValue = RuntimeHelpers.FastUnbox<uint>(srcObject);
uintValue = Unsafe.As<byte, uint>(ref rawSrcValue);
break;
default:
goto Failure;
Expand All @@ -302,28 +304,28 @@ private static Exception ConvertOrWidenPrimitivesEnumsAndPointersIfPossible(obje
switch (srcElementType)
{
case EETypeElementType.Char:
longValue = (long)RuntimeHelpers.FastUnbox<char>(srcObject);
longValue = (long)Unsafe.As<byte, char>(ref rawSrcValue);
break;
case EETypeElementType.SByte:
longValue = (long)RuntimeHelpers.FastUnbox<sbyte>(srcObject);
longValue = (long)Unsafe.As<byte, sbyte>(ref rawSrcValue);
break;
case EETypeElementType.Byte:
longValue = (long)RuntimeHelpers.FastUnbox<byte>(srcObject);
longValue = (long)rawSrcValue;
break;
case EETypeElementType.Int16:
longValue = (long)RuntimeHelpers.FastUnbox<short>(srcObject);
longValue = (long)Unsafe.As<byte, short>(ref rawSrcValue);
break;
case EETypeElementType.UInt16:
longValue = (long)RuntimeHelpers.FastUnbox<ushort>(srcObject);
longValue = (long)Unsafe.As<byte, ushort>(ref rawSrcValue);
break;
case EETypeElementType.Int32:
longValue = (long)RuntimeHelpers.FastUnbox<int>(srcObject);
longValue = (long)Unsafe.As<byte, int>(ref rawSrcValue);
break;
case EETypeElementType.UInt32:
longValue = (long)RuntimeHelpers.FastUnbox<uint>(srcObject);
longValue = (long)Unsafe.As<byte, uint>(ref rawSrcValue);
break;
case EETypeElementType.Int64:
longValue = RuntimeHelpers.FastUnbox<long>(srcObject);
longValue = Unsafe.As<byte, long>(ref rawSrcValue);
break;
default:
goto Failure;
Expand All @@ -335,19 +337,19 @@ private static Exception ConvertOrWidenPrimitivesEnumsAndPointersIfPossible(obje
switch (srcElementType)
{
case EETypeElementType.Char:
ulongValue = (ulong)RuntimeHelpers.FastUnbox<char>(srcObject);
ulongValue = (ulong)Unsafe.As<byte, char>(ref rawSrcValue);
break;
case EETypeElementType.Byte:
ulongValue = (ulong)RuntimeHelpers.FastUnbox<byte>(srcObject);
ulongValue = (ulong)rawSrcValue;
break;
case EETypeElementType.UInt16:
ulongValue = (ulong)RuntimeHelpers.FastUnbox<ushort>(srcObject);
ulongValue = (ulong)Unsafe.As<byte, ushort>(ref rawSrcValue);
break;
case EETypeElementType.UInt32:
ulongValue = (ulong)RuntimeHelpers.FastUnbox<uint>(srcObject);
ulongValue = (ulong)Unsafe.As<byte, uint>(ref rawSrcValue);
break;
case EETypeElementType.UInt64:
ulongValue = RuntimeHelpers.FastUnbox<ulong>(srcObject);
ulongValue = Unsafe.As<byte, ulong>(ref rawSrcValue);
break;
default:
goto Failure;
Expand All @@ -359,34 +361,34 @@ private static Exception ConvertOrWidenPrimitivesEnumsAndPointersIfPossible(obje
switch (srcElementType)
{
case EETypeElementType.Char:
floatValue = (float)RuntimeHelpers.FastUnbox<char>(srcObject);
floatValue = (float)Unsafe.As<byte, char>(ref rawSrcValue);
break;
case EETypeElementType.SByte:
floatValue = (float)RuntimeHelpers.FastUnbox<sbyte>(srcObject);
floatValue = (float)Unsafe.As<byte, sbyte>(ref rawSrcValue);
break;
case EETypeElementType.Byte:
floatValue = (float)RuntimeHelpers.FastUnbox<byte>(srcObject);
floatValue = (float)rawSrcValue;
break;
case EETypeElementType.Int16:
floatValue = (float)RuntimeHelpers.FastUnbox<short>(srcObject);
floatValue = (float)Unsafe.As<byte, short>(ref rawSrcValue);
break;
case EETypeElementType.UInt16:
floatValue = (float)RuntimeHelpers.FastUnbox<ushort>(srcObject);
floatValue = (float)Unsafe.As<byte, ushort>(ref rawSrcValue);
break;
case EETypeElementType.Int32:
floatValue = (float)RuntimeHelpers.FastUnbox<int>(srcObject);
floatValue = (float)Unsafe.As<byte, int>(ref rawSrcValue);
break;
case EETypeElementType.UInt32:
floatValue = (float)RuntimeHelpers.FastUnbox<uint>(srcObject);
floatValue = (float)Unsafe.As<byte, uint>(ref rawSrcValue);
break;
case EETypeElementType.Int64:
floatValue = (float)RuntimeHelpers.FastUnbox<long>(srcObject);
floatValue = (float)Unsafe.As<byte, long>(ref rawSrcValue);
break;
case EETypeElementType.UInt64:
floatValue = (float)RuntimeHelpers.FastUnbox<ulong>(srcObject);
floatValue = (float)Unsafe.As<byte, ulong>(ref rawSrcValue);
break;
case EETypeElementType.Single:
floatValue = RuntimeHelpers.FastUnbox<float>(srcObject);
floatValue = Unsafe.As<byte, float>(ref rawSrcValue);
break;
default:
goto Failure;
Expand All @@ -398,37 +400,37 @@ private static Exception ConvertOrWidenPrimitivesEnumsAndPointersIfPossible(obje
switch (srcElementType)
{
case EETypeElementType.Char:
doubleValue = (double)RuntimeHelpers.FastUnbox<char>(srcObject);
doubleValue = (double)Unsafe.As<byte, char>(ref rawSrcValue);
break;
case EETypeElementType.SByte:
doubleValue = (double)RuntimeHelpers.FastUnbox<sbyte>(srcObject);
doubleValue = (double)Unsafe.As<byte, sbyte>(ref rawSrcValue);
break;
case EETypeElementType.Byte:
doubleValue = (double)RuntimeHelpers.FastUnbox<byte>(srcObject);
doubleValue = (double)rawSrcValue;
break;
case EETypeElementType.Int16:
doubleValue = (double)RuntimeHelpers.FastUnbox<short>(srcObject);
doubleValue = (double)Unsafe.As<byte, short>(ref rawSrcValue);
break;
case EETypeElementType.UInt16:
doubleValue = (double)RuntimeHelpers.FastUnbox<ushort>(srcObject);
doubleValue = (double)Unsafe.As<byte, ushort>(ref rawSrcValue);
break;
case EETypeElementType.Int32:
doubleValue = (double)RuntimeHelpers.FastUnbox<int>(srcObject);
doubleValue = (double)Unsafe.As<byte, int>(ref rawSrcValue);
break;
case EETypeElementType.UInt32:
doubleValue = (double)RuntimeHelpers.FastUnbox<uint>(srcObject);
doubleValue = (double)Unsafe.As<byte, uint>(ref rawSrcValue);
break;
case EETypeElementType.Int64:
doubleValue = (double)RuntimeHelpers.FastUnbox<long>(srcObject);
doubleValue = (double)Unsafe.As<byte, long>(ref rawSrcValue);
break;
case EETypeElementType.UInt64:
doubleValue = (double)RuntimeHelpers.FastUnbox<ulong>(srcObject);
doubleValue = (double)Unsafe.As<byte, ulong>(ref rawSrcValue);
break;
case EETypeElementType.Single:
doubleValue = (double)RuntimeHelpers.FastUnbox<float>(srcObject);
doubleValue = (double)Unsafe.As<byte, float>(ref rawSrcValue);
break;
case EETypeElementType.Double:
doubleValue = RuntimeHelpers.FastUnbox<double>(srcObject);
doubleValue = Unsafe.As<byte, double>(ref rawSrcValue);
break;
default:
goto Failure;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -206,11 +206,6 @@ internal static bool IsBitwiseEquatable<T>()
internal static ref byte GetRawData(this object obj) =>
ref Unsafe.As<RawData>(obj).Data;

[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal static T FastUnbox<T>(this object obj)
where T : unmanaged =>
Unsafe.As<byte, T>(ref Unsafe.As<RawData>(obj).Data);

internal static unsafe nuint GetRawObjectDataSize(this object obj)
{
MethodTable* pMT = GetMethodTable(obj);
Expand Down

0 comments on commit ff4abbc

Please sign in to comment.