Alexey Zolotarev
02/18/2025, 8:07 AMMichael Paus
02/18/2025, 9:28 AMAlexey Zolotarev
02/18/2025, 9:35 AMMichael Paus
02/18/2025, 9:41 AMAlexey Zolotarev
02/18/2025, 9:42 AMMichael Paus
02/18/2025, 9:42 AMAlexey Zolotarev
02/18/2025, 9:45 AMephemient
02/18/2025, 9:57 AMString
etc.) all live in GC memoryephemient
02/18/2025, 9:58 AMMichael Paus
02/18/2025, 10:01 AMAlexey Zolotarev
02/18/2025, 10:06 AMyou could create a structure in linear memory and only interact with it through pointers but you'd have to copy to GC in order to use standard Kotlin functionsYes, this I understand. I'm wondering if there might be some sort of way (e.g. with cinterop maybe) to implement wrappers around objects allocated in the linear memory that would act as native Kotlin types. E.g. Go has
unsafe.Pointer
that can be used for that purpose. Of course, with Go/Wasm, everything is allocated in linear memory.Alexey Zolotarev
02/18/2025, 10:12 AMByteArray
, another to convert the byte array into a String
. This becomes a problem when the data is large.
There are some options like implementing streaming with some smaller buffer or using Wasi files but it still requires making copies.Alexey Zolotarev
02/18/2025, 10:17 AMByteArray
mapped to a linear memory. Even JS has this option with Uint8Array
.ephemient
02/18/2025, 10:19 AMByteArray
ephemient
02/18/2025, 10:20 AMAlexey Zolotarev
02/18/2025, 10:23 AMyou could create your own (as a pointer + length structure) but "bytearray whose storage may be deallocated" cannot be aYes, and that means I can't use any functions that operate on byte arrays so this wouldn't achieve much unfortunately.ByteArray
Charlie Tapping
02/19/2025, 11:49 AMAlexey Zolotarev
02/19/2025, 1:34 PMSay you have a function like the following which you compile to WebAssembly:
```kotlin
fun concat(input: String): String
```
The compiler will either output:In case of Kotlin as a guest it would not compile such function since
@WasmImport
and @WasmExport
only support primitive types that can be passed through the stack. I.e
@WasmExport("process")
fun process(input: String)
> Unsupported @WasmImport and @WasmExport return type String
But it's ok, say we define the function that takes a pointer and length instead of the string (actually it would be more involved than this with Kotlin since memory can't be directly allocated from the host to copy the input string to).
Now when the function is called, the guest can read the string from the linear memory at the specified location but it would have to first read it into a ByteArray
and then decode it to String
, hence 3 copies of the data are made.
I can implement a function that would decode the string from the linear memory directly I guess but in general the problem is that I can't use most of the stdlib (or many other libs) functions directly for data in the linear memory without copying it out into some builtin type first.
Except maybe <http://kotlin.io|kotlin.io>
for which RawSource
over linear memory can be implemented most likely 🙂Charlie Tapping
02/19/2025, 1:55 PMAlexey Zolotarev
02/19/2025, 2:07 PM