@@ -1424,28 +1424,40 @@ FCIMPL5(VOID, Buffer::BlockCopy, ArrayBase *src, int srcOffset, ArrayBase *dst,
1424
1424
1425
1425
MethodTable * pByteArrayMT = g_pByteArrayMT;
1426
1426
_ASSERTE (pByteArrayMT != NULL );
1427
- if (src->GetMethodTable () == pByteArrayMT && dst->GetMethodTable () == pByteArrayMT)
1427
+
1428
+ // Optimization: If src is a byte array, we can
1429
+ // simply set srcLen to GetNumComponents, without having
1430
+ // to call GetComponentSize or verifying GetArrayElementType
1431
+ if (src->GetMethodTable () == pByteArrayMT)
1428
1432
{
1429
1433
srcLen = src->GetNumComponents ();
1430
- dstLen = dst->GetNumComponents ();
1431
1434
}
1432
1435
else
1433
1436
{
1434
- // Size of the Arrays in bytes
1435
1437
srcLen = src->GetNumComponents () * src->GetComponentSize ();
1436
- dstLen = srcLen;
1437
1438
1438
1439
// We only want to allow arrays of primitives, no Objects.
1439
1440
const CorElementType srcET = src->GetArrayElementType ();
1440
1441
if (!CorTypeInfo::IsPrimitiveType_NoThrow (srcET))
1441
1442
FCThrowArgumentVoid (W (" src" ), W (" Arg_MustBePrimArray" ));
1442
-
1443
- if (src != dst) {
1444
- const CorElementType dstET = dst->GetArrayElementType ();
1445
- if (!CorTypeInfo::IsPrimitiveType_NoThrow (dstET))
1446
- FCThrowArgumentVoid (W (" dest" ), W (" Arg_MustBePrimArray" ));
1447
- dstLen = dst->GetNumComponents () * dst->GetComponentSize ();
1448
- }
1443
+ }
1444
+
1445
+ // Optimization: If copying to/from the same array, then
1446
+ // we know that dstLen and srcLen must be the same.
1447
+ if (src == dst)
1448
+ {
1449
+ dstLen = srcLen;
1450
+ }
1451
+ else if (dst->GetMethodTable () == pByteArrayMT)
1452
+ {
1453
+ dstLen = dst->GetNumComponents ();
1454
+ }
1455
+ else
1456
+ {
1457
+ const CorElementType dstET = dst->GetArrayElementType ();
1458
+ if (!CorTypeInfo::IsPrimitiveType_NoThrow (dstET))
1459
+ FCThrowArgumentVoid (W (" dest" ), W (" Arg_MustBePrimArray" ));
1460
+ dstLen = dst->GetNumComponents () * dst->GetComponentSize ();
1449
1461
}
1450
1462
1451
1463
if (srcOffset < 0 || dstOffset < 0 || count < 0 ) {
0 commit comments