You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Currently when building the USE_PTHREADS Emscripten creates a separate module.worker js file.
This is loaded from module.js like:
var pthreadMainJs = "module.worker.js";
pthreadMainJs = locateFile(pthreadMainJs);
var newWorkers = [];
for (var i = 0; i < numWorkers; ++i) {
newWorkers.push(new Worker(pthreadMainJs));
}
It is also possible to create an inline webworker, doing something like:
let workerBlob = new Blob(['string_containing_minified_module.worker.js'], type:"application/javascript"});
pthreadMainJs = URL.createObjectURL(workerBlob);
var newWorkers = [];
for (var i = 0; i < numWorkers; ++i) {
newWorkers.push(new Worker(pthreadMainJs));
}
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.
Reacted by Username404-59, Glavin Wiechert and George Corney
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.
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.
It would make the initial download slower
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.
with ToolchainProfiler.profile_block('memory initializer'):
if shared.Settings.USE_PTHREADS:
...
with open(worker_output, 'w') as f:
f.write(shared.read_and_preprocess(shared.path_from_root('src', 'worker.js'), expand_macros=True))
if shared.Settings.INLINE_WEBWORKER:
worker = shared.generate_inline_webworker(worker_output)
# construct js:
src = ''' //something like
var workerBlob = new Blob(['%(worker)'], type:"application/javascript"});
pthreadMainJs = URL.createObjectURL(workerBlob);
''' % {'worker': worker}
else:
# construct js:
src = '''
var pthreadMainJs = "module.worker.js";
pthreadMainJs = locateFile(pthreadMainJs);
'''
#insert constructed js (src) into Module
#I'm not sure how to do this
Does this seem like it's on the right path?
What's the best way to do macro substitution in Module.js?
By macro substitution do you mean the JS preprocessor? There's a helper function read_and_preprocess in shared.py which does that I think.
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.
I'll investigate more first if it fixes the issue, and then also do some profiling.
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'm glad you've got something working.
Personally I've long abandoned that idea, it didn't help with my bundling issue, and made start-up significantly slower.
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 SINGLE_FILE mode.
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 new Worker('...?emscripten-worker')) or a global name (new Worker('...', { name: 'emscripten-worker' })) or something else in similar spirit. (Note: just checking ENVIRONMENT_IS_WORKER is not enough, because main JS might be used in a Worker too).
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)
Activity
kripken commentedon Nov 7, 2019
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.
VirtualTim commentedon Nov 8, 2019
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.
VirtualTim commentedon Nov 11, 2019
I took a brief look at this, and here's my idea so far:
Settings.js:
shared.py:
emcc.py from around here:
emscripten/emcc.py
Line 2374 in 6d297f6
Does this seem like it's on the right path?
What's the best way to do macro substitution in Module.js?
kripken commentedon Nov 13, 2019
By macro substitution do you mean the JS preprocessor? There's a helper function
read_and_preprocessin shared.py which does that I think.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.
VirtualTim commentedon Nov 14, 2019
I guess my motivation was more about making pthreads builds easier/possible to webpack.
I'll investigate more first if it fixes the issue, and then also do some profiling.
stale commentedon Nov 14, 2020
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.
jeffRTC commentedon May 4, 2021
@VirtualTim
I badly need this for a restricted environment where external files are not allowed. It's like the
-s SINGLE_FILE=1option.jeffRTC commentedon May 4, 2021
@VirtualTim
I just created a NodeJS script to inline the worker and it works, but would be better to add it to compiler it-self.
VirtualTim commentedon May 5, 2021
I'm glad you've got something working.
Personally I've long abandoned that idea, it didn't help with my bundling issue, and made start-up significantly slower.
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.
RReverser commentedon Jun 2, 2021
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
SINGLE_FILEmode.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
new Worker('...?emscripten-worker')) or a global name (new Worker('...', { name: 'emscripten-worker' })) or something else in similar spirit. (Note: just checkingENVIRONMENT_IS_WORKERis not enough, because main JS might be used in a Worker too).sbc100 commentedon Jun 2, 2021
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)
60 remaining items
Inline pthread worker.js file into the main output file
Inline pthread worker.js file into the main output file
Inline pthread worker.js file into the main output file
Inline pthread worker.js file into the main output file
Inline pthread worker.js file into the main output file
Inline pthread worker.js file into the main output file
Inline pthread worker.js file into the main output file
Inline pthread worker.js file into the main output file
Inline pthread worker.js file into the main output file
Inline pthread worker.js file into the main output file
Inline pthread worker.js file into the main output file
Inline pthread worker.js file into the main output file (#21701)
msqr1 commentedon Apr 18, 2024
After 4 years lol. Would you do this for wasm workers, too? @sbc100
RReverser commentedon Apr 18, 2024
@sbc100 Nice, great job!