Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Changes from 1 commit
Commits
Show all changes
53 commits
Select commit Hold shift + click to select a range
566b15e
Implement IsSimpleCopy and CanAssignArrayType in managed code
huoyaoyuan May 27, 2024
8a592b9
Fold IsSimpleCopy back to CanAssignArrayType
huoyaoyuan May 27, 2024
0dce3d4
Delete FCall and QCall definitions for copy
huoyaoyuan May 27, 2024
d29e5d8
Convert InternalSetValue to managed
huoyaoyuan May 28, 2024
5dd0e3c
Setup FCalls
huoyaoyuan May 28, 2024
527021c
Remove FCall for GetCorElementTypeOfElementType
huoyaoyuan May 28, 2024
e7d843a
Complete TryUnBox
huoyaoyuan May 28, 2024
2bb5fe7
Fix FCall definition
huoyaoyuan May 28, 2024
f7da172
Implement InitializeArray in managed
huoyaoyuan May 28, 2024
0924555
Implement GetSpanDataFrom in managed
huoyaoyuan May 28, 2024
dc85381
Remove FCall definition
huoyaoyuan May 28, 2024
f99c553
Fix RVA field address
huoyaoyuan May 28, 2024
c5e6cc4
Fix RVA assert
huoyaoyuan May 28, 2024
470b86f
Do not use hydrated RtFieldInfo
huoyaoyuan May 28, 2024
c7821e6
Use QCall for LoadSize
huoyaoyuan May 29, 2024
5225203
Fix CanAssignArrayType condition
huoyaoyuan May 29, 2024
07293e3
Fix compilation
huoyaoyuan May 29, 2024
5da88c3
Simplify AssignType enum
huoyaoyuan May 29, 2024
5222bca
Fix I and U in CanPrimitiveWiden
huoyaoyuan May 29, 2024
335b2fb
CanCastTo should be QCall
huoyaoyuan May 30, 2024
93ec9d8
Remove ThrowHelper usages that not in hot path
huoyaoyuan May 31, 2024
c3f5097
Merge branch 'main' into array-fcall
huoyaoyuan Jun 6, 2024
481e4d4
Revert the known broken InternalSetValue
huoyaoyuan Jun 6, 2024
a266a30
Revert "Revert the known broken InternalSetValue"
huoyaoyuan Jun 6, 2024
b8e4584
Fix heap overwrite
huoyaoyuan Jun 7, 2024
5465a97
Add disabled test for RVA field reflection
huoyaoyuan Jun 9, 2024
4ce4f51
Push back changes around IsSimpleCopy and CanAssignArrayType
huoyaoyuan Jun 10, 2024
30b7d8f
Move PrimitiveWiden to InvokeUtils
huoyaoyuan Jun 12, 2024
8a9c5bc
Merge coreclr specific InvokeUtils
huoyaoyuan Jun 12, 2024
f95fa34
Revert "Merge coreclr specific InvokeUtils"
huoyaoyuan Jun 13, 2024
ebfd224
Move InvokeUtils to shared
huoyaoyuan Jun 13, 2024
d9205c6
Apply suggestions from code review
huoyaoyuan Jun 18, 2024
7eef724
Merge branch 'main' into array-fcall
huoyaoyuan Jun 18, 2024
8cfb58f
Fix trailing whitespace
huoyaoyuan Jun 18, 2024
fbb9d71
Reduce QCalls
huoyaoyuan Jun 18, 2024
324df76
Reduce CorElementType overhead
huoyaoyuan Jun 18, 2024
089ae12
Separate RVA reflection change out
huoyaoyuan Jun 19, 2024
a5e7441
Revert GetCorElementTypeOfElementType
huoyaoyuan Jun 19, 2024
5552d63
Change the MethodTable FCall to be optimal for primitive and share wi…
huoyaoyuan Jun 19, 2024
133cd8a
Return ref from GetSpanDataFrom
huoyaoyuan Jun 19, 2024
e17bf0e
Handle enum conversion to underlying type
huoyaoyuan Jun 19, 2024
dd1e2dd
Update exception message
huoyaoyuan Jun 19, 2024
32b8789
Add KeepAlive
huoyaoyuan Jun 20, 2024
582eaf1
Remove unnecessary qcall handle
huoyaoyuan Jun 20, 2024
1836ae5
Fix ref src
huoyaoyuan Jun 20, 2024
cb6aa00
Update FCall name and assert
huoyaoyuan Jun 24, 2024
49cf866
Remove unused changes
huoyaoyuan Jun 24, 2024
7a0c398
Use void* for address
huoyaoyuan Jun 24, 2024
db677ad
Apply suggestions from code review
huoyaoyuan Jun 25, 2024
353715e
Remove unused enum FCall
huoyaoyuan Jun 24, 2024
8fb1c96
Use sizeof
huoyaoyuan Jun 25, 2024
26d4c80
Add some comment
huoyaoyuan Jun 25, 2024
5f7263f
Add comment to lifetime
huoyaoyuan Jun 26, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Fold IsSimpleCopy back to CanAssignArrayType
  • Loading branch information
huoyaoyuan committed May 27, 2024
commit 8a592b9d7fbef2c387d91c7c3d2fc74cb7a7c997
61 changes: 17 additions & 44 deletions src/coreclr/System.Private.CoreLib/src/System/Array.CoreCLR.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,10 @@ private static unsafe void CopyImpl(Array sourceArray, int sourceIndex, Array de
if ((uint)(destinationIndex + length) > destinationArray.NativeLength)
throw new ArgumentException(SR.Arg_LongerThanDestArray, nameof(destinationArray));

if (sourceArray.GetType() == destinationArray.GetType() || IsSimpleCopy(sourceArray, destinationArray))
AssignArrayEnum assignType = AssignArrayEnum.AssignWrongType;

if (sourceArray.GetType() == destinationArray.GetType()
|| (assignType = CanAssignArrayType(sourceArray, destinationArray)) == AssignArrayEnum.AssignSimpleCopy)
{
MethodTable* pMT = RuntimeHelpers.GetMethodTable(sourceArray);

Expand All @@ -86,40 +89,7 @@ private static unsafe void CopyImpl(Array sourceArray, int sourceIndex, Array de
throw new ArrayTypeMismatchException(SR.ArrayTypeMismatch_ConstrainedCopy);

// Rare
CopySlow(sourceArray, sourceIndex, destinationArray, destinationIndex, length);
}

private static unsafe bool IsSimpleCopy(Array sourceArray, Array destinationArray)
{
TypeHandle srcTH = RuntimeHelpers.GetMethodTable(sourceArray)->GetArrayElementTypeHandle();
TypeHandle destTH = RuntimeHelpers.GetMethodTable(destinationArray)->GetArrayElementTypeHandle();
if (TypeHandle.AreSameType(srcTH, destTH)) // This check kicks for different array kind or dimensions
return true;

if (srcTH.IsValueType)
{
// Value class boxing
if (!destTH.IsValueType)
return false;

CorElementType srcElType = sourceArray.GetCorElementTypeOfElementType();
CorElementType destElType = destinationArray.GetCorElementTypeOfElementType();

// Copying primitives from one type to another
if (srcElType.IsPrimitiveType() && destElType.IsPrimitiveType())
{
if (GetNormalizedIntegralArrayElementType(srcElType) == GetNormalizedIntegralArrayElementType(destElType))
return true;
}
}
else
{
// Value class unboxing
if (destTH.IsValueType)
return false;
}

return srcTH.CanCastTo(destTH);
CopySlow(sourceArray, sourceIndex, destinationArray, destinationIndex, length, assignType);
}

private static CorElementType GetNormalizedIntegralArrayElementType(CorElementType elementType)
Expand All @@ -146,18 +116,16 @@ private static CorElementType GetNormalizedIntegralArrayElementType(CorElementTy
// instance & might fail when called from within a CER, or if the
// reliable flag is true, it will either always succeed or always
// throw an exception with no side effects.
private static unsafe void CopySlow(Array sourceArray, int sourceIndex, Array destinationArray, int destinationIndex, int length)
private static unsafe void CopySlow(Array sourceArray, int sourceIndex, Array destinationArray, int destinationIndex, int length, AssignArrayEnum assignType)
{
Debug.Assert(sourceArray.Rank == destinationArray.Rank);

AssignArrayEnum r = CanAssignArrayType(sourceArray, destinationArray);

if (r == AssignArrayEnum.AssignWrongType)
if (assignType == AssignArrayEnum.AssignWrongType)
ThrowHelper.ThrowArrayTypeMismatchException_CantAssignType();

if (length > 0)
{
switch (r)
switch (assignType)
{
case AssignArrayEnum.AssignUnboxValueClass:
CopyImplUnBoxEachElement(sourceArray, sourceIndex, destinationArray, destinationIndex, length);
Expand Down Expand Up @@ -185,6 +153,7 @@ private static unsafe void CopySlow(Array sourceArray, int sourceIndex, Array de
// Must match the definition in arraynative.cpp
private enum AssignArrayEnum
{
AssignSimpleCopy,
AssignWrongType,
AssignMustCast,
AssignBoxValueClassOrPrimitive,
Expand All @@ -196,7 +165,9 @@ private static unsafe AssignArrayEnum CanAssignArrayType(Array sourceArray, Arra
{
TypeHandle srcTH = RuntimeHelpers.GetMethodTable(sourceArray)->GetArrayElementTypeHandle();
TypeHandle destTH = RuntimeHelpers.GetMethodTable(destinationArray)->GetArrayElementTypeHandle();
Debug.Assert(!TypeHandle.AreSameType(srcTH, destTH)); // Handled by fast path

if (TypeHandle.AreSameType(srcTH, destTH)) // This check kicks for different array kind or dimensions
return AssignArrayEnum.AssignSimpleCopy;

// Value class boxing
if (srcTH.IsValueType && !destTH.IsValueType)
Expand Down Expand Up @@ -224,15 +195,17 @@ private static unsafe AssignArrayEnum CanAssignArrayType(Array sourceArray, Arra
// Copying primitives from one type to another
if (srcElType.IsPrimitiveType() && destElType.IsPrimitiveType())
{
Debug.Assert(srcElType != destElType); // Handled by fast path
if (RuntimeHelpers.CanPrimitiveWiden(srcElType, destElType))
if (GetNormalizedIntegralArrayElementType(srcElType) == GetNormalizedIntegralArrayElementType(destElType))
return AssignArrayEnum.AssignSimpleCopy;
else if (RuntimeHelpers.CanPrimitiveWiden(srcElType, destElType))
return AssignArrayEnum.AssignPrimitiveWiden;
else
return AssignArrayEnum.AssignWrongType;
}

// dest Object extends src
Debug.Assert(!srcTH.CanCastTo(destTH)); // Handled by fast path
if (srcTH.CanCastTo(destTH))
return AssignArrayEnum.AssignSimpleCopy;

// src Object extends dest
if (destTH.CanCastTo(srcTH))
Expand Down