Skip to content

Commit e994f00

Browse files
Copilotjkotas
andauthored
Add fast path to Array.ConstrainedCopy for single-dimensional arrays (#122530)
Adds a fast path optimization to `Array.ConstrainedCopy` for single-dimensional zero-based arrays, bringing it to performance parity with `Array.Copy`. Fixes #122518 --------- Co-authored-by: copilot-swe-agent[bot] <[email protected]> Co-authored-by: jkotas <[email protected]>
1 parent 72cec96 commit e994f00

File tree

2 files changed

+15
-5
lines changed
  • src

2 files changed

+15
-5
lines changed

src/libraries/System.Private.CoreLib/src/System/Array.cs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -352,18 +352,18 @@ public static void Copy(Array sourceArray, long sourceIndex, Array destinationAr
352352
Copy(sourceArray, isourceIndex, destinationArray, idestinationIndex, ilength);
353353
}
354354

355+
#if !MONO // implementation details of MethodTable
356+
355357
// Provides a strong exception guarantee - either it succeeds, or
356358
// it throws an exception with no side effects. The arrays must be
357359
// compatible array types based on the array element type - this
358360
// method does not support casting, boxing, or primitive widening.
359361
// It will up-cast, assuming the array types are correct.
360362
public static void ConstrainedCopy(Array sourceArray, int sourceIndex, Array destinationArray, int destinationIndex, int length)
361363
{
362-
CopyImpl(sourceArray, sourceIndex, destinationArray, destinationIndex, length, reliable: true);
364+
Copy(sourceArray, sourceIndex, destinationArray, destinationIndex, length, reliable: true);
363365
}
364366

365-
#if !MONO // implementation details of MethodTable
366-
367367
// Copies length elements from sourceArray, starting at index 0, to
368368
// destinationArray, starting at index 0.
369369
public static unsafe void Copy(Array sourceArray, Array destinationArray, int length)
@@ -396,7 +396,12 @@ public static unsafe void Copy(Array sourceArray, Array destinationArray, int le
396396

397397
// Copies length elements from sourceArray, starting at sourceIndex, to
398398
// destinationArray, starting at destinationIndex.
399-
public static unsafe void Copy(Array sourceArray, int sourceIndex, Array destinationArray, int destinationIndex, int length)
399+
public static void Copy(Array sourceArray, int sourceIndex, Array destinationArray, int destinationIndex, int length)
400+
{
401+
Copy(sourceArray, sourceIndex, destinationArray, destinationIndex, length, reliable: false);
402+
}
403+
404+
private static unsafe void Copy(Array sourceArray, int sourceIndex, Array destinationArray, int destinationIndex, int length, bool reliable)
400405
{
401406
if (sourceArray != null && destinationArray != null)
402407
{
@@ -423,7 +428,7 @@ public static unsafe void Copy(Array sourceArray, int sourceIndex, Array destina
423428
}
424429

425430
// Less common
426-
CopyImpl(sourceArray, sourceIndex, destinationArray, destinationIndex, length, reliable: false);
431+
CopyImpl(sourceArray, sourceIndex, destinationArray, destinationIndex, length, reliable);
427432
}
428433

429434
// Reliability-wise, this method will either possibly corrupt your

src/mono/System.Private.CoreLib/src/System/Array.Mono.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,11 @@ public static void Copy(Array sourceArray, int sourceIndex, Array destinationArr
106106
CopyImpl(sourceArray, sourceIndex, destinationArray, destinationIndex, length, false);
107107
}
108108

109+
public static void ConstrainedCopy(Array sourceArray, int sourceIndex, Array destinationArray, int destinationIndex, int length)
110+
{
111+
CopyImpl(sourceArray, sourceIndex, destinationArray, destinationIndex, length, true);
112+
}
113+
109114
private static void CopyImpl(Array sourceArray, int sourceIndex, Array destinationArray, int destinationIndex, int length, bool reliable)
110115
{
111116
ArgumentNullException.ThrowIfNull(sourceArray);

0 commit comments

Comments
 (0)