Replies: 1 comment 3 replies
-
|
Nice idea! I think moving string constants from wasm to JS (when they are only needed by JS) is a really nice idea. However, I think it might depend on relocation information being understood by binaryan. As of today binaryen cannot actually ever remove data from within a data segment (since it cannot move data around in memory after wasm-ld has placed it). Even if we link with without segment merging and each string is in its own segment, all binaryen can do is delete the segment, it cannot compress the memory or reclaim the addresses used for the string data. My guess is that that solution is to link with |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Here is something that I have been pondering on/off if might be possible with some suitable magic..
Given
a.cliba.jsIs there some kind of compiler codegen magic we could do, to recognize when a JS function is called with only a constant literal value, that we'd transform the literal over to JS side, and let Closure compiler optimize the resulting code?
I.e. the above code would effectively get optimized into a form:
liba.jsThis example is maybe a bit silly and contrived. Expanding, maybe in the case of strings, one might have:
a.cliba.jspossibly optimized into
a.cliba.jsto avoid having to run the
UTF8ToString()marshalling over the language boundary.If multiple string literals were used, e.g.
a.cliba.jsthis could theoretically become
a.cliba.jsif a suitably intelligent constant propagation would be able to track the multiple values.
I have been writing a toy JSON serializer/deserializer utility to experiment if I could completely replace C/C++ JSON libraries with a mechanism that would reuse JavaScript's JSON capabilities, and there I find that I am doing quite a lot of small string marshalling from Wasm -> JS, where the marshalled strings are all commonly literals, e.g.
to marshal a struct with some integer fields.
In this scenario, there can be thousands to tens of thousands of such calls to
json_get_*(json_object obj, const char *member_field), wheremember_fieldis generally a compile-time literal.. this results in a massive number of runtimeUTF8ToString()calls.And given that this is general purpose JSON deserializer code, I cannot hardcode these strings to JS side either.
So if we had a pass that recognized constants (and constant strings?) in Wasm side, and was able to move those to the JS side (and then remove the wasm side strings from the static data), that would improve this kind of use case.
There are also other use cases in e.g. https://github.com/juj/wasm_webgpu struct marshalling, that might be interesting for this.
Any thoughts if this kind of Wasm-JS boundary optimization might be feasible somehow? CC @kripken, @dschuff, @sbc100, @tlively
Beta Was this translation helpful? Give feedback.
All reactions