[WASM] Refactoring wasm array copy functions

This commit is contained in:
Igor Yakovlev
2022-07-07 16:19:27 +02:00
committed by teamcity
parent 39844af876
commit a0a66ffb96
8 changed files with 84 additions and 125 deletions
+3 -3
View File
@@ -104,11 +104,11 @@ fun wasmArrayForType(
@WasmOp(WasmOp.ARRAY_LEN)
fun len(): Int =
implementedAsIntrinsic
implementedAsIntrinsic
}
internal inline fun $name.copyTo(destination: $name, sourceIndex: Int, destinationIndex: Int, length: Int) {
wasm_array_copy<$name>(destination, destinationIndex, this, sourceIndex, length)
internal inline fun copyWasmArray(source: $name, destination: $name, sourceIndex: Int, destinationIndex: Int, length: Int) {
wasm_array_copy<$name>(destination, destinationIndex, source, sourceIndex, length)
}
internal inline fun $name.fill(size: Int, init: (Int) -> $type) {
@@ -29,8 +29,8 @@ public class String private constructor(internal val chars: WasmCharArray) : Com
if (otherLen == 0) return String(thisChars)
val newChars = WasmCharArray(thisLen + otherLen)
thisChars.copyTo(newChars, 0, 0, thisLen)
otherChars.copyTo(newChars, 0, thisLen, otherLen)
copyWasmArray(thisChars, newChars, 0, 0, thisLen)
copyWasmArray(otherChars, newChars, 0, thisLen, otherLen)
return String(newChars)
}
@@ -54,7 +54,7 @@ public class String private constructor(internal val chars: WasmCharArray) : Com
val actualEndIndex = endIndex.coerceAtMost(thisChars.len())
val newCharsLen = actualEndIndex - actualStartIndex
val newChars = WasmCharArray(newCharsLen)
thisChars.copyTo(newChars, actualStartIndex, 0, newCharsLen)
copyWasmArray(thisChars, newChars, actualStartIndex, 0, newCharsLen)
return String(newChars)
}
@@ -1017,8 +1017,10 @@ public actual fun CharArray?.contentToString(): String {
@SinceKotlin("1.3")
@Suppress("ACTUAL_FUNCTION_WITH_DEFAULT_ARGUMENTS")
public actual fun <T> Array<out T>.copyInto(destination: Array<T>, destinationOffset: Int = 0, startIndex: Int = 0, endIndex: Int = size): Array<T> {
@Suppress("UNCHECKED_CAST")
arrayCopy(this as Array<Any?>, startIndex, destination as Array<Any?>, destinationOffset, endIndex - startIndex)
AbstractList.checkRangeIndexes(startIndex, endIndex, this.size)
val rangeSize = endIndex - startIndex
AbstractList.checkRangeIndexes(destinationOffset, destinationOffset + rangeSize, destination.size)
kotlin.wasm.internal.copyWasmArray(this.storage, destination.storage, startIndex, destinationOffset, rangeSize)
return destination
}
@@ -1041,7 +1043,10 @@ public actual fun <T> Array<out T>.copyInto(destination: Array<T>, destinationOf
@SinceKotlin("1.3")
@Suppress("ACTUAL_FUNCTION_WITH_DEFAULT_ARGUMENTS")
public actual fun ByteArray.copyInto(destination: ByteArray, destinationOffset: Int = 0, startIndex: Int = 0, endIndex: Int = size): ByteArray {
arrayCopy(this, startIndex, destination, destinationOffset, endIndex - startIndex)
AbstractList.checkRangeIndexes(startIndex, endIndex, this.size)
val rangeSize = endIndex - startIndex
AbstractList.checkRangeIndexes(destinationOffset, destinationOffset + rangeSize, destination.size)
kotlin.wasm.internal.copyWasmArray(this.storage, destination.storage, startIndex, destinationOffset, rangeSize)
return destination
}
@@ -1064,7 +1069,10 @@ public actual fun ByteArray.copyInto(destination: ByteArray, destinationOffset:
@SinceKotlin("1.3")
@Suppress("ACTUAL_FUNCTION_WITH_DEFAULT_ARGUMENTS")
public actual fun ShortArray.copyInto(destination: ShortArray, destinationOffset: Int = 0, startIndex: Int = 0, endIndex: Int = size): ShortArray {
arrayCopy(this, startIndex, destination, destinationOffset, endIndex - startIndex)
AbstractList.checkRangeIndexes(startIndex, endIndex, this.size)
val rangeSize = endIndex - startIndex
AbstractList.checkRangeIndexes(destinationOffset, destinationOffset + rangeSize, destination.size)
kotlin.wasm.internal.copyWasmArray(this.storage, destination.storage, startIndex, destinationOffset, rangeSize)
return destination
}
@@ -1087,7 +1095,10 @@ public actual fun ShortArray.copyInto(destination: ShortArray, destinationOffset
@SinceKotlin("1.3")
@Suppress("ACTUAL_FUNCTION_WITH_DEFAULT_ARGUMENTS")
public actual fun IntArray.copyInto(destination: IntArray, destinationOffset: Int = 0, startIndex: Int = 0, endIndex: Int = size): IntArray {
arrayCopy(this, startIndex, destination, destinationOffset, endIndex - startIndex)
AbstractList.checkRangeIndexes(startIndex, endIndex, this.size)
val rangeSize = endIndex - startIndex
AbstractList.checkRangeIndexes(destinationOffset, destinationOffset + rangeSize, destination.size)
kotlin.wasm.internal.copyWasmArray(this.storage, destination.storage, startIndex, destinationOffset, rangeSize)
return destination
}
@@ -1110,7 +1121,10 @@ public actual fun IntArray.copyInto(destination: IntArray, destinationOffset: In
@SinceKotlin("1.3")
@Suppress("ACTUAL_FUNCTION_WITH_DEFAULT_ARGUMENTS")
public actual fun LongArray.copyInto(destination: LongArray, destinationOffset: Int = 0, startIndex: Int = 0, endIndex: Int = size): LongArray {
arrayCopy(this, startIndex, destination, destinationOffset, endIndex - startIndex)
AbstractList.checkRangeIndexes(startIndex, endIndex, this.size)
val rangeSize = endIndex - startIndex
AbstractList.checkRangeIndexes(destinationOffset, destinationOffset + rangeSize, destination.size)
kotlin.wasm.internal.copyWasmArray(this.storage, destination.storage, startIndex, destinationOffset, rangeSize)
return destination
}
@@ -1133,7 +1147,10 @@ public actual fun LongArray.copyInto(destination: LongArray, destinationOffset:
@SinceKotlin("1.3")
@Suppress("ACTUAL_FUNCTION_WITH_DEFAULT_ARGUMENTS")
public actual fun FloatArray.copyInto(destination: FloatArray, destinationOffset: Int = 0, startIndex: Int = 0, endIndex: Int = size): FloatArray {
arrayCopy(this, startIndex, destination, destinationOffset, endIndex - startIndex)
AbstractList.checkRangeIndexes(startIndex, endIndex, this.size)
val rangeSize = endIndex - startIndex
AbstractList.checkRangeIndexes(destinationOffset, destinationOffset + rangeSize, destination.size)
kotlin.wasm.internal.copyWasmArray(this.storage, destination.storage, startIndex, destinationOffset, rangeSize)
return destination
}
@@ -1156,7 +1173,10 @@ public actual fun FloatArray.copyInto(destination: FloatArray, destinationOffset
@SinceKotlin("1.3")
@Suppress("ACTUAL_FUNCTION_WITH_DEFAULT_ARGUMENTS")
public actual fun DoubleArray.copyInto(destination: DoubleArray, destinationOffset: Int = 0, startIndex: Int = 0, endIndex: Int = size): DoubleArray {
arrayCopy(this, startIndex, destination, destinationOffset, endIndex - startIndex)
AbstractList.checkRangeIndexes(startIndex, endIndex, this.size)
val rangeSize = endIndex - startIndex
AbstractList.checkRangeIndexes(destinationOffset, destinationOffset + rangeSize, destination.size)
kotlin.wasm.internal.copyWasmArray(this.storage, destination.storage, startIndex, destinationOffset, rangeSize)
return destination
}
@@ -1179,7 +1199,10 @@ public actual fun DoubleArray.copyInto(destination: DoubleArray, destinationOffs
@SinceKotlin("1.3")
@Suppress("ACTUAL_FUNCTION_WITH_DEFAULT_ARGUMENTS")
public actual fun BooleanArray.copyInto(destination: BooleanArray, destinationOffset: Int = 0, startIndex: Int = 0, endIndex: Int = size): BooleanArray {
arrayCopy(this, startIndex, destination, destinationOffset, endIndex - startIndex)
AbstractList.checkRangeIndexes(startIndex, endIndex, this.size)
val rangeSize = endIndex - startIndex
AbstractList.checkRangeIndexes(destinationOffset, destinationOffset + rangeSize, destination.size)
kotlin.wasm.internal.copyWasmArray(this.storage, destination.storage, startIndex, destinationOffset, rangeSize)
return destination
}
@@ -1202,7 +1225,10 @@ public actual fun BooleanArray.copyInto(destination: BooleanArray, destinationOf
@SinceKotlin("1.3")
@Suppress("ACTUAL_FUNCTION_WITH_DEFAULT_ARGUMENTS")
public actual fun CharArray.copyInto(destination: CharArray, destinationOffset: Int = 0, startIndex: Int = 0, endIndex: Int = size): CharArray {
arrayCopy(this, startIndex, destination, destinationOffset, endIndex - startIndex)
AbstractList.checkRangeIndexes(startIndex, endIndex, this.size)
val rangeSize = endIndex - startIndex
AbstractList.checkRangeIndexes(destinationOffset, destinationOffset + rangeSize, destination.size)
kotlin.wasm.internal.copyWasmArray(this.storage, destination.storage, startIndex, destinationOffset, rangeSize)
return destination
}
@@ -32,11 +32,11 @@ internal class WasmAnyArray(size: Int) {
@WasmOp(WasmOp.ARRAY_LEN)
fun len(): Int =
implementedAsIntrinsic
implementedAsIntrinsic
}
internal inline fun WasmAnyArray.copyTo(destination: WasmAnyArray, sourceIndex: Int, destinationIndex: Int, length: Int) {
wasm_array_copy<WasmAnyArray>(destination, destinationIndex, this, sourceIndex, length)
internal inline fun copyWasmArray(source: WasmAnyArray, destination: WasmAnyArray, sourceIndex: Int, destinationIndex: Int, length: Int) {
wasm_array_copy<WasmAnyArray>(destination, destinationIndex, source, sourceIndex, length)
}
internal inline fun WasmAnyArray.fill(size: Int, init: (Int) -> Any?) {
@@ -58,11 +58,11 @@ internal class WasmByteArray(size: Int) {
@WasmOp(WasmOp.ARRAY_LEN)
fun len(): Int =
implementedAsIntrinsic
implementedAsIntrinsic
}
internal inline fun WasmByteArray.copyTo(destination: WasmByteArray, sourceIndex: Int, destinationIndex: Int, length: Int) {
wasm_array_copy<WasmByteArray>(destination, destinationIndex, this, sourceIndex, length)
internal inline fun copyWasmArray(source: WasmByteArray, destination: WasmByteArray, sourceIndex: Int, destinationIndex: Int, length: Int) {
wasm_array_copy<WasmByteArray>(destination, destinationIndex, source, sourceIndex, length)
}
internal inline fun WasmByteArray.fill(size: Int, init: (Int) -> Byte) {
@@ -84,11 +84,11 @@ internal class WasmCharArray(size: Int) {
@WasmOp(WasmOp.ARRAY_LEN)
fun len(): Int =
implementedAsIntrinsic
implementedAsIntrinsic
}
internal inline fun WasmCharArray.copyTo(destination: WasmCharArray, sourceIndex: Int, destinationIndex: Int, length: Int) {
wasm_array_copy<WasmCharArray>(destination, destinationIndex, this, sourceIndex, length)
internal inline fun copyWasmArray(source: WasmCharArray, destination: WasmCharArray, sourceIndex: Int, destinationIndex: Int, length: Int) {
wasm_array_copy<WasmCharArray>(destination, destinationIndex, source, sourceIndex, length)
}
internal inline fun WasmCharArray.fill(size: Int, init: (Int) -> Char) {
@@ -110,11 +110,11 @@ internal class WasmShortArray(size: Int) {
@WasmOp(WasmOp.ARRAY_LEN)
fun len(): Int =
implementedAsIntrinsic
implementedAsIntrinsic
}
internal inline fun WasmShortArray.copyTo(destination: WasmShortArray, sourceIndex: Int, destinationIndex: Int, length: Int) {
wasm_array_copy<WasmShortArray>(destination, destinationIndex, this, sourceIndex, length)
internal inline fun copyWasmArray(source: WasmShortArray, destination: WasmShortArray, sourceIndex: Int, destinationIndex: Int, length: Int) {
wasm_array_copy<WasmShortArray>(destination, destinationIndex, source, sourceIndex, length)
}
internal inline fun WasmShortArray.fill(size: Int, init: (Int) -> Short) {
@@ -136,11 +136,11 @@ internal class WasmIntArray(size: Int) {
@WasmOp(WasmOp.ARRAY_LEN)
fun len(): Int =
implementedAsIntrinsic
implementedAsIntrinsic
}
internal inline fun WasmIntArray.copyTo(destination: WasmIntArray, sourceIndex: Int, destinationIndex: Int, length: Int) {
wasm_array_copy<WasmIntArray>(destination, destinationIndex, this, sourceIndex, length)
internal inline fun copyWasmArray(source: WasmIntArray, destination: WasmIntArray, sourceIndex: Int, destinationIndex: Int, length: Int) {
wasm_array_copy<WasmIntArray>(destination, destinationIndex, source, sourceIndex, length)
}
internal inline fun WasmIntArray.fill(size: Int, init: (Int) -> Int) {
@@ -162,11 +162,11 @@ internal class WasmLongArray(size: Int) {
@WasmOp(WasmOp.ARRAY_LEN)
fun len(): Int =
implementedAsIntrinsic
implementedAsIntrinsic
}
internal inline fun WasmLongArray.copyTo(destination: WasmLongArray, sourceIndex: Int, destinationIndex: Int, length: Int) {
wasm_array_copy<WasmLongArray>(destination, destinationIndex, this, sourceIndex, length)
internal inline fun copyWasmArray(source: WasmLongArray, destination: WasmLongArray, sourceIndex: Int, destinationIndex: Int, length: Int) {
wasm_array_copy<WasmLongArray>(destination, destinationIndex, source, sourceIndex, length)
}
internal inline fun WasmLongArray.fill(size: Int, init: (Int) -> Long) {
@@ -188,11 +188,11 @@ internal class WasmFloatArray(size: Int) {
@WasmOp(WasmOp.ARRAY_LEN)
fun len(): Int =
implementedAsIntrinsic
implementedAsIntrinsic
}
internal inline fun WasmFloatArray.copyTo(destination: WasmFloatArray, sourceIndex: Int, destinationIndex: Int, length: Int) {
wasm_array_copy<WasmFloatArray>(destination, destinationIndex, this, sourceIndex, length)
internal inline fun copyWasmArray(source: WasmFloatArray, destination: WasmFloatArray, sourceIndex: Int, destinationIndex: Int, length: Int) {
wasm_array_copy<WasmFloatArray>(destination, destinationIndex, source, sourceIndex, length)
}
internal inline fun WasmFloatArray.fill(size: Int, init: (Int) -> Float) {
@@ -214,11 +214,11 @@ internal class WasmDoubleArray(size: Int) {
@WasmOp(WasmOp.ARRAY_LEN)
fun len(): Int =
implementedAsIntrinsic
implementedAsIntrinsic
}
internal inline fun WasmDoubleArray.copyTo(destination: WasmDoubleArray, sourceIndex: Int, destinationIndex: Int, length: Int) {
wasm_array_copy<WasmDoubleArray>(destination, destinationIndex, this, sourceIndex, length)
internal inline fun copyWasmArray(source: WasmDoubleArray, destination: WasmDoubleArray, sourceIndex: Int, destinationIndex: Int, length: Int) {
wasm_array_copy<WasmDoubleArray>(destination, destinationIndex, source, sourceIndex, length)
}
internal inline fun WasmDoubleArray.fill(size: Int, init: (Int) -> Double) {
@@ -5,84 +5,6 @@
package kotlin.collections
import kotlin.wasm.internal.copyTo
private inline fun rangeCheck(index: Int, count: Int, arraySize: Int) {
if (index < 0 || count < 0 || index + count > arraySize) throw IndexOutOfBoundsException()
}
internal inline fun arrayCopy(array: ByteArray, fromIndex: Int, destination: ByteArray, toIndex: Int, count: Int) {
val srcStorage = array.storage
rangeCheck(fromIndex, count, srcStorage.len())
val dstStorage = destination.storage
rangeCheck(toIndex, count, dstStorage.len())
srcStorage.copyTo(dstStorage, fromIndex, toIndex, count)
}
internal inline fun arrayCopy(array: ShortArray, fromIndex: Int, destination: ShortArray, toIndex: Int, count: Int) {
val srcStorage = array.storage
rangeCheck(fromIndex, count, srcStorage.len())
val dstStorage = destination.storage
rangeCheck(toIndex, count, dstStorage.len())
srcStorage.copyTo(dstStorage, fromIndex, toIndex, count)
}
internal inline fun arrayCopy(array: CharArray, fromIndex: Int, destination: CharArray, toIndex: Int, count: Int) {
val srcStorage = array.storage
rangeCheck(fromIndex, count, srcStorage.len())
val dstStorage = destination.storage
rangeCheck(toIndex, count, dstStorage.len())
srcStorage.copyTo(dstStorage, fromIndex, toIndex, count)
}
internal inline fun arrayCopy(array: IntArray, fromIndex: Int, destination: IntArray, toIndex: Int, count: Int) {
val srcStorage = array.storage
rangeCheck(fromIndex, count, srcStorage.len())
val dstStorage = destination.storage
rangeCheck(toIndex, count, dstStorage.len())
srcStorage.copyTo(dstStorage, fromIndex, toIndex, count)
}
internal inline fun arrayCopy(array: LongArray, fromIndex: Int, destination: LongArray, toIndex: Int, count: Int) {
val srcStorage = array.storage
rangeCheck(fromIndex, count, srcStorage.len())
val dstStorage = destination.storage
rangeCheck(toIndex, count, dstStorage.len())
srcStorage.copyTo(dstStorage, fromIndex, toIndex, count)
}
internal inline fun arrayCopy(array: FloatArray, fromIndex: Int, destination: FloatArray, toIndex: Int, count: Int) {
val srcStorage = array.storage
rangeCheck(fromIndex, count, srcStorage.len())
val dstStorage = destination.storage
rangeCheck(toIndex, count, dstStorage.len())
srcStorage.copyTo(dstStorage, fromIndex, toIndex, count)
}
internal inline fun arrayCopy(array: DoubleArray, fromIndex: Int, destination: DoubleArray, toIndex: Int, count: Int) {
val srcStorage = array.storage
rangeCheck(fromIndex, count, srcStorage.len())
val dstStorage = destination.storage
rangeCheck(toIndex, count, dstStorage.len())
srcStorage.copyTo(dstStorage, fromIndex, toIndex, count)
}
internal inline fun arrayCopy(array: BooleanArray, fromIndex: Int, destination: BooleanArray, toIndex: Int, count: Int) {
val srcStorage = array.storage
rangeCheck(fromIndex, count, srcStorage.len())
val dstStorage = destination.storage
rangeCheck(toIndex, count, dstStorage.len())
srcStorage.copyTo(dstStorage, fromIndex, toIndex, count)
}
internal inline fun arrayCopy(array: Array<Any?>, fromIndex: Int, destination: Array<Any?>, toIndex: Int, count: Int) {
val srcStorage = array.storage
rangeCheck(fromIndex, count, srcStorage.len())
val dstStorage = destination.storage
rangeCheck(toIndex, count, dstStorage.len())
srcStorage.copyTo(dstStorage, fromIndex, toIndex, count)
}
/**
* Returns a *typed* array containing all of the elements of this collection.
*
@@ -8,13 +8,13 @@ package kotlin.text
import kotlin.wasm.internal.*
internal fun insertString(array: CharArray, destinationIndex: Int, value: String, sourceIndex: Int, count: Int): Int {
value.chars.copyTo(array.storage, sourceIndex, destinationIndex, count)
copyWasmArray(value.chars, array.storage, sourceIndex, destinationIndex, count)
return count
}
internal fun unsafeStringFromCharArray(array: CharArray, start: Int, size: Int): String {
val copy = WasmCharArray(size)
array.storage.copyTo(copy, start, 0, size)
copyWasmArray(array.storage, copy, start, 0, size)
return String.unsafeFromCharArray(copy)
}
@@ -74,7 +74,7 @@ public actual fun String(chars: CharArray, offset: Int, length: Int): String {
throw IndexOutOfBoundsException()
val copy = WasmCharArray(length)
chars.storage.copyTo(copy, offset, 0, length)
copyWasmArray(chars.storage, copy, offset, 0, length)
return String.unsafeFromCharArray(copy)
}
@@ -87,7 +87,7 @@ public actual fun CharArray.concatToString(): String {
val thisStorage = this.storage
val thisLength = thisStorage.len()
val copy = WasmCharArray(thisLength)
this.storage.copyTo(copy, 0, 0, thisLength)
copyWasmArray(this.storage, copy, 0, 0, thisLength)
return String.unsafeFromCharArray(copy)
}
@@ -108,7 +108,7 @@ public actual fun CharArray.concatToString(startIndex: Int = 0, endIndex: Int =
val length = endIndex - startIndex
val copy = WasmCharArray(length)
this.storage.copyTo(copy, startIndex, 0, length)
copyWasmArray(this.storage, copy, startIndex, 0, length)
return String.unsafeFromCharArray(copy)
}
@@ -121,7 +121,7 @@ public actual fun String.toCharArray(): CharArray {
val thisChars = this.chars
val thisLength = thisChars.len()
val newArray = CharArray(thisLength)
thisChars.copyTo(newArray.storage, 0, 0, thisLength)
copyWasmArray(thisChars, newArray.storage, 0, 0, thisLength)
return newArray
}
@@ -141,7 +141,7 @@ public actual fun String.toCharArray(startIndex: Int = 0, endIndex: Int = this.l
AbstractList.checkBoundsIndexes(startIndex, endIndex, length)
val newLength = endIndex - startIndex
val newArray = CharArray(newLength)
this.chars.copyTo(newArray.storage, startIndex, 0, newLength)
copyWasmArray(this.chars, newArray.storage, startIndex, 0, newLength)
return newArray
}
@@ -919,6 +919,17 @@ object ArrayOps : TemplateGroupBase() {
"""
}
}
on(Backend.Wasm) {
body {
"""
AbstractList.checkRangeIndexes(startIndex, endIndex, this.size)
val rangeSize = endIndex - startIndex
AbstractList.checkRangeIndexes(destinationOffset, destinationOffset + rangeSize, destination.size)
kotlin.wasm.internal.copyWasmArray(this.storage, destination.storage, startIndex, destinationOffset, rangeSize)
return destination
"""
}
}
}
}
}