Skip to content

Commit 7468c56

Browse files
committed
Try manual unrolling
1 parent d7909d5 commit 7468c56

File tree

1 file changed

+20
-1
lines changed
  • src/coreclr/System.Private.CoreLib/src/System/Runtime/CompilerServices

1 file changed

+20
-1
lines changed

src/coreclr/System.Private.CoreLib/src/System/Runtime/CompilerServices/CastHelpers.cs

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -551,7 +551,26 @@ internal static object Box(MethodTable* typeMT, ref byte unboxedData)
551551
}
552552
else
553553
{
554-
SpanHelpers.Memmove(ref boxed.GetRawData(), ref unboxedData, typeMT->GetNumInstanceFieldBytes());
554+
ref byte dst = ref boxed.GetRawData();
555+
nuint bytes = typeMT->GetNumInstanceFieldBytes();
556+
switch (bytes)
557+
{
558+
case 1:
559+
Unsafe.As<byte, byte>(ref dst) = Unsafe.As<byte, byte>(ref unboxedData);
560+
break;
561+
case 2:
562+
Unsafe.As<byte, ushort>(ref dst) = Unsafe.As<byte, ushort>(ref unboxedData);
563+
break;
564+
case 4:
565+
Unsafe.As<byte, uint>(ref dst) = Unsafe.As<byte, uint>(ref unboxedData);
566+
break;
567+
case 8:
568+
Unsafe.As<byte, ulong>(ref dst) = Unsafe.As<byte, ulong>(ref unboxedData);
569+
break;
570+
default:
571+
SpanHelpers.Memmove(ref dst, ref unboxedData, bytes);
572+
break;
573+
}
555574
}
556575

557576
return boxed;

0 commit comments

Comments
 (0)