bernd
05/21/2024, 11:05 AM@JsExport
fun fillSlow(n: Int): Int32Array {
val array = Int32Array(n)
for (i in 0..<n) {
array[i] = i
}
return array
}
Calling the wasm-function fillSlow
from js takes 4-5 times as long as running the function in js directly. My guess is that's because to write it makes n
calls back into the imported js-function 'org.khronos.webgl.setMethodImplForInt32Array' : (obj, index, value) => { obj[index] = value; }
, and the wasm->js overhead is that expensive. Fair enough:
@JsExport
fun fillFast(n: Int): Int32Array = withScopedMemoryAllocator { allocator ->
val p = allocator.allocate(n * 4)
var addr = p.address.toInt()
for (i in 0..<n) {
p.storeInt(i)
addr += 4
}
importArrayFromMem(p.address.toInt(), n)
}
fun importArrayFromMem(address: Int, length: Int): Int32Array =
js("new Int32Array(wasmExports.memory.buffer, address, length)")
Calling the wasm-function fillFast
from js is sufficiently fast, in fact 2-3x faster than even running fillSlow
in js directly. It does all the operations that I think should be required to do the same work: compute an offset (addr +=4
) and store a value (p.storeInt
). It is obviously not producing the right result however, as it keeps writing to the same address, i.e. to the beginning of the array. Unfortunately, changing this to (p+addr).storeInt(i)
immediately make it extremely slow, 2-3x slower than running fillSlow
in wasm and over 10x slower than running fillSlow
in js directly.
The main reason for the slowdown appear to be creation of all the Pointer
-objects that are required in order to call storeInt
. Even if I remove the store/add-instructions and only create a new pointer in every iteration, the function remains this slow.
Is there perhaps another way that allows writes to memory without having to create all those pointer-objects ? Thanks 🙂
(I test on kotlin multiplatform 1.9.24 and Chrome)Svyatoslav Kuzmich [JB]
05/21/2024, 11:30 AM(p+addr).storeInt(i)
should be fast and should not create any Pointer objects (it is a value
class). Are you running a “production” or a “development” build?bernd
05/21/2024, 11:32 AMbernd
05/21/2024, 11:34 AMUint
somehow: if I change the loop to
var addr = p.address
for (i in 0..<n) {
addr += 4u
}
it's still way slower than when incrementing the IntSvyatoslav Kuzmich [JB]
05/21/2024, 11:48 AM@Suppress("INVISIBLE_MEMBER", "INVISIBLE_REFERENCE")
@kotlin.wasm.internal.WasmOp(kotlin.wasm.internal.WasmOp.I32_STORE)
public fun myStore(addr: Int, value: Int): Unit =
error("intrinsic")
bernd
05/21/2024, 11:50 AMbernd
05/21/2024, 11:58 AMbernd
05/21/2024, 12:27 PM