Skip to content

Commit 1cd1031

Browse files
authored
Run unsafe_optimizations.js again after closure (#21196)
This allows `unsafe_optimizations.js` to cleanup some thing that closure compiler was able to remove usages of. Split out from #21195
1 parent 0e5b3a8 commit 1cd1031

12 files changed

+46
-67
lines changed

test/code_size/embind_val_wasm.json

+4-4
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": 7411,
5-
"a.js.gz": 3140,
4+
"a.js": 7387,
5+
"a.js.gz": 3112,
66
"a.wasm": 11433,
77
"a.wasm.gz": 5725,
8-
"total": 19517,
9-
"total_gz": 9296
8+
"total": 19493,
9+
"total_gz": 9268
1010
}
+4-4
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": 667,
5-
"a.js.gz": 458,
4+
"a.js": 665,
5+
"a.js.gz": 455,
66
"a.wasm": 1852,
77
"a.wasm.gz": 1049,
8-
"total": 3256,
9-
"total_gz": 1940
8+
"total": 3254,
9+
"total_gz": 1937
1010
}

test/code_size/hello_webgl2_wasm.json

+4-4
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": 4593,
5-
"a.js.gz": 2369,
4+
"a.js": 4584,
5+
"a.js.gz": 2351,
66
"a.wasm": 10451,
77
"a.wasm.gz": 6724,
8-
"total": 15613,
9-
"total_gz": 9472
8+
"total": 15604,
9+
"total_gz": 9454
1010
}
+4-4
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": 17937,
5-
"a.js.gz": 8074,
4+
"a.js": 17760,
5+
"a.js.gz": 7958,
66
"a.mem": 3123,
77
"a.mem.gz": 2693,
8-
"total": 21627,
9-
"total_gz": 11146
8+
"total": 21450,
9+
"total_gz": 11030
1010
}

test/code_size/hello_webgl_wasm.json

+4-4
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": 4080,
5-
"a.js.gz": 2198,
4+
"a.js": 4071,
5+
"a.js.gz": 2179,
66
"a.wasm": 10451,
77
"a.wasm.gz": 6724,
8-
"total": 15100,
9-
"total_gz": 9301
8+
"total": 15091,
9+
"total_gz": 9282
1010
}
+4-4
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": 17415,
5-
"a.js.gz": 7891,
4+
"a.js": 17239,
5+
"a.js.gz": 7781,
66
"a.mem": 3123,
77
"a.mem.gz": 2693,
8-
"total": 21105,
9-
"total_gz": 10963
8+
"total": 20929,
9+
"total_gz": 10853
1010
}

test/code_size/hello_world_wasm.json

+4-4
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": 292,
5-
"a.js.gz": 252,
4+
"a.js": 291,
5+
"a.js.gz": 249,
66
"a.wasm": 103,
77
"a.wasm.gz": 112,
8-
"total": 1068,
9-
"total_gz": 795
8+
"total": 1067,
9+
"total_gz": 792
1010
}
+4-4
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
{
22
"a.html": 671,
33
"a.html.gz": 430,
4-
"a.js": 694,
5-
"a.js.gz": 433,
4+
"a.js": 692,
5+
"a.js.gz": 430,
66
"a.mem": 6,
77
"a.mem.gz": 32,
8-
"total": 1371,
9-
"total_gz": 895
8+
"total": 1369,
9+
"total_gz": 892
1010
}

test/code_size/math_wasm.json

+4-4
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": 111,
5-
"a.js.gz": 127,
4+
"a.js": 110,
5+
"a.js.gz": 125,
66
"a.wasm": 2728,
77
"a.wasm.gz": 1672,
8-
"total": 3512,
9-
"total_gz": 2230
8+
"total": 3511,
9+
"total_gz": 2228
1010
}
+4-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
2-
"a.html": 17331,
3-
"a.html.gz": 7543,
4-
"total": 17331,
5-
"total_gz": 7543
2+
"a.html": 17264,
3+
"a.html.gz": 7529,
4+
"total": 17264,
5+
"total_gz": 7529
66
}

tools/link.py

+6-1
Original file line numberDiff line numberDiff line change
@@ -1974,6 +1974,7 @@ def fix_es6_import_statements(js_file):
19741974
write_file(js_file, src
19751975
.replace('EMSCRIPTEN$IMPORT$META', 'import.meta')
19761976
.replace('EMSCRIPTEN$AWAIT$IMPORT', 'await import'))
1977+
save_intermediate('es6-module')
19771978

19781979

19791980
def create_worker_file(input_file, target_dir, output_file):
@@ -2019,13 +2020,17 @@ def phase_final_emitting(options, state, target, wasm_target, memfile):
20192020
# steps that occurred after Closure.
20202021
if settings.MINIMAL_RUNTIME == 2 and settings.USE_CLOSURE_COMPILER and settings.DEBUG_LEVEL == 0:
20212022
shared.run_js_tool(utils.path_from_root('tools/unsafe_optimizations.js'), [final_js, '-o', final_js], cwd=utils.path_from_root('.'))
2023+
save_intermediate('unsafe-optimizations')
20222024
# Finally, rerun Closure compile with simple optimizations. It will be able
20232025
# to further minify the code. (n.b. it would not be safe to run in advanced
20242026
# mode)
20252027
final_js = building.closure_compiler(final_js, advanced=False, extra_closure_args=options.closure_args)
2028+
# Run unsafe_optimizations.js once more. This allows the cleanup of newly
2029+
# unused things that closure compiler leaves behing (e.g `new Float64Array(x)`).
2030+
shared.run_js_tool(utils.path_from_root('tools/unsafe_optimizations.js'), [final_js, '-o', final_js], cwd=utils.path_from_root('.'))
2031+
save_intermediate('unsafe-optimizations2')
20262032

20272033
fix_es6_import_statements(final_js)
2028-
save_intermediate('es6-module')
20292034

20302035
# Apply pre and postjs files
20312036
if options.extern_pre_js or options.extern_post_js:

tools/unsafe_optimizations.js

-26
Original file line numberDiff line numberDiff line change
@@ -68,27 +68,6 @@ function optPassSimplifyModularizeFunction(ast) {
6868
});
6969
}
7070

71-
// Closure integration of the Module object generates an awkward "var b; b || (b = Module);" code.
72-
// 'b || (b = Module)' -> 'b = Module'.
73-
function optPassSimplifyModuleInitialization(ast) {
74-
visitNodes(ast, ['BlockStatement', 'Program'], (node) => {
75-
for (const n of node.body) {
76-
if (
77-
n.type == 'ExpressionStatement' &&
78-
n.expression.type == 'LogicalExpression' &&
79-
n.expression.operator == '||' &&
80-
n.expression.left.name === n.expression.right.left?.name &&
81-
n.expression.right.right.name == 'Module'
82-
) {
83-
// Clear out the logical operator.
84-
n.expression = n.expression.right;
85-
// There is only one Module assignment, so can finish the pass here.
86-
return false;
87-
}
88-
}
89-
});
90-
}
91-
9271
// Finds redundant operator new statements that are not assigned anywhere.
9372
// (we aren't interested in side effects of the calls if no assignment)
9473
function optPassRemoveRedundantOperatorNews(ast) {
@@ -225,7 +204,6 @@ function optPassMergeVarInitializationAssignments(ast) {
225204
function runOnJsText(js, pretty = false) {
226205
const ast = acorn.parse(js, {ecmaVersion: 2021});
227206

228-
optPassSimplifyModuleInitialization(ast);
229207
optPassRemoveRedundantOperatorNews(ast);
230208

231209
let progress = true;
@@ -273,10 +251,6 @@ function runTests() {
273251
'var Module=function(f){};',
274252
);
275253

276-
// optPassSimplifyModuleInitialization:
277-
test('b || (b = Module);', 'b=Module;');
278-
test('function foo(){b || (b = Module);}', 'function foo(){b=Module}');
279-
280254
// optPassRemoveRedundantOperatorNews:
281255
test('new Uint16Array(a);', '');
282256
test('new Uint16Array(a),new Uint16Array(a);', ';');

0 commit comments

Comments
 (0)