New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Allow to access typed arrays by reference instead of copy #47
Comments
Ah! Thanks for bringing this up. Yeah currently we are copying because of the context in: #9 But looking at AssemblyScript/assemblyscript#1047. I'm not too sure if this would introduce breaking changes. cc @MaxGraey , do you happen to know if the removing these |
Also, I don't want to slow you down @letmaik , if your comfortable, AsBind essentially wraps around the AS loader, and makes it more convenient. I don't know what project you are working on (would be super interested to hear if that's okay! No worries if not! 😄 ), but if you don't mind getting a bit more lower level with passing types around, feel free to check that out as well because then you can choose between copying vs getting a direct view 😄 |
|
wasmExports.__getInt8Array(responseRef).slice(); This redundant. It should be just |
This is for a hobby project, basically my first emulator (for a simple system to start with, CHIP-8), and I'd like to use AS because it seems really nice :) So there's definitely no urgency here but I'd like to do it the proper way as if it was a performance-critical piece of code, while also staying as high-level as possible. That's why I'm looking at as-bind to remove the nasty details at the boundary.
I'd like to understand this a bit better. As long as the typed array object exists in wasm memory, it is safe to use it in JS, right? Or is there a case where an existing object can get relocated to a different location in linear memory? |
using const arrPtr = wasm.someProcessing();
let typedArray = wasm.__getUint8ArrayView(arrPtr);
pureJSProcessing(typedArray); // don't call any methods from wasm. Just JS routine
wasm.__release(arrPtr); // free resource It could work properly. But if I hope this is clearer now =) |
I found a description of the same issue here: emscripten-core/emscripten#6747 I think as-bind should allow to get an unsafe view on array buffers. This could be opt-in by setting some config option similar to the type caching option: asBindInstance.exports.myFunction.returnUnsafeArrayViews = true; // default false
// or globally:
asBindInstance.returnUnsafeArrayViews = true; The docs should mention that by default a copy is returned, but advanced users can opt in to get a view instead, together with all the caveats, e.g. the array buffer must still be alive/referenced inside wasm (since the reference count will not be increased when returning the buffer), and no further wasm function should be invoked (to avoid triggering memory growth which would detach the view). |
Ah for sure! Thanks for catching that! Yeah I remember we used to need it for some reason 🤔
Thank you for clarifying! Yeah I remember bumping into this a while back, but then we fixed it in the loader 😄
Oh nice! Yeah emulators are fun! That's super awesome! 😄 🎉
Yeah, so we can totally add this option on functions, I think it'd work! Though, I'll probably think of a different name. Probably like The change mostly needs to be done here: https://github.com/torch2424/as-bind/blob/master/lib/asbind-instance/supported-ref-types.js#L127 Providing an Actually, that's super easy. I'll just do that now 😂 GIve me like 10 minutes 😄 |
Should be live in |
@torch2424 Great! Just out of curiosity, why does it return the ptr and the array? What can you do with the ptr? |
@letmaik Yay! Glad it's working for you! I returned them both for a few reasons:
I was hoenstly a bit 50/50 if it should return the pointer. But I figured more good than harm could come from it 😀 |
Returning a typed array from AS to JS seems to make a copy of it. However, I'd like to obtain a view on the same underlying ArrayBuffer. Looks like
.slice()
is responsible for the copy:as-bind/lib/asbind-instance/supported-ref-types.js
Line 29 in 9ab3ece
The text was updated successfully, but these errors were encountered: