Optimize copying for typed arrays in JS

This commit is contained in:
Ilya Gorbunov
2018-08-23 23:56:48 +03:00
parent ea37a65178
commit b0bcd78e38
+11 -6
View File
@@ -136,13 +136,18 @@ internal fun <T> arrayCopy(source: Array<out T>, destination: Array<in T>, desti
val rangeSize = endIndex - startIndex val rangeSize = endIndex - startIndex
AbstractList.checkRangeIndexes(destinationOffset, destinationOffset + rangeSize, destination.size) AbstractList.checkRangeIndexes(destinationOffset, destinationOffset + rangeSize, destination.size)
if (source !== destination || destinationOffset <= startIndex) { if (js("ArrayBuffer").isView(destination) && js("ArrayBuffer").isView(source)) {
for (index in 0 until rangeSize) { val subrange = source.asDynamic().subarray(startIndex, endIndex)
destination[destinationOffset + index] = source[startIndex + index] destination.asDynamic().set(subrange, destinationOffset)
}
} else { } else {
for (index in rangeSize - 1 downTo 0) { if (source !== destination || destinationOffset <= startIndex) {
destination[destinationOffset + index] = source[startIndex + index] for (index in 0 until rangeSize) {
destination[destinationOffset + index] = source[startIndex + index]
}
} else {
for (index in rangeSize - 1 downTo 0) {
destination[destinationOffset + index] = source[startIndex + index]
}
} }
} }
} }