-
Notifications
You must be signed in to change notification settings - Fork 3.3k
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
Add option to inline webworker #9796
Comments
I'd be interested to know if this helps overall load. It would make the initial download slower, but maybe it's worth it later? The webpack benefits may be worth it, though. A downside is that this makes debugging harder (can't modify the source) so we have to keep the current approach around, which means this would be a new option. |
Agreed.
Applying some very basic minification (just whitespace+comments) added 3.13KB to the JS. I'm sure that can be reduced further by smarter minification. |
I took a brief look at this, and here's my idea so far:
shared.py:
emcc.py from around here: Line 2374 in 6d297f6
Does this seem like it's on the right path? |
By macro substitution do you mean the JS preprocessor? There's a helper function However I'd recommend measuring this first to see how beneficial it is - I'm not sure it's worth an extra option yet, but I'm curious to see some data. |
I guess my motivation was more about making pthreads builds easier/possible to webpack. |
This issue has been automatically marked as stale because there has been no activity in the past year. It will be closed automatically if no further activity occurs in the next 30 days. Feel free to re-open at any time if this issue is still relevant. |
I badly need this for a restricted environment where external files are not allowed. It's like the |
I just created a NodeJS script to inline the worker and it works, but would be better to add it to compiler it-self.
|
I'm glad you've got something working. I know of one other project that's doing something similar, so I still think there's value of adding this to Emscripten. I'm just not going to be the one to do it. |
Cross-commenting here too - rather than using Blobs or inlining worker JS as string in other ways, I think a better solution is to actually inline Worker's code as normal JS into the main one in Then, it's possible to detect whether the code is running as main JS or as worker JS, and branch correspondingly. Such condition can be a special GET param (then Worker could be created as |
Sounds good to me. Any reason no to do make this the default? It would simplify deployments to not need the extra file. The only down I can think of is the marginal increase in JS codesize used on the main thread... but that is marginal and fixed and seem worth it given the amount if simplification we would get. It also get confused about how the main js file is loaded into the pthread workers (I think we use eval under node.. which is kind of scary) |
I mean any reason not to make this the only way to do things? |
With ES6 we should just use
Mostly the size increase you noted, which, in turn, means slower download of the initial JS. But I guess you're right that it doesn't matter too much, especially since the application is likely to need that worker JS before the app start anyway (for the PTHREAD_POOL mode). |
I think this will make the threads more heavy-weight. I suspect that this will also have a negative effect on JS optimisation, but actual profiling will be needed to know if it is worth worrying about. |
What makes you say it will make threads more heavy-weight? IIUC it will make the amount of code loaded onto a worker strictly less since since the code needed to load the main js file inside the worker.js file (the whole |
Back when I was experimenting with getting SINGLE_FILE working with workers ~2 years ago I found it drastically increased startup time. Things have obviously changes since then, and @RReverser's approach is likely to perform differently. I can't seem to find the results of the profiling I previously did, but I did have a hard drive die sine then, so it may be lost. |
Yeah it does sound like it's the added overhead from using Blobs with JS code as strings, which behave quite differently than normal URLs in terms of caching and sharing resources. I think Worker logic inlined in the main file should behave much better in this regard. |
Ran into this in Yarn as well, in our case the CLI is bundled as a single JS file so we can't have it try to load itself nor any other file. The only option for us would be to embed the worker as a string and pass it directly to the If the output was Webpack friendly we could handle this on our end |
It would be a good idea to implement this with the SINGLE_FILE setting instead of creating a new one, else... well it won't produce a single file ¯\_(ツ)_/¯ |
has this gotten anywhere or is it dead? |
This method has many advantages over the previous method of generating a separate file: - Avoids separate output file simplifying deployment. - Avoids confusing laying of global scopes - Avoids exporting symbols on the Module simply for visibility within the worker file. - Avoids code duplication - Avoids the needs to importScripts call, and the node polyfill for this. - Allows optimizers such as closure and JSDCE to operate on the combined code. - `-sSINGLE_FILE` now works with pthreads - Fewer network requests - No need for locateFile logic to run on the worker to find the worker.js Fixes: emscripten-core#9796
This method has many advantages over the previous method of generating a separate file: - Avoids separate output file simplifying deployment. - Avoids confusing laying of global scopes - Avoids exporting symbols on the Module simply for visibility within the worker file. - Avoids code duplication - Avoids the needs to importScripts call, and the node polyfill for this. - Allows optimizers such as closure and JSDCE to operate on the combined code. - `-sSINGLE_FILE` now works with pthreads - Fewer network requests - No need for locateFile logic to run on the worker to find the worker.js Fixes: emscripten-core#9796
This method has many advantages over the previous method of generating a separate file: - Avoids separate output file simplifying deployment. - Avoids confusing laying of global scopes - Avoids exporting symbols on the Module simply for visibility within the worker file. - Avoids code duplication - Avoids the needs to importScripts call, and the node polyfill for this. - Allows optimizers such as closure and JSDCE to operate on the combined code. - `-sSINGLE_FILE` now works with pthreads - Fewer network requests - No need for locateFile logic to run on the worker to find the worker.js Fixes: emscripten-core#9796
This method has many advantages over the previous method of generating a separate file: - Avoids separate output file simplifying deployment. - Avoids confusing laying of global scopes - Avoids exporting symbols on the Module simply for visibility within the worker file. - Avoids code duplication - Avoids the needs to importScripts call, and the node polyfill for this. - Allows optimizers such as closure and JSDCE to operate on the combined code. - `-sSINGLE_FILE` now works with pthreads - Fewer network requests - No need for locateFile logic to run on the worker to find the worker.js Fixes: emscripten-core#9796
This method has many advantages over the previous method of generating a separate file: - Avoids separate output file simplifying deployment. - Avoids confusing laying of global scopes - Avoids exporting symbols on the Module simply for visibility within the worker file. - Avoids code duplication - Avoids the needs to importScripts call, and the node polyfill for this. - Allows optimizers such as closure and JSDCE to operate on the combined code. - `-sSINGLE_FILE` now works with pthreads - Fewer network requests - No need for locateFile logic to run on the worker to find the worker.js Fixes: emscripten-core#9796
This method has many advantages over the previous method of generating a separate file: - Avoids separate output file simplifying deployment. - Avoids confusing laying of global scopes - Avoids exporting symbols on the Module simply for visibility within the worker file. - Avoids code duplication - Avoids the needs to importScripts call, and the node polyfill for this. - Allows optimizers such as closure and JSDCE to operate on the combined code. - `-sSINGLE_FILE` now works with pthreads - Fewer network requests - No need for locateFile logic to run on the worker to find the worker.js The test_pthread_custom_pthread_main_url test was completely removed since it was testing how worker.js was located and worker.js no longer exists. Fixes: emscripten-core#9796
This method has many advantages over the previous method of generating a separate file: - Avoids separate output file simplifying deployment. - Avoids confusing laying of global scopes - Avoids exporting symbols on the Module simply for visibility within the worker file. - Avoids code duplication - Avoids the needs to importScripts call, and the node polyfill for this. - Allows optimizers such as closure and JSDCE to operate on the combined code. - `-sSINGLE_FILE` now works with pthreads - Fewer network requests - No need for locateFile logic to run on the worker to find the worker.js The test_pthread_custom_pthread_main_url test was completely removed since it was testing how worker.js was located and worker.js no longer exists. Fixes: emscripten-core#9796
This method has many advantages over the previous method of generating a separate file: - Avoids separate output file simplifying deployment. - Avoids confusing laying of global scopes - Avoids exporting symbols on the Module simply for visibility within the worker file. - Avoids code duplication - Avoids the needs to importScripts call, and the node polyfill for this. - Allows optimizers such as closure and JSDCE to operate on the combined code. - `-sSINGLE_FILE` now works with pthreads - Fewer network requests - No need for locateFile logic to run on the worker to find the worker.js The test_pthread_custom_pthread_main_url test was completely removed since it was testing how worker.js was located and worker.js no longer exists. Fixes: emscripten-core#9796
This method has many advantages over the previous method of generating a separate file: - Avoids separate output file simplifying deployment. - Avoids confusing laying of global scopes - Avoids exporting symbols on the Module simply for visibility within the worker file. - Avoids code duplication - Avoids the needs to importScripts call, and the node polyfill for this. - Allows optimizers such as closure and JSDCE to operate on the combined code. - `-sSINGLE_FILE` now works with pthreads - Fewer network requests - No need for locateFile logic to run on the worker to find the worker.js The test_pthread_custom_pthread_main_url test was completely removed since it was testing how worker.js was located and worker.js no longer exists. Fixes: emscripten-core#9796
This method has many advantages over the previous method of generating a separate file: - Avoids separate output file simplifying deployment. - Avoids confusing laying of global scopes - Avoids exporting symbols on the Module simply for visibility within the worker file. - Avoids code duplication - Avoids the needs to importScripts call, and the node polyfill for this. - Allows optimizers such as closure and JSDCE to operate on the combined code. - `-sSINGLE_FILE` now works with pthreads - Fewer network requests - No need for locateFile logic to run on the worker to find the worker.js The test_pthread_custom_pthread_main_url test was completely removed since it was testing how worker.js was located and worker.js no longer exists. Fixes: emscripten-core#9796
This method has many advantages over the previous method of generating a separate file: - Avoids separate output file simplifying deployment. - Avoids confusing laying of global scopes - Avoids exporting symbols on the Module simply for visibility within the worker file. - Avoids code duplication - Avoids the needs to importScripts call, and the node polyfill for this. - Allows optimizers such as closure and JSDCE to operate on the combined code. - `-sSINGLE_FILE` now works with pthreads - Fewer network requests - No need for locateFile logic to run on the worker to find the worker.js The test_pthread_custom_pthread_main_url test was completely removed since it was testing how worker.js was located and worker.js no longer exists. Fixes: emscripten-core#9796
This method has many advantages over the previous method of generating a separate file: - Avoids separate output file simplifying deployment. - Avoids confusing laying of global scopes - Avoids exporting symbols on the Module simply for visibility within the worker file. - Avoids code duplication - Avoids the needs to importScripts call, and the node polyfill for this. - Allows optimizers such as closure and JSDCE to operate on the combined code. - `-sSINGLE_FILE` now works with pthreads - Fewer network requests - No need for locateFile logic to run on the worker to find the worker.js The test_pthread_custom_pthread_main_url test was completely removed since it was testing how worker.js was located and worker.js no longer exists. Fixes: emscripten-core#9796
This method has many advantages over the previous method of generating a separate file: - Avoids separate output file simplifying deployment. - Avoids confusing laying of global scopes - Avoids exporting symbols on the Module simply for visibility within the worker file. - Avoids code duplication - Avoids the needs to importScripts call, and the node polyfill for this. - Allows optimizers such as closure and JSDCE to operate on the combined code. - `-sSINGLE_FILE` now works with pthreads - Fewer network requests - No need for locateFile logic to run on the worker to find the worker.js The test_pthread_custom_pthread_main_url test was completely removed since it was testing how worker.js was located and worker.js no longer exists. Fixes: emscripten-core#9796
This method has many advantages over the previous method of generating a separate file: - Avoids separate output file simplifying deployment. - Avoids confusing laying of global scopes - Avoids exporting symbols on the Module simply for visibility within the worker file. - Avoids code duplication - Avoids the needs to importScripts call, and the node polyfill for this. - Allows optimizers such as closure and JSDCE to operate on the combined code. - `-sSINGLE_FILE` now works with pthreads - Fewer network requests - No need for locateFile logic to run on the worker to find the worker.js The test_pthread_custom_pthread_main_url test was completely removed since it was testing how worker.js was located and worker.js no longer exists. Fixes: emscripten-core#9796
This method has many advantages over the previous method of generating a separate file: - Avoids separate output file simplifying deployment. - Avoids confusing laying of global scopes - Avoids exporting symbols on the Module simply for visibility within the worker file. - Avoids code duplication - Avoids the needs to importScripts call, and the node polyfill for this. - Allows optimizers such as closure and JSDCE to operate on the combined code. - `-sSINGLE_FILE` now works with pthreads - Fewer network requests - No need for locateFile logic to run on the worker to find the worker.js The test_pthread_custom_pthread_main_url test was completely removed since it was testing how worker.js was located and worker.js no longer exists. Fixes: emscripten-core#9796
This method has many advantages over the previous method of generating a separate file: - Avoids separate output file simplifying deployment. - Avoids confusing laying of global scopes - Avoids exporting symbols on the Module simply for visibility within the worker file. - Avoids code duplication - Avoids the needs to importScripts call, and the node polyfill for this. - Allows optimizers such as closure and JSDCE to operate on the combined code. - `-sSINGLE_FILE` now works with pthreads - Fewer network requests - No need for locateFile logic to run on the worker to find the worker.js The test_pthread_custom_pthread_main_url test was completely removed since it was testing how worker.js was located and worker.js no longer exists. Fixes: emscripten-core#9796
This method has many advantages over the previous method of generating a separate file: - Avoids separate output file simplifying deployment. - Avoids confusing laying of global scopes - Avoids exporting symbols on the Module simply for visibility within the worker file. - Avoids code duplication - Avoids the needs to importScripts call, and the node polyfill for this. - Allows optimizers such as closure and JSDCE to operate on the combined code. - `-sSINGLE_FILE` now works with pthreads - Fewer network requests - No need for locateFile logic to run on the worker to find the worker.js The test_pthread_custom_pthread_main_url test was completely removed since it was testing how worker.js was located and worker.js no longer exists. Fixes: emscripten-core#9796
This method has many advantages over the previous method of generating a separate file: - Avoids separate output file simplifying deployment. - Avoids confusing laying of global scopes - Avoids exporting symbols on the Module simply for visibility within the worker file. - Avoids code duplication - Avoids the needs to importScripts call, and the node polyfill for this. - Allows optimizers such as closure and JSDCE to operate on the combined code. - `-sSINGLE_FILE` now works with pthreads - Fewer network requests - No need for locateFile logic to run on the worker to find the worker.js The test_pthread_custom_pthread_main_url test was completely removed since it was testing how worker.js was located and worker.js no longer exists. test_pthread_safe_stack depends on `onAbort` being proxied back to the main thread. In this case the `onAbort` handler is injected conditionally in `--pre-js=browser_report.js`. In the previous code this meant that the proxied version took precedence because the pthread handler override was injected first. test_pthread_asan_use_after_free_2_wasmfs depends on `printErr` not being proxied back to the main thread. It is injected unconditionally during `--pre-js`. In the previous code path this means that non-proxied version took precedence because it overrode the incoming pthread handler. Fixes: emscripten-core#9796
This method has many advantages over the previous method of generating a separate file: - Avoids separate output file simplifying deployment. - Avoids confusing laying of global scopes - Avoids exporting symbols on the Module simply for visibility within the worker file. - Avoids code duplication - Avoids the needs to importScripts call, and the node polyfill for this. - Allows optimizers such as closure and JSDCE to operate on the combined code. - `-sSINGLE_FILE` now works with pthreads - Fewer network requests - No need for locateFile logic to run on the worker to find the worker.js The test_pthread_custom_pthread_main_url test was completely removed since it was testing how worker.js was located and worker.js no longer exists. test_pthread_safe_stack depends on `onAbort` being proxied back to the main thread. In this case the `onAbort` handler is injected conditionally in `--pre-js=browser_report.js`. In the previous code this meant that the proxied version took precedence because the pthread handler override was injected first. test_pthread_asan_use_after_free_2_wasmfs depends on `printErr` not being proxied back to the main thread. It is injected unconditionally during `--pre-js`. In the previous code path this means that non-proxied version took precedence because it overrode the incoming pthread handler. Fixes: emscripten-core#9796
This method has many advantages over the previous method of generating a separate file: - Avoids separate output file simplifying deployment. - Avoids confusing laying of global scopes - Avoids exporting symbols on the Module simply for visibility within the worker file. - Avoids code duplication - Avoids the needs to importScripts call, and the node polyfill for this. - Allows optimizers such as closure and JSDCE to operate on the combined code. - `-sSINGLE_FILE` now works with pthreads - Fewer network requests - No need for locateFile logic to run on the worker to find the worker.js The test_pthread_custom_pthread_main_url test was completely removed since it was testing how worker.js was located and worker.js no longer exists. test_pthread_safe_stack depends on `onAbort` being proxied back to the main thread. In this case the `onAbort` handler is injected conditionally in `--pre-js=browser_report.js`. In the previous code this meant that the proxied version took precedence because the pthread handler override was injected first. test_pthread_asan_use_after_free_2_wasmfs depends on `printErr` not being proxied back to the main thread. It is injected unconditionally during `--pre-js`. In the previous code path this means that non-proxied version took precedence because it overrode the incoming pthread handler. Fixes: emscripten-core#9796
This method has many advantages over the previous method of generating a separate file: - Avoids separate output file simplifying deployment. - Avoids confusing laying of global scopes - Avoids exporting symbols on the Module simply for visibility within the worker file. - Avoids code duplication - Avoids the needs to importScripts call, and the node polyfill for this. - Allows optimizers such as closure and JSDCE to operate on the combined code. - `-sSINGLE_FILE` now works with pthreads - Fewer network requests - No need for locateFile logic to run on the worker to find the worker.js The test_pthread_custom_pthread_main_url test was completely removed since it was testing how worker.js was located and worker.js no longer exists. test_pthread_safe_stack depends on `onAbort` being proxied back to the main thread. In this case the `onAbort` handler is injected conditionally in `--pre-js=browser_report.js`. In the previous code this meant that the proxied version took precedence because the pthread handler override was injected first. test_pthread_asan_use_after_free_2_wasmfs depends on `printErr` not being proxied back to the main thread. It is injected unconditionally during `--pre-js`. In the previous code path this means that non-proxied version took precedence because it overrode the incoming pthread handler. Fixes: emscripten-core#9796
This method has many advantages over the previous method of generating a separate file: - Avoids separate output file simplifying deployment. - Avoids confusing laying of global scopes - Avoids exporting symbols on the Module simply for visibility within the worker file. - Avoids code duplication - Avoids the needs to importScripts call, and the node polyfill for this. - Allows optimizers such as closure and JSDCE to operate on the combined code. - `-sSINGLE_FILE` now works with pthreads - Fewer network requests - No need for locateFile logic to run on the worker to find the worker.js The test_pthread_custom_pthread_main_url test was completely removed since it was testing how worker.js was located and worker.js no longer exists. test_pthread_safe_stack depends on `onAbort` being proxied back to the main thread. In this case the `onAbort` handler is injected conditionally in `--pre-js=browser_report.js`. In the previous code this meant that the proxied version took precedence because the pthread handler override was injected first. test_pthread_asan_use_after_free_2_wasmfs depends on `printErr` not being proxied back to the main thread. It is injected unconditionally during `--pre-js`. In the previous code path this means that non-proxied version took precedence because it overrode the incoming pthread handler. Fixes: emscripten-core#9796
This method has many advantages over the previous method of generating a separate file: - Avoids separate output file simplifying deployment. - Avoids confusing laying of global scopes - Avoids exporting symbols on the Module simply for visibility within the worker file. - Avoids code duplication - Avoids the needs to importScripts call, and the node polyfill for this. - Allows optimizers such as closure and JSDCE to operate on the combined code. - `-sSINGLE_FILE` now works with pthreads - Fewer network requests - No need for locateFile logic to run on the worker to find the worker.js The test_pthread_custom_pthread_main_url test was completely removed since it was testing how worker.js was located and worker.js no longer exists. test_pthread_safe_stack depends on `onAbort` being proxied back to the main thread. In this case the `onAbort` handler is injected conditionally in `--pre-js=browser_report.js`. In the previous code this meant that the proxied version took precedence because the pthread handler override was injected first. test_pthread_asan_use_after_free_2_wasmfs depends on `printErr` not being proxied back to the main thread. It is injected unconditionally during `--pre-js`. In the previous code path this means that non-proxied version took precedence because it overrode the incoming pthread handler. Fixes: emscripten-core#9796
This method has many advantages over the previous method of generating a separate file: - Avoids separate output file simplifying deployment. - Avoids confusing laying of global scopes - Avoids exporting symbols on the Module simply for visibility within the worker file. - Avoids code duplication - Avoids the needs to importScripts call, and the node polyfill for this. - Allows optimizers such as closure and JSDCE to operate on the combined code. - `-sSINGLE_FILE` now works with pthreads - Fewer network requests - No need for locateFile logic to run on the worker to find the worker.js The test_pthread_custom_pthread_main_url test was completely removed since it was testing how worker.js was located and worker.js no longer exists. test_pthread_safe_stack depends on `onAbort` being proxied back to the main thread. In this case the `onAbort` handler is injected conditionally in `--pre-js=browser_report.js`. In the previous code this meant that the proxied version took precedence because the pthread handler override was injected first. test_pthread_asan_use_after_free_2_wasmfs depends on `printErr` not being proxied back to the main thread. It is injected unconditionally during `--pre-js`. In the previous code path this means that non-proxied version took precedence because it overrode the incoming pthread handler. Fixes: emscripten-core#9796
This method has many advantages over the previous method of generating a separate file: - Avoids separate output file simplifying deployment. - Avoids confusing laying of global scopes - Avoids exporting symbols on the Module simply for visibility within the worker file. - Avoids code duplication - Avoids the needs to importScripts call, and the node polyfill for this. - Allows optimizers such as closure and JSDCE to operate on the combined code. - `-sSINGLE_FILE` now works with pthreads - Fewer network requests - No need for locateFile logic to run on the worker to find the worker.js The test_pthread_custom_pthread_main_url test was completely removed since it was testing how worker.js was located and worker.js no longer exists. test_pthread_safe_stack depends on `onAbort` being proxied back to the main thread. In this case the `onAbort` handler is injected conditionally in `--pre-js=browser_report.js`. In the previous code this meant that the proxied version took precedence because the pthread handler override was injected first. test_pthread_asan_use_after_free_2_wasmfs depends on `printErr` not being proxied back to the main thread. It is injected unconditionally during `--pre-js`. In the previous code path this means that non-proxied version took precedence because it overrode the incoming pthread handler. Fixes: emscripten-core#9796
This method has many advantages over the previous method of generating a separate file: - Avoids separate output file simplifying deployment. - Avoids confusing laying of global scopes - Avoids exporting symbols on the Module simply for visibility within the worker file. - Avoids code duplication - Avoids the needs to importScripts call, and the node polyfill for this. - Allows optimizers such as closure and JSDCE to operate on the combined code. - `-sSINGLE_FILE` now works with pthreads - Fewer network requests - No need for locateFile logic to run on the worker to find the worker.js The test_pthread_custom_pthread_main_url test was completely removed since it was testing how worker.js was located and worker.js no longer exists. test_pthread_safe_stack depends on `onAbort` being proxied back to the main thread. In this case the `onAbort` handler is injected conditionally in `--pre-js=browser_report.js`. In the previous code this meant that the proxied version took precedence because the pthread handler override was injected first. test_pthread_asan_use_after_free_2_wasmfs depends on `printErr` not being proxied back to the main thread. It is injected unconditionally during `--pre-js`. In the previous code path this means that non-proxied version took precedence because it overrode the incoming pthread handler. Fixes: emscripten-core#9796
This method has many advantages over the previous method of generating a separate file: - Avoids separate output file simplifying deployment. - Avoids confusing laying of global scopes - Avoids exporting symbols on the Module simply for visibility within the worker file. - Avoids code duplication - Avoids the needs to importScripts call, and the node polyfill for this. - Allows optimizers such as closure and JSDCE to operate on the combined code. - `-sSINGLE_FILE` now works with pthreads - Fewer network requests - No need for locateFile logic to run on the worker to find the worker.js The test_pthread_custom_pthread_main_url test was completely removed since it was testing how worker.js was located and worker.js no longer exists. test_pthread_safe_stack depends on `onAbort` being proxied back to the main thread. In this case the `onAbort` handler is injected conditionally in `--pre-js=browser_report.js`. In the previous code this meant that the proxied version took precedence because the pthread handler override was injected first. test_pthread_asan_use_after_free_2_wasmfs depends on `printErr` not being proxied back to the main thread. It is injected unconditionally during `--pre-js`. In the previous code path this means that non-proxied version took precedence because it overrode the incoming pthread handler. Fixes: #9796
After 4 years lol. Would you do this for wasm workers, too? @sbc100 |
@sbc100 Nice, great job! |
Currently when building the USE_PTHREADS Emscripten creates a separate module.worker js file.
This is loaded from module.js like:
It is also possible to create an inline webworker, doing something like:
I imagine that it would be simple enough to minify module.worker.js, then embed that into the main module.js.
I believe that this will speed up loading threads, and should also make webpacking pthreads builds easier/possible.
The text was updated successfully, but these errors were encountered: