Skip to content

Commit 0d8507a

Browse files
bbrehmSethTisue
authored andcommitted
Improve Array.copy performance
If source and dest are both non-primitive array types, then we can always use System.arraycopy and avoid the slowcopy branch. ArrayBuffer.toArray is added to the benchmark suite in order to avoid regressions and demonstrate the performance impact.
1 parent 42eb1b2 commit 0d8507a

File tree

1 file changed

+3
-1
lines changed

1 file changed

+3
-1
lines changed

library/src/scala/Array.scala

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,9 @@ object Array {
106106
*/
107107
def copy(src: AnyRef, srcPos: Int, dest: AnyRef, destPos: Int, length: Int): Unit = {
108108
val srcClass = src.getClass
109-
if (srcClass.isArray && dest.getClass.isAssignableFrom(srcClass))
109+
val destClass = dest.getClass
110+
if (srcClass.isArray && ((destClass eq srcClass) ||
111+
(destClass.isArray && !srcClass.getComponentType.isPrimitive && !destClass.getComponentType.isPrimitive)))
110112
java.lang.System.arraycopy(src, srcPos, dest, destPos, length)
111113
else
112114
slowcopy(src, srcPos, dest, destPos, length)

0 commit comments

Comments
 (0)