Skip to content

Commit c2924cd

Browse files
authored
Add a codesize test for AUDIO_WORKLET. NFC (#24197)
I want to get baseline before landing #24190
1 parent 36423eb commit c2924cd

File tree

3 files changed

+196
-5
lines changed

3 files changed

+196
-5
lines changed

test/code_size/audio_worklet_wasm.js

+176
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,176 @@
1+
var g = globalThis.Module || "undefined" != typeof Module ? Module : {}, k = "undefined" !== typeof AudioWorkletGlobalScope, m = "em-ww" == globalThis.name, n, p, q, r, t, x, U, V, A, Q, C, W;
2+
3+
m && !k && (onmessage = a => {
4+
onmessage = null;
5+
a = a.data;
6+
g ||= {};
7+
Object.assign(g, a);
8+
t = a.mem;
9+
u();
10+
v();
11+
a.wasm = a.mem = 0;
12+
});
13+
14+
function u() {
15+
var a = t.buffer;
16+
p = new Uint8Array(a);
17+
n = new Int32Array(a);
18+
g.HEAPU32 = q = new Uint32Array(a);
19+
g.HEAPF32 = r = new Float32Array(a);
20+
}
21+
22+
if (k || !m) t = g.mem || new WebAssembly.Memory({
23+
initial: 256,
24+
maximum: 256,
25+
shared: !0
26+
}), u();
27+
28+
var w = [], y = a => {
29+
a = a.data;
30+
let b = a._wsc;
31+
b && x.get(b)(...a.x);
32+
}, z = a => {
33+
w.push(a);
34+
}, B = a => A(a), D = () => C(), G = (a, b, c, e) => {
35+
b = E[b];
36+
E[a].connect(b.destination || b, c, e);
37+
}, E = {}, H = 0, I = "undefined" != typeof TextDecoder ? new TextDecoder : void 0, J = (a = 0) => {
38+
for (var b = p, c = a + NaN, e = a; b[e] && !(e >= c); ) ++e;
39+
if (16 < e - a && b.buffer && I) return I.decode(b.slice(a, e));
40+
for (c = ""; a < e; ) {
41+
var d = b[a++];
42+
if (d & 128) {
43+
var h = b[a++] & 63;
44+
if (192 == (d & 224)) c += String.fromCharCode((d & 31) << 6 | h); else {
45+
var f = b[a++] & 63;
46+
d = 224 == (d & 240) ? (d & 15) << 12 | h << 6 | f : (d & 7) << 18 | h << 12 | f << 6 | b[a++] & 63;
47+
65536 > d ? c += String.fromCharCode(d) : (d -= 65536, c += String.fromCharCode(55296 | d >> 10, 56320 | d & 1023));
48+
}
49+
} else c += String.fromCharCode(d);
50+
}
51+
return c;
52+
}, K = a => {
53+
var b = window.AudioContext || window.webkitAudioContext;
54+
if (a >>= 2) {
55+
var c = q[a] ? (c = q[a]) ? J(c) : "" : void 0;
56+
a = {
57+
latencyHint: c,
58+
sampleRate: n[a + 1] || void 0
59+
};
60+
} else a = void 0;
61+
if (c = b) b = new b(a), E[++H] = b, c = H;
62+
return c;
63+
}, L = (a, b, c, e, d) => {
64+
if (c >>= 2) {
65+
var h = n[c], f = n[c + 1];
66+
if (q[c + 2]) {
67+
var l = q[c + 2] >> 2;
68+
c = n[c + 1];
69+
let F = [];
70+
for (;c--; ) F.push(q[l++]);
71+
l = F;
72+
} else l = void 0;
73+
e = {
74+
numberOfInputs: h,
75+
numberOfOutputs: f,
76+
outputChannelCount: l,
77+
processorOptions: {
78+
cb: e,
79+
ud: d,
80+
sc: 128
81+
}
82+
};
83+
} else e = void 0;
84+
a = new AudioWorkletNode(E[a], b ? J(b) : "", e);
85+
E[++H] = a;
86+
return H;
87+
}, M = (a, b, c, e) => {
88+
b >>= 2;
89+
let d = [], h = q[b + 1], f = q[b + 2] >> 2, l = 0;
90+
for (;h--; ) d.push({
91+
name: l++,
92+
defaultValue: r[f++],
93+
minValue: r[f++],
94+
maxValue: r[f++],
95+
automationRate: [ "a", "k" ][q[f++]] + "-rate"
96+
});
97+
h = E[a].audioWorklet.s.port;
98+
f = h.postMessage;
99+
b = (b = q[b]) ? J(b) : "";
100+
f.call(h, {
101+
_wpn: b,
102+
ap: d,
103+
ch: a,
104+
cb: c,
105+
ud: e
106+
});
107+
}, N = () => !1, O = 1, P = a => {
108+
a = a.data;
109+
let b = a._wsc;
110+
b && x.get(b)(...a.x);
111+
}, R = a => Q(a), S = (a, b, c, e, d) => {
112+
let h = E[a], f = h.audioWorklet, l = () => {
113+
x.get(e)(a, 0, d);
114+
};
115+
if (!f) return l();
116+
f.addModule("a.aw.js").then((() => {
117+
f.s = new AudioWorkletNode(h, "message", {
118+
processorOptions: {
119+
$ww: O++,
120+
wasm: g.wasm,
121+
mem: t,
122+
sb: b,
123+
sz: c
124+
}
125+
});
126+
f.s.port.onmessage = P;
127+
return f.addModule(g.js);
128+
})).then((() => {
129+
x.get(e)(a, 1, d);
130+
})).catch(l);
131+
};
132+
133+
function T(a) {
134+
let b = document.createElement("button");
135+
b.innerHTML = "Toggle playback";
136+
document.body.appendChild(b);
137+
a = E[a];
138+
b.onclick = () => {
139+
"running" != a.state ? a.resume() : a.suspend();
140+
};
141+
}
142+
143+
function v() {
144+
U = {
145+
f: T,
146+
g: G,
147+
d: K,
148+
h: L,
149+
e: M,
150+
b: N,
151+
c: S,
152+
a: t
153+
};
154+
WebAssembly.instantiate(g.wasm, {
155+
a: U
156+
}).then((a => {
157+
a = a.instance.exports;
158+
V = a.j;
159+
A = a.l;
160+
Q = a.m;
161+
C = a.n;
162+
W = a.o;
163+
x = a.k;
164+
g.stackSave = D;
165+
g.stackAlloc = R;
166+
g.stackRestore = B;
167+
g.wasmTable = x;
168+
m ? (a = g, W(a.sb, a.sz), "undefined" === typeof AudioWorkletGlobalScope && (removeEventListener("message", z),
169+
w = w.forEach(y), addEventListener("message", y))) : a.i();
170+
m || V();
171+
}));
172+
}
173+
174+
k && v();
175+
176+
m || v();
+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"a.html": 519,
3+
"a.html.gz": 364,
4+
"a.js": 2851,
5+
"a.js.gz": 1583,
6+
"a.aw.js": 2551,
7+
"a.aw.js.gz": 922,
8+
"a.wasm": 1287,
9+
"a.wasm.gz": 859,
10+
"total": 7208,
11+
"total_gz": 3728
12+
}

test/test_other.py

+8-5
Original file line numberDiff line numberDiff line change
@@ -11725,6 +11725,7 @@ def test_function_exports_are_small(self, args, opt, closure):
1172511725
self.assertEqual(num_times_export_is_referenced, 1)
1172611726

1172711727
@parameterized({
11728+
'audio_worklet': ('audio_worklet', False, True),
1172811729
'hello_world_wasm': ('hello_world', False, True),
1172911730
'hello_world_wasm2js': ('hello_world', True, True),
1173011731
'random_printf_wasm': ('random_printf', False),
@@ -11739,7 +11740,7 @@ def test_function_exports_are_small(self, args, opt, closure):
1173911740
'hello_embind': ('embind_hello', False),
1174011741
})
1174111742
@crossplatform
11742-
def test_minimal_runtime_code_size(self, test_name, js, compare_js_output=False):
11743+
def test_minimal_runtime_code_size(self, test_name, wasm2js, compare_js_output=False):
1174311744
smallest_code_size_args = ['-sMINIMAL_RUNTIME=2',
1174411745
'-sENVIRONMENT=web',
1174511746
'-sTEXTDECODER=2',
@@ -11763,8 +11764,6 @@ def test_minimal_runtime_code_size(self, test_name, js, compare_js_output=False)
1176311764
'-DNDEBUG',
1176411765
'-ffast-math']
1176511766

11766-
wasm2js = ['-sWASM=0']
11767-
1176811767
math_sources = [test_file('code_size/math.c')]
1176911768
hello_world_sources = [test_file('small_hello_world.c'),
1177011769
'-sMALLOC=none']
@@ -11778,6 +11777,7 @@ def test_minimal_runtime_code_size(self, test_name, js, compare_js_output=False)
1177811777
'-sMODULARIZE']
1177911778
hello_webgl2_sources = hello_webgl_sources + ['-sMAX_WEBGL_VERSION=2']
1178011779
hello_wasm_worker_sources = [test_file('wasm_worker/wasm_worker_code_size.c'), '-sWASM_WORKERS', '-sENVIRONMENT=web,worker']
11780+
audio_worklet_sources = [test_file('webaudio/audioworklet.c'), '-sWASM_WORKERS', '-sAUDIO_WORKLET', '-sENVIRONMENT=web,worker', '-sTEXTDECODER=1']
1178111781
embind_hello_sources = [test_file('code_size/embind_hello_world.cpp'), '-lembind']
1178211782
embind_val_sources = [test_file('code_size/embind_val_hello_world.cpp'),
1178311783
'-lembind',
@@ -11792,6 +11792,7 @@ def test_minimal_runtime_code_size(self, test_name, js, compare_js_output=False)
1179211792
'math': math_sources,
1179311793
'hello_webgl2': hello_webgl2_sources,
1179411794
'hello_wasm_worker': hello_wasm_worker_sources,
11795+
'audio_worklet': audio_worklet_sources,
1179511796
'embind_val': embind_val_sources,
1179611797
'embind_hello': embind_hello_sources,
1179711798
}[test_name]
@@ -11802,11 +11803,13 @@ def print_percent(actual, expected):
1180211803
return ' ({:+.2f}%)'.format((actual - expected) * 100.0 / expected)
1180311804

1180411805
outputs = ['a.html', 'a.js']
11806+
if '-sAUDIO_WORKLET' in sources:
11807+
outputs += ['a.aw.js']
1180511808

1180611809
args = smallest_code_size_args[:]
1180711810

11808-
if js:
11809-
args += wasm2js
11811+
if wasm2js:
11812+
args += ['-sWASM=0']
1181011813
test_name += '_wasm2js'
1181111814
else:
1181211815
outputs += ['a.wasm']

0 commit comments

Comments
 (0)