-
Notifications
You must be signed in to change notification settings - Fork 698
Description
Hi,
lets imagine I have some 1024 f32 values stored in some Float32Array buffer, waiting to be processed by WASM based DSP code :)
I'm still pretty new to WebAssembly. I understood that you can pass only typed numeric values as arguments to exported WASM functions. That makes sense to me and that's why I decided to pass my data using memory. I'm fine with that as well...
So, to pass my 1024 values, I assign them to the .memory directly. Like:
const mem = exports.memory.buffer;
const F32 = new Float32Array(mem);
F32[0] = 31337.777;
This is fun, but to assign all my values, I have to loop over all of the values to assign them all to the memory. Well, somehow that feels wrong performance-wise. I would have expected a native impl. to do that for me. e.g. a key like data in the memoryDescriptor argument of the WebAssembly.Memory constructor allowing to initialize the memory with an ArrayBuffer.
So, well, then I do my WASM function call and when the WASM impl. did it's magic, it's writing the result values back to the memory. This is happening inside of the DSP loop, so there is no overhead inside my WASM code to do that - as far as I can see.
But now, after I'm back in JS context, I have to iterate over the whole memory again just to read all the values and construct another JS based data representation. And somehow I expected a native impl. to be present for this purpose as well. Maybe something like memory.read(Float32Array) to return me the buffer data as a Float32Array, abstracting the pointer and iteration maze.
Am I missing something?
Is there a better way to pass large amounts of data from/to WASM that I just overlooked?
Thanks in advance and best,
Aron