3 ms·
It's great that the rust community are finding ways to improve the performance of decoding strings from WASM to js, it's one of the major performance holes you
by samwillis 1y ago
It's great that the rust community are finding ways to improve the performance of decoding strings from WASM to js, it's one of the major performance holes you hit when using WASM.
The issue comes down to the fact that even if your WASM code can return a utf16 buffer, to use it as a string in JS code the engine needs to make a copy at some point. The TextDecoder api does a first good job of making this efficient, ensuring there is just a single copy, but it's still overhead.
Ideally there should be a way to wrap an array buffer with a "String View", offloading the responsibility of ensuring its utf16 to the WASM code, and there being no copy made. But that brings a ton of complexities as strings need to be immutable in js, but the underlying buffer could still be changed.
- breve 1y agoThe JS string built-ins proposal for WebAssembly: https://github.com/WebAssembly/js-string-builtins/blob/main/proposals/js-string-builtins/Overview.md https://github.com/WebAssembly/js-string-builtins/blob/main/...
- samwillis 1y agoPersonally I feel this is backwards - I don't want access to js literals and objects from WASM, I just want a way to wrap an arbitrary array buffer that contains a utf16 string as a js string. It keeps WASM simple and provides a thin layer as an optimisation.
- deleted 1y ago[deleted]
- vanderZwan 1y ago> It keeps WASM simple At the cost of complicating JS string implementations, probably to the point of undoing the benefits. Currently JS strings are immutable objects, allowing for all kinds of optimization tricks (interning, ropes, etc.). Having one string represented by a mutable arraybuffer messes with that. There's probably also security concerns with allowing mutable access to string internals inside the JS engine side. So the simple-appearing solution you suggested would be rejected all major browser vendors who back the various WASM and JS engines. Access to constant JS strings without any form of mutability is the only realistic option for accessing JS strings. And creating constant strings is the only one for sending them back.
- deleted 1y ago[deleted]