Skip to content
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 a new compiler setting that tells the JS build output to expect that the locateFile module hook will be used #21927

Open
dasa opened this issue May 10, 2024 · 17 comments

Comments

@dasa
Copy link
Contributor

dasa commented May 10, 2024

This is an enhancement request.

I think this setting would reduce unnecessary JS code and complexity in the output that's left unused when locateFile is defined on the Module.

Perhaps it could be called USE_LOCATE_FILE, and a runtime error would be expected if locateFile was not defined.

/cc @RReverser

@sbc100
Copy link
Collaborator

sbc100 commented May 10, 2024

If there are code size saving that result then we should certainly do this.

Could you point the code that you think can be elided in this case?

@RReverser
Copy link
Collaborator

We already have that mechanism for arbitrary Module settings though, do we really need another named setting?

@dasa
Copy link
Contributor Author

dasa commented May 10, 2024

My thought was that

      function locateFile(path) {
        if (Module["locateFile"]) {
          return Module["locateFile"](path, scriptDirectory);
        }
        return scriptDirectory + path;
      }

Could be reduced to:

      function locateFile(path) {
        return Module["locateFile"](path, "");
      }

And then this code that sets up _scriptName and scriptDirectory could be removed, but maybe I'm missing something.

    var _scriptName = typeof document != "undefined" ? document.currentScript?.src : undefined;
    if (typeof __filename != "undefined") _scriptName ||= __filename;

// ...

        if (ENVIRONMENT_IS_WORKER) {
          scriptDirectory = self.location.href;
        } else if (typeof document != "undefined" && document.currentScript) {
          scriptDirectory = document.currentScript.src;
        }
        if (_scriptName) {
          scriptDirectory = _scriptName;
        }
        if (scriptDirectory.startsWith("blob:")) {
          scriptDirectory = "";
        } else {
          scriptDirectory = scriptDirectory.substr(0, scriptDirectory.replace(/[?#].*/, "").lastIndexOf("/") + 1);
        }

@sbc100
Copy link
Collaborator

sbc100 commented May 11, 2024

Do you mean INCOMING_MODULE_JS_API? I think one issue is that inclusion in this list just tells emscripten "I may set this as an incoming argument".. and not "you can depend on this setting being set".

We could perhaps change this meaning for certain settings such as locateFile?

@sbc100
Copy link
Collaborator

sbc100 commented May 11, 2024

BTW , if we really want to shave some serious code size we should be looking at the wasm or wasmBinary settings which could also allow is to avoid including all of the hundreds of lines of module loading code.. but -sMINIMAL_RUNTIME already does this. Have you looked into MINIMAL_RUNTIME by the way?

@sbc100
Copy link
Collaborator

sbc100 commented May 11, 2024

@dasa, BTW can I ask what you are using locateFile for? Is it because you cannot host your wasm file alongside your js file?

@dasa
Copy link
Contributor Author

dasa commented May 11, 2024

Have you looked into MINIMAL_RUNTIME by the way?

I have not.

BTW can I ask what you are using locateFile for? Is it because you cannot host your wasm file alongside your js file?

Correct, for example, I have a NPM package that loads the Wasm file from a CDN.

@kripken
Copy link
Member

kripken commented May 14, 2024

Do you mean INCOMING_MODULE_JS_API? I think one issue is that inclusion in this list just tells emscripten "I may set this as an incoming argument".. and not "you can depend on this setting being set".

@sbc100 I think that is the same, unless I am missing your meaning? That is, the setting means "the user may set Module.X; if a setting is not in this list, the compiler can assume the user does not set Module.X".

In fact, locateFile is already in the default list:

'loadSplitModule', 'locateFile', 'logReadFiles', 'mainScriptUrlOrBlob', 'mem',

So I agree with @RReverser that we may not need a new setting here, we can take advantage of the existing one, unless I'm missing something.

@dasa
Copy link
Contributor Author

dasa commented May 14, 2024

I'm proposing a setting telling the compiler that it can count on the user setting locateFile so it can remove unused code.

@dasa
Copy link
Contributor Author

dasa commented May 14, 2024

The values in INCOMING_MODULE_JS_API seem to be treated as things that MAY be set rather than MUST be set. Instead of a new setting just for locateFile, maybe a new setting for things that MUST be set would be more generic.

@sbc100
Copy link
Collaborator

sbc100 commented May 14, 2024

I guess one option would be to add new setting such GUARANTEED_INCOMING_MODULE_JS_API (but with a better name).

Alternatively we could keep the existing setting and change its meaning for certain elements from "may have" to "will have"?

@sbc100
Copy link
Collaborator

sbc100 commented May 14, 2024

Or perhaps as you suggest we create a specific new setting just for this?

I've always disliked the setting name INCOMING_MODULE_JS_API so maybe this is our chance to come up with a better name?

@sbc100
Copy link
Collaborator

sbc100 commented May 14, 2024

Can we imagine a use case were a developer want to be able to supply locateFile ... but not all the time?

@RReverser
Copy link
Collaborator

Idk, it seems like such a tiny saving (a few bytes of the if, and it's not even a repetitive pattern) that I'd say a separate setting/handling just to save those is not worth it.

@dasa
Copy link
Contributor Author

dasa commented May 14, 2024

It's not much, but isn't it also the code that sets scriptDirectory?

@sbc100
Copy link
Collaborator

sbc100 commented May 14, 2024

I believe the _scriptName code is still needed/used even if locateFile is specified. Specifically I think _scriptName is used in pthread builds when creating new workers, regardless of locateFile.

@kripken
Copy link
Member

kripken commented May 15, 2024

Ah, so this is to save code size in the case where it is provided, sorry for misunderstanding before.

I tend to agree with @RReverser that this may not be worth it. Optimizing code size for the default is worth doing - most people don't need locateFile - and I think when people use an optional setting like locateFile it's ok to add a little code size. Optimizing all such optional settings by adding new settings would be a lot of work.

With that said, if the code size difference was very large I'd be ok with a new setting, but in this case I think it's quite minor? I would also be ok with a generic new setting like GUARANTEED_INCOMING_MODULE_JS_API as @sbc100 proposed if we think it will have more uses.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants