Skip to content

Commit

Permalink
Reduce Worker indirection for bundlers
Browse files Browse the repository at this point in the history
Now that wasm-bindgen has `wasm_bindgen::link_to!`, we can use it to
ensure that Worker is emitted alongside the snippet file instead of
having to use the snippet file itself as a Worker source.

In turn, this allows to use static import of the main module in the
Worker, which avoids unnecessary extra code splitting we used to have.

Aside from slightly better chunking, this also happens to work around
the Parcel issue parcel-bundler/parcel#8727.
  • Loading branch information
RReverser committed Dec 27, 2023
1 parent bda6114 commit a42a69f
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 73 deletions.
32 changes: 16 additions & 16 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ exclude = [".github"]
repository = "https://github.com/RReverser/wasm-bindgen-rayon"

[dependencies]
wasm-bindgen = "0.2.74"
wasm-bindgen = "0.2.84"
rayon-core = "1.12"
spmc = "0.3.0"
js-sys = "0.3.48"
Expand Down
6 changes: 6 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,12 @@ extern "C" {
fn start_workers(module: JsValue, memory: JsValue, builder: wbg_rayon_PoolBuilder) -> Promise;
}

#[cfg(not(feature = "no-bundler"))]
fn _ensure_worker_emitted() {
// Just ensure that the worker is emitted into the output folder, but don't actually use the URL.
wasm_bindgen::link_to!(module = "/src/workerHelpers.worker.js");
}

#[wasm_bindgen]
impl wbg_rayon_PoolBuilder {
fn new(num_threads: usize) -> Self {
Expand Down
63 changes: 9 additions & 54 deletions src/workerHelpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,45 +18,6 @@
// If we didn't take that into the account, we could send much simpler signals
// like just `0` or whatever, but the code would be less resilient.

function waitForMsgType(target, type) {
return new Promise(resolve => {
target.addEventListener('message', function onMsg({ data }) {
if (data == null || data.type !== type) return;
target.removeEventListener('message', onMsg);
resolve(data);
});
});
}

waitForMsgType(self, 'wasm_bindgen_worker_init').then(async data => {
// # Note 1
// Our JS should have been generated in
// `[out-dir]/snippets/wasm-bindgen-rayon-[hash]/workerHelpers.js`,
// resolve the main module via `../../..`.
//
// This might need updating if the generated structure changes on wasm-bindgen
// side ever in the future, but works well with bundlers today. The whole
// point of this crate, after all, is to abstract away unstable features
// and temporary bugs so that you don't need to deal with them in your code.
//
// # Note 2
// This could be a regular import, but then some bundlers complain about
// circular deps.
//
// Dynamic import could be cheap if this file was inlined into the parent,
// which would require us just using `../../..` in `new Worker` below,
// but that doesn't work because wasm-pack unconditionally adds
// "sideEffects":false (see below).
//
// OTOH, even though it can't be inlined, it should be still reasonably
// cheap since the requested file is already in cache (it was loaded by
// the main thread).
const pkg = await import('../../..');
await pkg.default(data.module, data.memory);
postMessage({ type: 'wasm_bindgen_worker_ready' });
pkg.wbg_rayon_start_worker(data.receiver);
});

// Note: this is never used, but necessary to prevent a bug in Firefox
// (https://bugzilla.mozilla.org/show_bug.cgi?id=1702191) where it collects
// Web Workers that have a shared WebAssembly memory with the main thread,
Expand All @@ -72,7 +33,6 @@ export async function startWorkers(module, memory, builder) {
}

const workerInit = {
type: 'wasm_bindgen_worker_init',
module,
memory,
receiver: builder.receiver()
Expand All @@ -86,21 +46,16 @@ export async function startWorkers(module, memory, builder) {
// way to get asset URLs relative to the module across various bundlers
// and browser, ideally we should switch to `import.meta.resolve`
// once it becomes a standard.
//
// Note: we could use `../../..` as the URL here to inline workerHelpers.js
// into the parent entry instead of creating another split point -
// this would be preferable from optimization perspective -
// however, Webpack then eliminates all message handler code
// because wasm-pack produces "sideEffects":false in package.json
// unconditionally.
//
// The only way to work around that is to have side effect code
// in an entry point such as Worker file itself.
const worker = new Worker(new URL('./workerHelpers.js', import.meta.url), {
type: 'module'
});
const worker = new Worker(
new URL('./workerHelpers.worker.js', import.meta.url),
{
type: 'module'
}
);
worker.postMessage(workerInit);
await waitForMsgType(worker, 'wasm_bindgen_worker_ready');
await new Promise(resolve =>
worker.addEventListener('message', resolve, { once: true })
);
return worker;
})
);
Expand Down
28 changes: 28 additions & 0 deletions src/workerHelpers.worker.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* Copyright 2022 Google Inc. All Rights Reserved.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

// Note: our JS should have been generated in
// `[out-dir]/snippets/wasm-bindgen-rayon-[hash]/workerHelpers.worker.js`,
// resolve the main module via `../../..`.
//
// This might need updating if the generated structure changes on wasm-bindgen
// side ever in the future, but works well with bundlers today. The whole
// point of this crate, after all, is to abstract away unstable features
// and temporary bugs so that you don't need to deal with them in your code.
import initWbg, { wbg_rayon_start_worker } from '../../../';

onmessage = async ({ data: { module, memory, receiver } }) => {
await initWbg(module, memory);
postMessage(true);
wbg_rayon_start_worker(receiver);
};
3 changes: 1 addition & 2 deletions test/run-tests.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -68,5 +68,4 @@ async function runTest(t) {
test('no-bundler', runTest);
test('rollup', runTest);
test('webpack', runTest);
// Parcel seems broken for now: https://github.com/parcel-bundler/parcel/issues/8727
test.skip('parcel', runTest);
test('parcel', runTest);

0 comments on commit a42a69f

Please sign in to comment.