Skip to content

Commit cd5ac0b

Browse files
LouisCADqwwdfsad
authored andcommitted
Replace hand-rolled arraycopy with stdlib copyInto
1 parent 7acaae6 commit cd5ac0b

File tree

9 files changed

+47
-86
lines changed

9 files changed

+47
-86
lines changed

kotlinx-coroutines-core/common/src/channels/ConflatedBroadcastChannel.kt

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -145,8 +145,15 @@ public class ConflatedBroadcastChannel<E>() : BroadcastChannel<E> {
145145
check(i >= 0)
146146
if (n == 1) return null
147147
val update = arrayOfNulls<Subscriber<E>>(n - 1)
148-
arraycopy(list, 0, update, 0, i)
149-
arraycopy(list, i + 1, update, i, n - i - 1)
148+
list.copyInto(
149+
destination = update,
150+
endIndex = i
151+
)
152+
list.copyInto(
153+
destination = update,
154+
destinationOffset = i,
155+
startIndex = i + 1
156+
)
150157
return update as Array<Subscriber<E>>
151158
}
152159

kotlinx-coroutines-core/common/src/internal/ArrayCopy.common.kt

Lines changed: 0 additions & 10 deletions
This file was deleted.

kotlinx-coroutines-core/common/src/internal/ArrayQueue.kt

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,15 @@ internal open class ArrayQueue<T : Any> {
3636
val currentSize = elements.size
3737
val newCapacity = currentSize shl 1
3838
val newElements = arrayOfNulls<Any>(newCapacity)
39-
val remaining = elements.size - head
40-
arraycopy(elements, head, newElements, 0, remaining)
41-
arraycopy(elements, 0, newElements, remaining, head)
39+
elements.copyInto(
40+
destination = newElements,
41+
startIndex = head
42+
)
43+
elements.copyInto(
44+
destination = newElements,
45+
destinationOffset = elements.size - head,
46+
endIndex = head
47+
)
4248
elements = newElements
4349
head = 0
4450
tail = currentSize

kotlinx-coroutines-core/common/test/flow/NamedDispatchers.kt

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
package kotlinx.coroutines
66

7-
import kotlinx.coroutines.internal.*
87
import kotlin.coroutines.*
98
import kotlin.native.concurrent.*
109

@@ -55,9 +54,15 @@ private class ArrayStack {
5554
val currentSize = elements.size
5655
val newCapacity = currentSize shl 1
5756
val newElements = arrayOfNulls<String>(newCapacity)
58-
val remaining = elements.size - head
59-
arraycopy(elements, head, newElements, 0, remaining)
60-
arraycopy(elements, 0, newElements, remaining, head)
57+
elements.copyInto(
58+
destination = newElements,
59+
startIndex = head
60+
)
61+
elements.copyInto(
62+
destination = newElements,
63+
destinationOffset = elements.size - head,
64+
endIndex = head
65+
)
6166
elements = newElements
6267
}
6368
}

kotlinx-coroutines-core/js/src/internal/ArrayCopy.kt

Lines changed: 0 additions & 12 deletions
This file was deleted.

kotlinx-coroutines-core/js/test/internal/ArrayCopyKtTest.kt

Lines changed: 0 additions & 18 deletions
This file was deleted.

kotlinx-coroutines-core/jvm/src/internal/ArrayCopy.kt

Lines changed: 0 additions & 15 deletions
This file was deleted.

kotlinx-coroutines-core/native/src/internal/ArrayCopy.kt

Lines changed: 0 additions & 18 deletions
This file was deleted.

kotlinx-coroutines-core/native/src/internal/CopyOnWriteList.kt

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,17 @@ internal class CopyOnWriteList<E>(private var array: Array<Any?> = arrayOfNulls(
2121
override fun add(index: Int, element: E) {
2222
rangeCheck(index)
2323
val update = arrayOfNulls<Any?>(if (array.size == _size) array.size * 2 else array.size)
24-
arraycopy(array, 0, update, 0, index)
24+
array.copyInto(
25+
destination = update,
26+
endIndex = index
27+
)
2528
update[index] = element
26-
arraycopy(array, index, update, index + 1, _size - index + 1)
29+
array.copyInto(
30+
destination = update,
31+
destinationOffset = index + 1,
32+
startIndex = index,
33+
endIndex = _size + 1
34+
)
2735
array = update
2836
}
2937

@@ -43,8 +51,16 @@ internal class CopyOnWriteList<E>(private var array: Array<Any?> = arrayOfNulls(
4351
val n = array.size
4452
val element = array[index]
4553
val update = arrayOfNulls<Any>(n)
46-
arraycopy(array, 0, update, 0, index)
47-
arraycopy(array, index + 1, update, index, n - index - 1)
54+
array.copyInto(
55+
destination = update,
56+
endIndex = index
57+
)
58+
array.copyInto(
59+
destination = update,
60+
destinationOffset = index,
61+
startIndex = index + 1,
62+
endIndex = n
63+
)
4864
array = update
4965
--_size
5066
return element as E

0 commit comments

Comments
 (0)