Skip to content

Add option to inline webworker #9796

Closed

Description

@VirtualTim

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.

Activity

  1. kripken commented on Nov 7, 2019

    @kripken
    Member

    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.

  2. VirtualTim commented on Nov 8, 2019

    @VirtualTim
    CollaboratorAuthor

    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.

  3. VirtualTim commented on Nov 11, 2019

    @VirtualTim
    CollaboratorAuthor

    I took a brief look at this, and here's my idea so far:
    Settings.js:

    //add option:
    var INLINE_WEBWORKER = 0;
    

    shared.py:

    def generate_inline_webworker(worker_filename):
      webworker = js_optimizer_no_asmjs(worker_filename, return_output=True, extra_info=None, acorn=True)
      return webworker
    

    emcc.py from around here:

    if shared.Settings.USE_PTHREADS:

    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?

  4. kripken commented on Nov 13, 2019

    @kripken
    Member

    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.

  5. VirtualTim commented on Nov 14, 2019

    @VirtualTim
    CollaboratorAuthor

    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.

  6. stale commented on Nov 14, 2020

    @stale

    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.

  7. jeffRTC commented on May 4, 2021

    @jeffRTC

    @VirtualTim

    I badly need this for a restricted environment where external files are not allowed. It's like the -s SINGLE_FILE=1 option.

  8. reopened this on May 4, 2021
  9. jeffRTC commented on May 4, 2021

    @jeffRTC

    @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.

    const fs = require('fs');
    const args = process.argv;
    
    function escapeIt(string) {
        return string.replace(/[\`]/g, '\\`');
    }
    
    // MyModule
    let moduleName = args[2];
    
    // C:/runtime.js
    let runtimeScriptFilePath = args[3];
    // C:/runtime.worker.js
    let workerScriptFilePath = args[4];
    
    let runtimeScript = fs.readFileSync(runtimeScriptFilePath, "utf8");
    let workerScript = fs.readFileSync(workerScriptFilePath, "utf8");
    
    let target = `locateFile("${moduleName}.worker.js")`;
    let replace = `URL.createObjectURL(new Blob([\`${escapeIt(workerScript)}\`], { type: "application/javascript" }))`;
    
    let result = runtimeScript.replace(target, replace);
    
    fs.writeFileSync(runtimeScriptFilePath, result);
    
  10. VirtualTim commented on May 5, 2021

    @VirtualTim
    CollaboratorAuthor

    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.

  11. RReverser commented on Jun 2, 2021

    @RReverser
    Collaborator

    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).

  12. sbc100 commented on Jun 2, 2021

    @sbc100
    Collaborator

    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)

  13. 60 remaining items

  14. added 11 commits that reference this issue on Apr 12, 2024
    77219ab
    177f8ce
    e2726f8
    9c3f2af
    e9cb4fc
    f83a697
    49c0fba
    2dfa64d
    651e58b
    eb5cf0a
    7c5b0cf
  15. added a commit that references this issue on Apr 16, 2024
    37e1e68
  16. msqr1 commented on Apr 18, 2024

    @msqr1

    After 4 years lol. Would you do this for wasm workers, too? @sbc100

  17. RReverser commented on Apr 18, 2024

    @RReverser
    Collaborator

    @sbc100 Nice, great job!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions