Skip to content

Commit 4ef3cea

Browse files
committed
Optimization: change Array.copy to System.arrayCopy
This is more efficient, especially for small sizes where setup cost for Array.copy is higher.
1 parent 2244eb1 commit 4ef3cea

File tree

12 files changed

+17
-17
lines changed

12 files changed

+17
-17
lines changed

compiler/src/dotty/tools/dotc/core/Names.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -376,7 +376,7 @@ object Names {
376376

377377
override def replace(from: Char, to: Char): SimpleName = {
378378
val cs = new Array[Char](length)
379-
Array.copy(chrs, start, cs, 0, length)
379+
System.arraycopy(chrs, start, cs, 0, length)
380380
for (i <- 0 until length) {
381381
if (cs(i) == from) cs(i) = to
382382
}

compiler/src/dotty/tools/dotc/core/SymDenotations.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2302,7 +2302,7 @@ object SymDenotations {
23022302

23032303
private def resize(size: Int) = {
23042304
val classIds1 = new Array[Int](size)
2305-
Array.copy(classIds, 0, classIds1, 0, classIds.length min size)
2305+
System.arraycopy(classIds, 0, classIds1, 0, classIds.length min size)
23062306
classIds = classIds1
23072307
}
23082308

compiler/src/dotty/tools/dotc/core/tasty/TastyBuffer.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ class TastyBuffer(initialSize: Int) {
5959
/** Write the first `n` bytes of `data`. */
6060
def writeBytes(data: Array[Byte], n: Int): Unit = {
6161
while (bytes.length < length + n) bytes = dble(bytes)
62-
Array.copy(data, 0, bytes, length, n)
62+
System.arraycopy(data, 0, bytes, length, n)
6363
length += n
6464
}
6565

compiler/src/dotty/tools/dotc/core/tasty/TastyReader.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ class TastyReader(val bytes: Array[Byte], start: Int, end: Int, val base: Int =
5454
/** Read the next `n` bytes of `data`. */
5555
def readBytes(n: Int): Array[Byte] = {
5656
val result = new Array[Byte](n)
57-
Array.copy(bytes, bp, result, 0, n)
57+
System.arraycopy(bytes, bp, result, 0, n)
5858
bp += n
5959
result
6060
}

compiler/src/dotty/tools/dotc/core/tasty/TreeBuffer.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ class TreeBuffer extends TastyBuffer(50000) {
137137
var i = 0
138138
var wasted = 0
139139
def shift(end: Int) =
140-
Array.copy(bytes, start, bytes, start - lastDelta, end - start)
140+
System.arraycopy(bytes, start, bytes, start - lastDelta, end - start)
141141
while (i < numOffsets) {
142142
val next = offsets(i)
143143
shift(next)

compiler/src/dotty/tools/dotc/core/unpickleScala2/PickleBuffer.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ class PickleBuffer(data: Array[Byte], from: Int, to: Int) {
2020
/** Double bytes array */
2121
private def dble(): Unit = {
2222
val bytes1 = new Array[Byte](bytes.length * 2)
23-
Array.copy(bytes, 0, bytes1, 0, writeIndex)
23+
System.arraycopy(bytes, 0, bytes1, 0, writeIndex)
2424
bytes = bytes1
2525
}
2626

@@ -69,7 +69,7 @@ class PickleBuffer(data: Array[Byte], from: Int, to: Int) {
6969
def patchNat(pos: Int, x: Int): Unit = {
7070
def patchNatPrefix(x: Int): Unit = {
7171
writeByte(0)
72-
Array.copy(bytes, pos, bytes, pos + 1, writeIndex - (pos + 1))
72+
System.arraycopy(bytes, pos, bytes, pos + 1, writeIndex - (pos + 1))
7373
bytes(pos) = ((x & 0x7f) | 0x80).toByte
7474
val y = x >>> 7
7575
if (y != 0) patchNatPrefix(y)

compiler/src/dotty/tools/dotc/rewrites/Rewrites.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ object Rewrites {
3434
@tailrec def loop(ps: List[Patch], inIdx: Int, outIdx: Int): Unit = {
3535
def copy(upTo: Int): Int = {
3636
val untouched = upTo - inIdx
37-
Array.copy(cs, inIdx, ds, outIdx, untouched)
37+
System.arraycopy(cs, inIdx, ds, outIdx, untouched)
3838
outIdx + untouched
3939
}
4040
ps match {

compiler/src/dotty/tools/dotc/util/SimpleIdentityMap.scala

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -160,8 +160,8 @@ object SimpleIdentityMap {
160160
m
161161
} else {
162162
val bindings1 = new Array[AnyRef](bindings.length - 2)
163-
Array.copy(bindings, 0, bindings1, 0, i)
164-
Array.copy(bindings, i + 2, bindings1, i, bindings1.length - i)
163+
System.arraycopy(bindings, 0, bindings1, 0, i)
164+
System.arraycopy(bindings, i + 2, bindings1, i, bindings1.length - i)
165165
new MapMore(bindings1)
166166
}
167167
}
@@ -185,7 +185,7 @@ object SimpleIdentityMap {
185185
i += 2
186186
}
187187
val bindings2 = new Array[AnyRef](bindings.length + 2)
188-
Array.copy(bindings, 0, bindings2, 0, bindings.length)
188+
System.arraycopy(bindings, 0, bindings2, 0, bindings.length)
189189
bindings2(bindings.length) = k
190190
bindings2(bindings.length + 1) = v
191191
new MapMore(bindings2)

compiler/src/dotty/tools/dotc/util/SimpleIdentitySet.scala

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ object SimpleIdentitySet {
8383
def size = xs.length
8484
def + [E >: Elem <: AnyRef](x: E): SimpleIdentitySet[E] = {
8585
val xs1 = new Array[AnyRef](size + 1)
86-
Array.copy(xs, 0, xs1, 0, size)
86+
System.arraycopy(xs, 0, xs1, 0, size)
8787
xs1(size) = x
8888
new SetN[E](xs1)
8989
}
@@ -98,8 +98,8 @@ object SimpleIdentitySet {
9898
else new Set3(xs(0), xs(1), xs(2))
9999
else {
100100
val xs1 = new Array[AnyRef](size - 1)
101-
Array.copy(xs, 0, xs1, 0, i)
102-
Array.copy(xs, i + 1, xs1, i, size - (i + 1))
101+
System.arraycopy(xs, 0, xs1, 0, i)
102+
System.arraycopy(xs, i + 1, xs1, i, size - (i + 1))
103103
new SetN(xs1)
104104
}
105105
}

compiler/src/dotty/tools/dotc/util/SourceFile.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ class SourceFile(val file: AbstractFile, computeContent: => Array[Char]) extends
180180
val id = chunks << ChunkSizeLog
181181
if (chunks == sourceOfChunk.length) {
182182
val a = new Array[SourceFile](chunks * 2)
183-
Array.copy(sourceOfChunk, 0, a, 0, chunks)
183+
System.arraycopy(sourceOfChunk, 0, a, 0, chunks)
184184
sourceOfChunk = a
185185
}
186186
sourceOfChunk(chunks) = this

compiler/src/dotty/tools/dotc/util/Util.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ object Util {
2626
/** An array twice the size of given array, with existing elements copied over */
2727
def dble[T: ClassTag](arr: Array[T]): Array[T] = {
2828
val arr1 = new Array[T](arr.length * 2)
29-
Array.copy(arr, 0, arr1, 0, arr.length)
29+
System.arraycopy(arr, 0, arr1, 0, arr.length)
3030
arr1
3131
}
3232
}

library/src-bootstrapped/scala/Tuple.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ object Tuple {
183183
def $consArray[H](x: H, elems: Array[Object]): Array[Object] = {
184184
val elems1 = new Array[Object](elems.length + 1)
185185
elems1(0) = x.asInstanceOf[Object]
186-
Array.copy(elems, 0, elems1, 1, elems.length)
186+
System.arraycopy(elems, 0, elems1, 1, elems.length)
187187
elems1
188188
}
189189

0 commit comments

Comments
 (0)