Skip to content

Commit 8f1ce84

Browse files
authored
Replace hacky_postprocess_around_closure_limitations.py (#16459)
* Replace regex-based tools/hacky_postprocess_around_closure_limitations.py with an acorn AST-based unsafe optimizer. * Add missing ArrowFunctionExpression. Rebaseline code size test. * Fix TryStatement child traversal. * Fix comment * Apply review * eslint * Expand unsafe_optimizations in MODULARIZE builds. * Rebaseline * eslint * Add pass to simplify modularize function start * Rebaseline * Rerun a second simple pass of Closure after main unsafe optimizations * eslint * Optimize comma-chained operator news. Add internal test suite to Emscripten suite. * Rebaseline and restore code size test on Windows and mac.
1 parent ed8acb3 commit 8f1ce84

18 files changed

+413
-200
lines changed

emcc.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2857,13 +2857,13 @@ def phase_final_emitting(options, state, target, wasm_target, memfile):
28572857

28582858
module_export_name_substitution()
28592859

2860-
# Run a final regex pass to clean up items that were not possible to optimize by Closure, or unoptimalities that were left behind
2860+
# Run a final optimization pass to clean up items that were not possible to optimize by Closure, or unoptimalities that were left behind
28612861
# by processing steps that occurred after Closure.
2862-
if settings.MINIMAL_RUNTIME == 2 and settings.USE_CLOSURE_COMPILER and settings.DEBUG_LEVEL == 0 and not settings.SINGLE_FILE:
2863-
# Process .js runtime file. Note that we need to handle the license text
2864-
# here, so that it will not confuse the hacky script.
2865-
js_manipulation.handle_license(final_js)
2866-
shared.run_process([shared.PYTHON, utils.path_from_root('tools/hacky_postprocess_around_closure_limitations.py'), final_js])
2862+
if settings.MINIMAL_RUNTIME == 2 and settings.USE_CLOSURE_COMPILER and settings.DEBUG_LEVEL == 0:
2863+
shared.run_js_tool(utils.path_from_root('tools/unsafe_optimizations.js'), [final_js, '-o', final_js], cwd=utils.path_from_root('.'))
2864+
# Finally, rerun Closure compile with simple optimizations. It will be able to further minify the code. (n.b. it would not be safe
2865+
# to run in advanced mode)
2866+
final_js = building.closure_compiler(final_js, pretty=False, advanced=False, extra_closure_args=options.closure_args)
28672867

28682868
# Unmangle previously mangled `import.meta` references in both main code and libraries.
28692869
# See also: `preprocess` in parseTools.js.
@@ -3528,7 +3528,7 @@ def module_export_name_substitution():
35283528
src = re.sub(r'{\s*[\'"]?__EMSCRIPTEN_PRIVATE_MODULE_EXPORT_NAME_SUBSTITUTION__[\'"]?:\s*1\s*}', replacement, src)
35293529
# For Node.js and other shell environments, create an unminified Module object so that
35303530
# loading external .asm.js file that assigns to Module['asm'] works even when Closure is used.
3531-
if settings.MINIMAL_RUNTIME and (shared.target_environment_may_be('node') or shared.target_environment_may_be('shell')):
3531+
if settings.MINIMAL_RUNTIME and not settings.MODULARIZE and (shared.target_environment_may_be('node') or shared.target_environment_may_be('shell')):
35323532
src = 'if(typeof Module==="undefined"){var Module={};}\n' + src
35333533
write_file(final_js, src)
35343534
shared.configuration.get_temp_files().note(final_js)

tests/code_size/hello_wasm_worker_wasm.js

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,8 @@
1-
var b = Module;
2-
3-
var c = b.$ww, e, f;
4-
5-
e = b.mem || new WebAssembly.Memory({
1+
var b = Module, c = b.$ww, f, e = b.mem || new WebAssembly.Memory({
62
initial: 256,
73
maximum: 256,
84
shared: !0
9-
});
10-
11-
var g = e.buffer;
12-
13-
var h = {}, k = 1;
5+
}), g = e.buffer, h = {}, k = 1, m = [], p, q;
146

157
function l(a) {
168
m.push(a);
@@ -22,12 +14,8 @@ function n(a) {
2214
d && f.get(d)(...a.x);
2315
}
2416

25-
var m = [];
26-
2717
c && (h[0] = this, addEventListener("message", l));
2818

29-
var p, q;
30-
3119
WebAssembly.instantiate(b.wasm, {
3220
a: {
3321
b: function(a, d) {
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
{
22
"a.html": 737,
33
"a.html.gz": 433,
4-
"a.js": 753,
5-
"a.js.gz": 468,
4+
"a.js": 733,
5+
"a.js.gz": 463,
66
"a.wasm": 1800,
77
"a.wasm.gz": 994,
8-
"total": 3290,
9-
"total_gz": 1895
8+
"total": 3270,
9+
"total_gz": 1890
1010
}
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
{
22
"a.html": 569,
33
"a.html.gz": 379,
4-
"a.js": 4973,
5-
"a.js.gz": 2417,
4+
"a.js": 4962,
5+
"a.js.gz": 2422,
66
"a.wasm": 10458,
77
"a.wasm.gz": 6671,
8-
"total": 16000,
9-
"total_gz": 9467
8+
"total": 15989,
9+
"total_gz": 9472
1010
}
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
{
22
"a.html": 567,
33
"a.html.gz": 379,
4-
"a.js": 20046,
5-
"a.js.gz": 8213,
4+
"a.js": 18189,
5+
"a.js.gz": 8017,
66
"a.mem": 3171,
77
"a.mem.gz": 2714,
8-
"total": 23784,
9-
"total_gz": 11306
8+
"total": 21927,
9+
"total_gz": 11110
1010
}

tests/code_size/hello_webgl_wasm.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
{
22
"a.html": 569,
33
"a.html.gz": 379,
4-
"a.js": 4479,
5-
"a.js.gz": 2250,
4+
"a.js": 4447,
5+
"a.js.gz": 2247,
66
"a.wasm": 10458,
77
"a.wasm.gz": 6671,
8-
"total": 15506,
9-
"total_gz": 9300
8+
"total": 15474,
9+
"total_gz": 9297
1010
}
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
{
22
"a.html": 567,
33
"a.html.gz": 379,
4-
"a.js": 19531,
5-
"a.js.gz": 8049,
4+
"a.js": 17667,
5+
"a.js.gz": 7853,
66
"a.mem": 3171,
77
"a.mem.gz": 2714,
8-
"total": 23269,
9-
"total_gz": 11142
8+
"total": 21405,
9+
"total_gz": 10946
1010
}

tests/code_size/hello_world_wasm.js

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,21 @@
1-
var d = Module;
2-
3-
var e = new TextDecoder("utf8"), f, g, h;
1+
var d = Module, e = new TextDecoder("utf8"), f, g, h;
42

53
WebAssembly.instantiate(d.wasm, {
64
a: {
75
a: function(a) {
8-
var b = console, k = b.log;
6+
var c = console, k = c.log;
97
if (a) {
10-
for (var l = a + void 0, c = a; !(c >= l) && f[c]; ) ++c;
11-
a = e.decode(f.subarray(a, c));
8+
for (var l = a + void 0, b = a; !(b >= l) && f[b]; ) ++b;
9+
a = e.decode(f.subarray(a, b));
1210
} else a = "";
13-
k.call(b, a);
11+
k.call(c, a);
1412
}
1513
}
1614
}).then((function(a) {
1715
a = a.instance.exports;
1816
h = a.d;
1917
g = a.b;
20-
var b = g.buffer;
21-
f = new Uint8Array(b);
18+
f = new Uint8Array(g.buffer);
2219
a.c();
2320
h();
2421
}));

tests/code_size/hello_world_wasm.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
{
22
"a.html": 673,
33
"a.html.gz": 431,
4-
"a.js": 319,
5-
"a.js.gz": 261,
4+
"a.js": 308,
5+
"a.js.gz": 258,
66
"a.wasm": 104,
77
"a.wasm.gz": 112,
8-
"total": 1096,
9-
"total_gz": 804
8+
"total": 1085,
9+
"total_gz": 801
1010
}

tests/code_size/hello_world_wasm2js.js

Lines changed: 24 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1,73 +1,42 @@
1-
var b = Module;
2-
3-
var c = new TextDecoder("utf8"), d, f;
4-
5-
f = new function(a) {
1+
var b = Module, c = new TextDecoder("utf8"), f = new function(a) {
62
this.buffer = new ArrayBuffer(65536 * a.initial);
73
}({
84
initial: 256,
95
maximum: 256
10-
});
11-
12-
var h = f.buffer;
13-
14-
d = new Uint8Array(h);
15-
16-
var k = {
6+
}), h = f.buffer, d = new Uint8Array(h), k = {
177
a: function(a) {
18-
var g = console, m = g.log;
8+
var n = console, e = n.log;
199
if (a) {
20-
for (var n = a + void 0, e = a; !(e >= n) && d[e]; ) ++e;
21-
a = c.decode(d.subarray(a, e));
10+
for (var g = a + void 0, m = a; !(m >= g) && d[m]; ) ++m;
11+
a = c.decode(d.subarray(a, m));
2212
} else a = "";
23-
m.call(g, a);
13+
e.call(n, a);
2414
},
2515
memory: f
26-
}, l, p = (new function() {
27-
this.exports = function instantiate(v) {
28-
function c(d) {
29-
d.set = function(a, b) {
30-
this[a] = b;
16+
}, p = (new function() {
17+
this.exports = function(a) {
18+
function n(e) {
19+
e.set = function(g, m) {
20+
this[g] = m;
3121
};
32-
d.get = function(a) {
33-
return this[a];
22+
e.get = function(g) {
23+
return this[g];
3424
};
35-
return d;
25+
return e;
3626
}
37-
function t(u) {
38-
var e = Math.imul;
39-
var f = Math.fround;
40-
var g = Math.abs;
41-
var h = Math.clz32;
42-
var i = Math.min;
43-
var j = Math.max;
44-
var k = Math.floor;
45-
var l = Math.ceil;
46-
var m = Math.trunc;
47-
var n = Math.sqrt;
48-
var o = u.abort;
49-
var p = NaN;
50-
var q = Infinity;
51-
var r = u.a;
52-
function x(a, b) {
53-
a = a | 0;
54-
b = b | 0;
55-
r(1024);
56-
return 0;
57-
}
58-
function w() {}
59-
var s = c([]);
27+
return function(e) {
28+
var g = e.a;
6029
return {
61-
b: w,
62-
c: x,
63-
d: s
30+
b: function() {},
31+
c: function(m, q) {
32+
g(1024);
33+
return 0;
34+
},
35+
d: n([])
6436
};
65-
}
66-
return t(v);
37+
}(a);
6738
}(k);
68-
}).exports;
69-
70-
l = p.c;
39+
}).exports, l = p.c;
7140

7241
d.set(new Uint8Array(b.mem), 1024);
7342

0 commit comments

Comments
 (0)