Skip to content

Commit d4537ff

Browse files
authored
Fix WASM_WORKERS + MINIMAL_RUNTIME to transfer compiled module (#24865)
The current code was transferring `Module['wasm']` which, on them the main thread is normally a byte array and not the compiled module. The new code uses the same pattern that pthreads uses to stash the compiled module and postMessage that instead, which should be more performant. In the worker / pthread the global `Module['wasm']` is then set to the received object so somewhat confusingly `Module['wasm']` is always a compiled module in a worker / pthread (at least under `MINIMAL_RUNTIME`).
1 parent 72a18eb commit d4537ff

6 files changed

+46
-50
lines changed

src/lib/libwasm_worker.js

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -200,11 +200,7 @@ if (ENVIRONMENT_IS_WASM_WORKER
200200
// Signal with a non-zero value that this Worker will be a Wasm Worker,
201201
// and not the main browser thread.
202202
wwID: _wasmWorkersID,
203-
#if MINIMAL_RUNTIME
204-
wasm: Module['wasm'],
205-
#else
206203
wasm: wasmModule,
207-
#endif
208204
wasmMemory,
209205
stackLowestAddress, // sb = stack bottom (lowest stack address, SP points at this when stack is full)
210206
stackSize, // sz = stack size

src/postamble_minimal.js

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -88,11 +88,9 @@ Module['wasm'] = base64Decode('<<< WASM_BINARY_DATA >>>');
8888
var wasmExports;
8989
#endif
9090

91-
#if PTHREADS
91+
#if PTHREADS || WASM_WORKERS
9292
var wasmModule;
93-
#endif
9493

95-
#if PTHREADS || WASM_WORKERS
9694
function loadModule() {
9795
assignWasmImports();
9896
#endif
@@ -152,12 +150,14 @@ WebAssembly.instantiate(Module['wasm'], imports).then(/** @suppress {missingProp
152150
// output.module objects. But if Module['wasm'] is an already compiled
153151
// WebAssembly module, then output is the WebAssembly instance itself.
154152
// Depending on the build mode, Module['wasm'] can mean a different thing.
155-
#if PTHREADS
156-
// In pthreads, Module['wasm'] is an already compiled WebAssembly.Module. In
157-
// that case, 'output' is a WebAssembly.Instance.
153+
#if PTHREADS || WASM_WORKERS
154+
// In pthreads and wasm workers, Module['wasm'] is a compiled
155+
// WebAssembly.Module. In that case, 'output' is a WebAssembly.Instance.
158156
// In main thread, Module['wasm'] is either a typed array or a fetch stream.
159157
// In that case, 'output.instance' is the WebAssembly.Instance.
160158
wasmExports = (output.instance || output).exports;
159+
// Stash the Wasm module for future worker creation.
160+
wasmModule = output.module || Module['wasm'];
161161
#elif MINIMAL_RUNTIME_STREAMING_WASM_COMPILATION
162162
// In MINIMAL_RUNTIME_STREAMING_WASM_COMPILATION mode, Module['wasm'] is the
163163
// compiled module so we just get the instance back.
@@ -217,9 +217,8 @@ WebAssembly.instantiate(Module['wasm'], imports).then(/** @suppress {missingProp
217217
<<< ATPRERUNS >>>
218218

219219
initRuntime(wasmExports);
220+
220221
#if PTHREADS
221-
// Export Wasm module for pthread creation to access.
222-
wasmModule = output.module || Module['wasm'];
223222
PThread.loadWasmModuleToAllWorkers(ready);
224223
#else
225224
ready();

test/code_size/audio_worklet_wasm.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,7 @@ function y() {
220220
WebAssembly.instantiate(l.wasm, {
221221
a: Z
222222
}).then((a => {
223-
a = a.instance.exports;
223+
a = (a.instance || a).exports;
224224
X = a.j;
225225
I = a.l;
226226
D = a.m;
Lines changed: 30 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,74 +1,75 @@
1-
var b = Module, c = "em-ww" == globalThis.name, e, f, l, x, y, z;
1+
var c = Module, d = "em-ww" == globalThis.name, e, f, l, y, z, A, t;
22

3-
c && (onmessage = a => {
3+
d && (onmessage = a => {
44
onmessage = null;
55
e = a = a.data;
66
f = a.o;
77
g();
8-
b ||= {};
9-
b.wasm = a.j;
8+
c ||= {};
9+
c.wasm = a.j;
1010
h();
1111
a.j = a.s = 0;
1212
});
1313

1414
function g() {}
1515

16-
c || (f = b.mem || new WebAssembly.Memory({
16+
d || (f = c.mem || new WebAssembly.Memory({
1717
initial: 256,
1818
maximum: 256,
1919
shared: !0
2020
}), g());
2121

2222
var k = [], n = a => {
2323
a = a.data;
24-
let d = a._wsc;
25-
d && l.get(d)(...a.x);
24+
let b = a._wsc;
25+
b && l.get(b)(...a.x);
2626
}, p = a => {
2727
k.push(a);
28-
}, q = {}, r = 1, t = (a, d) => {
29-
let m = q[r] = new Worker(b.js, {
28+
}, q = {}, r = 1, u = (a, b) => {
29+
let m = q[r] = new Worker(c.js, {
3030
name: "em-ww"
3131
});
3232
m.postMessage({
3333
u: r,
34-
j: b.wasm,
34+
j: t,
3535
o: f,
3636
l: a,
37-
m: d
37+
m: b
3838
});
3939
m.onmessage = n;
4040
return r++;
41-
}, u = () => !1, v = (a, d) => {
41+
}, v = () => !1, w = (a, b) => {
4242
q[a].postMessage({
43-
_wsc: d,
43+
_wsc: b,
4444
x: []
4545
});
4646
};
4747

48-
c && (q[0] = globalThis, addEventListener("message", p));
48+
d && (q[0] = globalThis, addEventListener("message", p));
4949

50-
function w() {
50+
function x() {
5151
console.log("Hello from wasm worker!");
5252
}
5353

5454
function h() {
55-
z = {
56-
b: t,
57-
c: u,
58-
d: v,
59-
e: w,
55+
A = {
56+
b: u,
57+
c: v,
58+
d: w,
59+
e: x,
6060
a: f
6161
};
62-
WebAssembly.instantiate(b.wasm, {
63-
a: z
62+
WebAssembly.instantiate(c.wasm, {
63+
a: A
6464
}).then((a => {
65-
a = a.instance.exports;
66-
x = a.g;
67-
y = a.i;
68-
l = a.h;
69-
c ? (y(e.l, e.m), removeEventListener("message", p), k = k.forEach(n), addEventListener("message", n)) : a.f();
70-
c || x();
65+
var b = (a.instance || a).exports;
66+
t = a.module || c.wasm;
67+
y = b.g;
68+
z = b.i;
69+
l = b.h;
70+
d ? (z(e.l, e.m), removeEventListener("message", p), k = k.forEach(n), addEventListener("message", n)) : b.f();
71+
d || y();
7172
}));
7273
}
7374

74-
c || h();
75+
d || h();
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
{
22
"a.html": 519,
33
"a.html.gz": 357,
4-
"a.js": 3871,
5-
"a.js.gz": 2036,
4+
"a.js": 3876,
5+
"a.js.gz": 2042,
66
"a.wasm": 1288,
77
"a.wasm.gz": 860,
8-
"total": 5678,
9-
"total_gz": 3253
8+
"total": 5683,
9+
"total_gz": 3259
1010
}
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
{
22
"a.html": 519,
33
"a.html.gz": 357,
4-
"a.js": 830,
5-
"a.js.gz": 525,
4+
"a.js": 855,
5+
"a.js.gz": 543,
66
"a.wasm": 1885,
77
"a.wasm.gz": 1072,
8-
"total": 3234,
9-
"total_gz": 1954
8+
"total": 3259,
9+
"total_gz": 1972
1010
}

0 commit comments

Comments
 (0)