Skip to content

Check full JS output for hello world #12806

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 18, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions tests/code_size/hello_world_wasm.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
var d = Module;

var e = new TextDecoder("utf8");

function f(a) {
if (!a) return "";
for (var b = a + NaN, c = a; !(c >= b) && g[c]; ) ++c;
return e.decode(g.subarray(a, c));
}

var g, h, k;

WebAssembly.instantiate(d.wasm, {
a: {
a: function(a) {
console.log(f(a));
}
}
}).then((function(a) {
a = a.instance.exports;
k = a.e;
h = a.b;
var b = h.buffer;
g = new Uint8Array(b);
a.d();
k();
}));
79 changes: 79 additions & 0 deletions tests/code_size/hello_world_wasm2js.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
var b = Module;

var c = new TextDecoder("utf8");

function e(a) {
if (!a) return "";
for (var m = a + NaN, d = a; !(d >= m) && f[d]; ) ++d;
return c.decode(f.subarray(a, d));
}

var f, g;

g = new function(a) {
this.buffer = new ArrayBuffer(65536 * a.initial);
}({
initial: 256,
maximum: 256
});

var h = g.buffer;

f = new Uint8Array(h);

var k = {
a: function(a) {
console.log(e(a));
},
memory: g
}, l, n = (new function() {
this.exports = function instantiate(t) {
function r(u) {
u.set = function(v, w) {
this[v] = w;
};
u.get = function(v) {
return this[v];
};
return u;
}
function s(x) {
var a = Math.imul;
var b = Math.fround;
var c = Math.abs;
var d = Math.clz32;
var e = Math.min;
var f = Math.max;
var g = Math.floor;
var h = Math.ceil;
var i = Math.trunc;
var j = Math.sqrt;
var k = x.abort;
var l = NaN;
var m = Infinity;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yikes - This looks like something that could be low hanging to DCE? I wonder why Closure did not clean them.. :/

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@alon... What phase would we expect to clean this up? Is it something that closure should be able deal with?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe this is inside the wasm2js output. The JS optimizer doesn't normally run on that. We usually don't run a JS minifier on compiled code, as it could be massive. But you can try --closure 2 which I think does that.

An option is to handle this in wasm2js, that is, track what is needed and emit only that. Not sure how easy it would be.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But that about closure compiler itself.. .shouldn't it be able to eliminate these unused variables?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Closure would be able to do it., but closure does not run on the wasm2js output - except for --closure 2 (because, as I said, the compiled code can be massive).

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah I see. Thanks. I wonder if we should be running with --closure 2 for these minimal codesize tests? @juj?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because it falls down on huge files, I don't think we recommend --closure 2. But it would be good to investigate that, as maybe closure has gotten faster/uses less memory now?

var n = x.a;
function q(a, b) {
a = a | 0;
b = b | 0;
n(1024);
return 0;
}
function p() {}
var o = r([]);
return {
b: o,
c: p,
d: q
};
}
return s(t);
}(k);
}).exports;

l = n.d;

f.set(new Uint8Array(b.mem), 1024);

n.c();

l();
21 changes: 18 additions & 3 deletions tests/test_other.py
Original file line number Diff line number Diff line change
Expand Up @@ -8189,16 +8189,16 @@ def test(args, closure, opt):
@no_windows("Code size is slightly different on Windows")
@no_mac("Code size is slightly different on Mac")
@parameterized({
'hello_world_wasm': ('hello_world', False),
'hello_world_wasm2js': ('hello_world', True),
'hello_world_wasm': ('hello_world', False, True),
'hello_world_wasm2js': ('hello_world', True, True),
'random_printf_wasm': ('random_printf', False),
'random_printf_wasm2js': ('random_printf', True),
'hello_webgl_wasm': ('hello_webgl', False),
'hello_webgl_wasm2js': ('hello_webgl', True),
'hello_webgl2_wasm': ('hello_webgl2', False),
'hello_webgl2_wasm2js': ('hello_webgl2', True),
})
def test_minimal_runtime_code_size(self, test_name, js):
def test_minimal_runtime_code_size(self, test_name, js, compare_js_output=False):
smallest_code_size_args = ['-s', 'MINIMAL_RUNTIME=2',
'-s', 'ENVIRONMENT=web',
'-s', 'TEXTDECODER=2',
Expand Down Expand Up @@ -8286,6 +8286,21 @@ def get_file_gzipped_size(f):
try_delete(f_gz)
return size

# For certain tests, don't just check the output size but check
# the full JS output matches the expectations. That means that
# any change that touches those core lines of output will need
# to rebaseline this test. However:
# a) such changes deserve extra scrutiny
# b) such changes should be few and far between
# c) rebaselining is trivial (just run with EMTEST_REBASELINE=1)
# Note that we do not compare the full wasm output since that is
# even more fragile and can change with LLVM updates.
if compare_js_output:
js_out = path_from_root('tests', 'code_size', test_name + '.js')
terser = shared.get_npm_cmd('terser')
self.run_process(terser + ['-b', 'beautify=true', 'a.js', '-o', 'pretty.js'])
self.assertFileContents(js_out, open('pretty.js').read())

obtained_results = {}
total_output_size = 0
total_expected_size = 0
Expand Down