Skip to content

Skip wasmBinaryFile initialization on pthread workers #21829

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

Merged
merged 1 commit into from
Apr 25, 2024

Conversation

sbc100
Copy link
Collaborator

@sbc100 sbc100 commented Apr 25, 2024

The fact that we were calling locateFile in an attempt to find the wasm binary on each worker was confusing at best. The wasm file is always received via postMessage when running on the worker.

See #21826 and #21827

@sbc100 sbc100 requested review from kripken and dschuff April 25, 2024 16:01
@sbc100 sbc100 force-pushed the locateFile_workers branch from 33fea95 to 3fd95b6 Compare April 25, 2024 16:32
Copy link
Member

@kripken kripken left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code lgtm but I think it's worth seeing why this increases code size. It seems like just moving code around shouldn't cause that, so maybe there is some specific issue that we can improve here.

@sbc100
Copy link
Collaborator Author

sbc100 commented Apr 25, 2024

Moving this code does seems to have some tiny effects on code size.. some positive but mostly minutely negative. Since this code is simply moved from top level to the inside of a function, with zero changes, I can only assume that is slightly confusing closure compiler.

An alternative to move this code would be to instead wrap it in if (!ENVIRONMENT_IS_PTHREAD && !ENVIRONMETN_IS_WORKER)

@sbc100
Copy link
Collaborator Author

sbc100 commented Apr 25, 2024

I tried looking at the generated code (after closure compiler was applied) both before and after this change but I'm struggling to come with any rationale for why it might regress.

I took one same regression which is other.test_metadce_mem_O3_standalone_narg which regressed from 4554 to 4580 (24 bytes or 0.5%).

I've attached the result of clang-formatting the resulting JS files.

Can you see where the differences are coming from.
compare.zip

@sbc100 sbc100 force-pushed the locateFile_workers branch from 3fd95b6 to 8dcab60 Compare April 25, 2024 16:57
@sbc100
Copy link
Collaborator Author

sbc100 commented Apr 25, 2024

I did some refactoring, and put this code into a function that can do early returns instead of branches. The code size effects are now almost all wins (especially with gz).

@kripken
Copy link
Member

kripken commented Apr 25, 2024

Thanks, after adding some whitespace it is easier to flip back and forth between the files to see the differences,

https://gist.github.com/kripken/286edabc866138d05716c2efbbd1cad4

The main issue (aside from code moving around, which does not affect size) is that in the new code the locateFile usage remains in a function, where it has a return, and there is a call to it. Before it was inlined, saving those bytes.

I see no reason for it not to be inlined in the new code. Perhaps closure is more careful about inlining into nested scopes. Or maybe it is because of the condition right before it - a little hard to see what that was before closure, but maybe you have some idea there.

If not this is probably ok, but it's worth trying a bit to avoid a regression here I think.

@kripken
Copy link
Member

kripken commented Apr 25, 2024

Ah, our last two comments raced. Your new code lgtm, and if gzip size is mostly a win then that seems fine.

@sbc100
Copy link
Collaborator Author

sbc100 commented Apr 25, 2024

Perhaps because createWasm is already quote a large function closure chose not to inline. Or perhaps the inlining rules for top level code are differen to function-scope code?

Anyway, after extracting the function the code size effects seem much better.

@sbc100 sbc100 enabled auto-merge (squash) April 25, 2024 17:35
@sbc100 sbc100 force-pushed the locateFile_workers branch from 8dcab60 to 56bc583 Compare April 25, 2024 17:43
Delay the search for the wasm binary (e.g. using `locateFile`) so that
it does not run on pthread workers (or weasm workers) where
`Module.instantiateWasm` is set.

The wasm file is always received via postMessage when running on the
worker so we shouldn't ever be calling locateFile in this situation.

See emscripten-core#21826 and emscripten-core#21827
@sbc100 sbc100 force-pushed the locateFile_workers branch from 56bc583 to e09b6ba Compare April 25, 2024 19:01
@sbc100 sbc100 disabled auto-merge April 25, 2024 20:12
@sbc100 sbc100 merged commit d9c87f2 into emscripten-core:main Apr 25, 2024
@sbc100 sbc100 deleted the locateFile_workers branch April 25, 2024 20:12
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

Successfully merging this pull request may close these issues.

2 participants