diff --git a/test/code_size/embind_val_wasm.json b/test/code_size/embind_val_wasm.json index 77462a448e15a..5943d126972e8 100644 --- a/test/code_size/embind_val_wasm.json +++ b/test/code_size/embind_val_wasm.json @@ -1,10 +1,10 @@ { "a.html": 673, "a.html.gz": 431, - "a.js": 7411, - "a.js.gz": 3140, + "a.js": 7387, + "a.js.gz": 3112, "a.wasm": 11433, "a.wasm.gz": 5725, - "total": 19517, - "total_gz": 9296 + "total": 19493, + "total_gz": 9268 } diff --git a/test/code_size/hello_wasm_worker_wasm.json b/test/code_size/hello_wasm_worker_wasm.json index 174ae41255448..b384a4163717b 100644 --- a/test/code_size/hello_wasm_worker_wasm.json +++ b/test/code_size/hello_wasm_worker_wasm.json @@ -1,10 +1,10 @@ { "a.html": 737, "a.html.gz": 433, - "a.js": 667, - "a.js.gz": 458, + "a.js": 665, + "a.js.gz": 455, "a.wasm": 1852, "a.wasm.gz": 1049, - "total": 3256, - "total_gz": 1940 + "total": 3254, + "total_gz": 1937 } diff --git a/test/code_size/hello_webgl2_wasm.json b/test/code_size/hello_webgl2_wasm.json index 708a528a29e1f..c7dbff7f256eb 100644 --- a/test/code_size/hello_webgl2_wasm.json +++ b/test/code_size/hello_webgl2_wasm.json @@ -1,10 +1,10 @@ { "a.html": 569, "a.html.gz": 379, - "a.js": 4681, - "a.js.gz": 2412, + "a.js": 4671, + "a.js.gz": 2396, "a.wasm": 10388, "a.wasm.gz": 6692, - "total": 15638, - "total_gz": 9483 + "total": 15628, + "total_gz": 9467 } diff --git a/test/code_size/hello_webgl2_wasm2js.json b/test/code_size/hello_webgl2_wasm2js.json index 823857f3e7a81..ac8c5b2a4fba6 100644 --- a/test/code_size/hello_webgl2_wasm2js.json +++ b/test/code_size/hello_webgl2_wasm2js.json @@ -1,10 +1,10 @@ { "a.html": 567, "a.html.gz": 379, - "a.js": 17917, - "a.js.gz": 8089, + "a.js": 17766, + "a.js.gz": 7996, "a.mem": 3123, "a.mem.gz": 2693, - "total": 21607, - "total_gz": 11161 + "total": 21456, + "total_gz": 11068 } diff --git a/test/code_size/hello_webgl_wasm.json b/test/code_size/hello_webgl_wasm.json index 1d73a396b7380..684fe7701a196 100644 --- a/test/code_size/hello_webgl_wasm.json +++ b/test/code_size/hello_webgl_wasm.json @@ -1,10 +1,10 @@ { "a.html": 569, "a.html.gz": 379, - "a.js": 4167, - "a.js.gz": 2236, + "a.js": 4158, + "a.js.gz": 2220, "a.wasm": 10388, "a.wasm.gz": 6692, - "total": 15124, - "total_gz": 9307 + "total": 15115, + "total_gz": 9291 } diff --git a/test/code_size/hello_webgl_wasm2js.json b/test/code_size/hello_webgl_wasm2js.json index cb6d778b2b99e..25367cc8b201e 100644 --- a/test/code_size/hello_webgl_wasm2js.json +++ b/test/code_size/hello_webgl_wasm2js.json @@ -1,10 +1,10 @@ { "a.html": 567, "a.html.gz": 379, - "a.js": 17395, - "a.js.gz": 7900, + "a.js": 17239, + "a.js.gz": 7827, "a.mem": 3123, "a.mem.gz": 2693, - "total": 21085, - "total_gz": 10972 + "total": 20929, + "total_gz": 10899 } diff --git a/test/code_size/hello_world_wasm.json b/test/code_size/hello_world_wasm.json index ab32b871f761a..b61bdb6c8f74c 100644 --- a/test/code_size/hello_world_wasm.json +++ b/test/code_size/hello_world_wasm.json @@ -1,10 +1,10 @@ { "a.html": 673, "a.html.gz": 431, - "a.js": 292, - "a.js.gz": 252, + "a.js": 291, + "a.js.gz": 249, "a.wasm": 103, "a.wasm.gz": 112, - "total": 1068, - "total_gz": 795 + "total": 1067, + "total_gz": 792 } diff --git a/test/code_size/hello_world_wasm2js.js b/test/code_size/hello_world_wasm2js.js index c93291a460944..90abfb3c8c621 100644 --- a/test/code_size/hello_world_wasm2js.js +++ b/test/code_size/hello_world_wasm2js.js @@ -1,59 +1,64 @@ -var b = Module, g = new function(a) { - this.buffer = new ArrayBuffer(65536 * a.initial); -}({ - initial: 256, - maximum: 256 -}), k = g.buffer, d = new Uint8Array(k), l = new TextDecoder("utf8"), m; +var c = Module, g, h, k = new TextDecoder("utf8"), l; -function c(a) { - this.exports = function(h) { - function n(f) { - f.set = function(e, p) { - this[e] = p; +function d(b) { + this.exports = function(f) { + function m(e) { + e.set = function(a, n) { + this[a] = n; }; - f.get = function(e) { - return this[e]; + e.get = function(a) { + return this[a]; }; - return f; + return e; } - return function(f) { - var e = f.a.a; + return function(e) { + var a = new ArrayBuffer(16777216), n = e.a.a; + e = m([]); return { - b: function() {}, - c: function(p, q) { - e(1024); + b: Object.create(Object.prototype, { + grow: {}, + buffer: { + get: function() { + return a; + } + } + }), + c: function() {}, + d: function(p, q) { + n(1024); return 0; }, - d: n([]) + e: e }; - }(h); - }(a); + }(f); + }(b); } -(function(a, h) { +(function(b, f) { return { - then: function(n) { - n({ - instance: new c(h) + then: function(m) { + m({ + instance: new d(f) }); } }; -})(b.wasm, { +})(c.wasm, { a: { - a: a => { - var h = console, n = h.log; - if (a) { - for (var f = a + void 0, e = a; !(e >= f) && d[e]; ) ++e; - a = l.decode(d.subarray(a, e)); - } else a = ""; - n.call(h, a); - }, - memory: g + a: b => { + var f = console, m = f.log; + if (b) { + for (var e = b + void 0, a = b; !(a >= e) && g[a]; ) ++a; + b = k.decode(g.subarray(b, a)); + } else b = ""; + m.call(f, b); + } } -}).then((a => { - a = a.instance.exports; - m = a.c; - d.set(new Uint8Array(b.mem), 1024); - a.b(); - m(); +}).then((b => { + b = b.instance.exports; + l = b.d; + h = b.b; + g = new Uint8Array(h.buffer); + g.set(new Uint8Array(c.mem), 1024); + b.c(); + l(); })); \ No newline at end of file diff --git a/test/code_size/hello_world_wasm2js.json b/test/code_size/hello_world_wasm2js.json index f4e43530f0c1a..6c26b7a43f595 100644 --- a/test/code_size/hello_world_wasm2js.json +++ b/test/code_size/hello_world_wasm2js.json @@ -1,10 +1,10 @@ { "a.html": 671, "a.html.gz": 430, - "a.js": 694, - "a.js.gz": 433, + "a.js": 708, + "a.js.gz": 444, "a.mem": 6, "a.mem.gz": 32, - "total": 1371, - "total_gz": 895 + "total": 1385, + "total_gz": 906 } diff --git a/test/code_size/math_wasm.json b/test/code_size/math_wasm.json index 5526b37a985d7..e5040389b0724 100644 --- a/test/code_size/math_wasm.json +++ b/test/code_size/math_wasm.json @@ -1,10 +1,10 @@ { "a.html": 673, "a.html.gz": 431, - "a.js": 111, - "a.js.gz": 127, + "a.js": 110, + "a.js.gz": 125, "a.wasm": 2728, "a.wasm.gz": 1672, - "total": 3512, - "total_gz": 2230 + "total": 3511, + "total_gz": 2228 } diff --git a/test/code_size/random_printf_wasm2js.json b/test/code_size/random_printf_wasm2js.json index b2c93945084b3..04fef72bf4570 100644 --- a/test/code_size/random_printf_wasm2js.json +++ b/test/code_size/random_printf_wasm2js.json @@ -1,6 +1,6 @@ { - "a.html": 17331, - "a.html.gz": 7543, - "total": 17331, - "total_gz": 7543 + "a.html": 17279, + "a.html.gz": 7541, + "total": 17279, + "total_gz": 7541 } diff --git a/tools/link.py b/tools/link.py index 97882439c9eb4..e4b0517b4d14b 100644 --- a/tools/link.py +++ b/tools/link.py @@ -1445,8 +1445,7 @@ def check_memory_setting(setting): (options.shell_path == DEFAULT_SHELL_HTML or options.shell_path == utils.path_from_root('src/shell_minimal.html')): exit_with_error(f'Due to collision in variable name "Module", the shell file "{options.shell_path}" is not compatible with build options "-sMODULARIZE -sEXPORT_NAME=Module". Either provide your own shell file, change the name of the export to something else to avoid the name collision. (see https://github.com/emscripten-core/emscripten/issues/7950 for details)') - # TODO(sbc): Remove WASM2JS here once the size regression it would introduce has been fixed. - if settings.SHARED_MEMORY or settings.RELOCATABLE or settings.ASYNCIFY_LAZY_LOAD_CODE or settings.WASM2JS: + if settings.SHARED_MEMORY or settings.RELOCATABLE or settings.ASYNCIFY_LAZY_LOAD_CODE: settings.IMPORTED_MEMORY = 1 if settings.WASM_BIGINT: @@ -1974,6 +1973,7 @@ def fix_es6_import_statements(js_file): write_file(js_file, src .replace('EMSCRIPTEN$IMPORT$META', 'import.meta') .replace('EMSCRIPTEN$AWAIT$IMPORT', 'await import')) + save_intermediate('es6-module') def create_worker_file(input_file, target_dir, output_file): @@ -2019,13 +2019,17 @@ def phase_final_emitting(options, state, target, wasm_target, memfile): # steps that occurred after Closure. if settings.MINIMAL_RUNTIME == 2 and settings.USE_CLOSURE_COMPILER and settings.DEBUG_LEVEL == 0: shared.run_js_tool(utils.path_from_root('tools/unsafe_optimizations.js'), [final_js, '-o', final_js], cwd=utils.path_from_root('.')) + save_intermediate('unsafe-optimizations') # Finally, rerun Closure compile with simple optimizations. It will be able # to further minify the code. (n.b. it would not be safe to run in advanced # mode) final_js = building.closure_compiler(final_js, advanced=False, extra_closure_args=options.closure_args) + # Run unsafe_optimizations.js once more. This allows the cleanup of newly + # unused things that closure compiler leaves behing (e.g `new Float64Array(x)`). + shared.run_js_tool(utils.path_from_root('tools/unsafe_optimizations.js'), [final_js, '-o', final_js], cwd=utils.path_from_root('.')) + save_intermediate('unsafe-optimizations2') fix_es6_import_statements(final_js) - save_intermediate('es6-module') # Apply pre and postjs files if options.extern_pre_js or options.extern_post_js: diff --git a/tools/unsafe_optimizations.js b/tools/unsafe_optimizations.js index 45e8d01c3545b..67d1d67a2dfa0 100644 --- a/tools/unsafe_optimizations.js +++ b/tools/unsafe_optimizations.js @@ -68,27 +68,6 @@ function optPassSimplifyModularizeFunction(ast) { }); } -// Closure integration of the Module object generates an awkward "var b; b || (b = Module);" code. -// 'b || (b = Module)' -> 'b = Module'. -function optPassSimplifyModuleInitialization(ast) { - visitNodes(ast, ['BlockStatement', 'Program'], (node) => { - for (const n of node.body) { - if ( - n.type == 'ExpressionStatement' && - n.expression.type == 'LogicalExpression' && - n.expression.operator == '||' && - n.expression.left.name === n.expression.right.left?.name && - n.expression.right.right.name == 'Module' - ) { - // Clear out the logical operator. - n.expression = n.expression.right; - // There is only one Module assignment, so can finish the pass here. - return false; - } - } - }); -} - // Finds redundant operator new statements that are not assigned anywhere. // (we aren't interested in side effects of the calls if no assignment) function optPassRemoveRedundantOperatorNews(ast) { @@ -225,7 +204,6 @@ function optPassMergeVarInitializationAssignments(ast) { function runOnJsText(js, pretty = false) { const ast = acorn.parse(js, {ecmaVersion: 2021}); - optPassSimplifyModuleInitialization(ast); optPassRemoveRedundantOperatorNews(ast); let progress = true; @@ -273,10 +251,6 @@ function runTests() { 'var Module=function(f){};', ); - // optPassSimplifyModuleInitialization: - test('b || (b = Module);', 'b=Module;'); - test('function foo(){b || (b = Module);}', 'function foo(){b=Module}'); - // optPassRemoveRedundantOperatorNews: test('new Uint16Array(a);', ''); test('new Uint16Array(a),new Uint16Array(a);', ';');