Skip to content

Commit b45fcac

Browse files
authored
Optimize ENVIRONMENT_IS_WASM_WORKER check when building with Audio Worklets enabled (#25364)
Optimize ENVIRONMENT_IS_WASM_WORKER check when building with Audio Worklets enabled, for a code size saving of 7 bytes. Before, Closure would emit: ```js m = "em-ww" == globalThis.name, n = !!globalThis.AudioWorkletGlobalScope, n && (m = !0); ``` Afterwards, it emits ```js n = !!globalThis.AudioWorkletGlobalScope, m = "em-ww" == globalThis.name || n, ```
1 parent bcde51d commit b45fcac

File tree

3 files changed

+20
-21
lines changed

3 files changed

+20
-21
lines changed

src/shell_minimal.js

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -58,22 +58,23 @@ if (ENVIRONMENT_IS_NODE) {
5858
}
5959
#endif
6060

61-
#if WASM_WORKERS
61+
#if AUDIO_WORKLET
62+
var ENVIRONMENT_IS_AUDIO_WORKLET = !!globalThis.AudioWorkletGlobalScope;
63+
#endif
64+
65+
#if AUDIO_WORKLET && WASM_WORKERS
66+
var ENVIRONMENT_IS_WASM_WORKER = globalThis.name == 'em-ww' || ENVIRONMENT_IS_AUDIO_WORKLET;
67+
#elif WASM_WORKERS
6268
var ENVIRONMENT_IS_WASM_WORKER = globalThis.name == 'em-ww';
69+
#endif
6370

64-
#if ENVIRONMENT_MAY_BE_NODE
71+
#if WASM_WORKERS && ENVIRONMENT_MAY_BE_NODE
6572
if (ENVIRONMENT_IS_NODE) {
6673
// The way we signal to a worker that it is hosting a pthread is to construct
6774
// it with a specific name.
6875
ENVIRONMENT_IS_WASM_WORKER = worker_threads['workerData'] == 'em-ww'
6976
}
7077
#endif
71-
#endif
72-
73-
#if AUDIO_WORKLET
74-
var ENVIRONMENT_IS_AUDIO_WORKLET = !!globalThis.AudioWorkletGlobalScope;
75-
if (ENVIRONMENT_IS_AUDIO_WORKLET) ENVIRONMENT_IS_WASM_WORKER = true;
76-
#endif
7778

7879
#if ASSERTIONS && ENVIRONMENT_MAY_BE_NODE && ENVIRONMENT_MAY_BE_SHELL
7980
if (ENVIRONMENT_IS_NODE && ENVIRONMENT_IS_SHELL) {

test/codesize/audio_worklet_wasm.expected.js

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
var m = globalThis.Module || "undefined" != typeof Module ? Module : {}, q = "em-ww" == globalThis.name, r = !!globalThis.AudioWorkletGlobalScope, t, z, J, K, H, E, v, X, F, D, C, Y, A, Z;
2-
3-
r && (q = !0);
1+
var m = globalThis.Module || "undefined" != typeof Module ? Module : {}, q = !!globalThis.AudioWorkletGlobalScope, r = "em-ww" == globalThis.name || q, t, z, J, K, H, E, v, X, F, D, C, Y, A, Z;
42

53
function u(a) {
64
t = a;
@@ -12,12 +10,12 @@ function u(a) {
1210
a.G = a.M = 0;
1311
}
1412

15-
q && !r && (onmessage = a => {
13+
r && !q && (onmessage = a => {
1614
onmessage = null;
1715
u(a.data);
1816
});
1917

20-
if (r) {
18+
if (q) {
2119
function a(e) {
2220
class d extends AudioWorkletProcessor {
2321
constructor(c) {
@@ -98,7 +96,7 @@ function w() {
9896
E = new Float32Array(a);
9997
}
10098

101-
q || (v = m.mem || new WebAssembly.Memory({
99+
r || (v = m.mem || new WebAssembly.Memory({
102100
initial: 256,
103101
maximum: 256,
104102
shared: !0
@@ -236,9 +234,9 @@ function y() {
236234
C = a.n;
237235
Y = a.o;
238236
A = a.k;
239-
q ? (Y(t.J, t.F), r || (removeEventListener("message", N), L = L.forEach(M), addEventListener("message", M))) : a.i();
240-
q || X();
237+
r ? (Y(t.J, t.F), q || (removeEventListener("message", N), L = L.forEach(M), addEventListener("message", M))) : a.i();
238+
r || X();
241239
}));
242240
}
243241

244-
q || y();
242+
r || y();
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": 3866,
5-
"a.js.gz": 2041,
4+
"a.js": 3859,
5+
"a.js.gz": 2037,
66
"a.wasm": 1308,
77
"a.wasm.gz": 876,
8-
"total": 5693,
9-
"total_gz": 3274
8+
"total": 5686,
9+
"total_gz": 3270
1010
}

0 commit comments

Comments
 (0)