Description
Motivation
Long ago I noticed that currently even constant strings are routed through entire encoding/decoding process on each pass between Rust and JS, becoming unnecessarily expensive part of the resulting code.
Now I've noticed that this affects even internal functions which need intermediate strings, thus affecting even more potentially hot paths.
E.g. this is a profile from my WASM library that heavily relies on JS iteration - js_sys::try_iter
- which, in turn, recreated same constant string for the iteration protocol over and over.
We can fix this particular function but, ideally, we should have a more generic solution for constant strings.
Proposed Solution
Either:
- Add a step to the postprocessor to walk over the generated WASM, detect calls to
JsValue::from_str
with pointers to a data section and statically extract these into JS. - Do the same in runtime by using a cache backed by
lazy_static!
(but then we need to somehow detect whether a string is in the data section, which is a bit harder in runtime but not impossible). - Add some sort of a proc-macro
js_const_str!("...")
that would extract these statically like (1), although could make it easier to detect by being more expicit.
All of the proposed solutions require choosing the right balance between complexity of the solution itself vs complexity of usage from API point of view (we don't want to add a function that most users would forget to use).