Skip to content

Commit ba160e1

Browse files
committed
Don't force the use of imported memory under WASM2JS
Setting `IMPORTED_MEMORY` can have other implications and don't want t by default just because we chose WASM2JS. The there is a very minor code size regression here that only effects WASM2JS, but when combined with emscripten-core#21196 the overall effect is still a win. See emscripten-core#21071
1 parent 93a23bc commit ba160e1

7 files changed

+66
-62
lines changed
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": 17760,
5-
"a.js.gz": 7958,
4+
"a.js": 17790,
5+
"a.js.gz": 7987,
66
"a.mem": 3123,
77
"a.mem.gz": 2693,
8-
"total": 21450,
9-
"total_gz": 11030
8+
"total": 21480,
9+
"total_gz": 11059
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": 17239,
5-
"a.js.gz": 7781,
4+
"a.js": 17263,
5+
"a.js.gz": 7819,
66
"a.mem": 3123,
77
"a.mem.gz": 2693,
8-
"total": 20929,
9-
"total_gz": 10853
8+
"total": 20953,
9+
"total_gz": 10891
1010
}

test/code_size/hello_world_wasm2js.js

Lines changed: 47 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,59 +1,64 @@
1-
var b = Module, g = new function(a) {
2-
this.buffer = new ArrayBuffer(65536 * a.initial);
3-
}({
4-
initial: 256,
5-
maximum: 256
6-
}), k = g.buffer, d = new Uint8Array(k), l = new TextDecoder("utf8"), m;
1+
var c = Module, g, h, k = new TextDecoder("utf8"), l;
72

8-
function c(a) {
9-
this.exports = function(h) {
10-
function n(f) {
11-
f.set = function(e, p) {
12-
this[e] = p;
3+
function d(b) {
4+
this.exports = function(f) {
5+
function m(e) {
6+
e.set = function(a, n) {
7+
this[a] = n;
138
};
14-
f.get = function(e) {
15-
return this[e];
9+
e.get = function(a) {
10+
return this[a];
1611
};
17-
return f;
12+
return e;
1813
}
19-
return function(f) {
20-
var e = f.a.a;
14+
return function(e) {
15+
var a = new ArrayBuffer(16777216), n = e.a.a;
16+
e = m([]);
2117
return {
22-
b: function() {},
23-
c: function(p, q) {
24-
e(1024);
18+
b: Object.create(Object.prototype, {
19+
grow: {},
20+
buffer: {
21+
get: function() {
22+
return a;
23+
}
24+
}
25+
}),
26+
c: function() {},
27+
d: function(p, q) {
28+
n(1024);
2529
return 0;
2630
},
27-
d: n([])
31+
e: e
2832
};
29-
}(h);
30-
}(a);
33+
}(f);
34+
}(b);
3135
}
3236

33-
(function(a, h) {
37+
(function(b, f) {
3438
return {
35-
then: function(n) {
36-
n({
37-
instance: new c(h)
39+
then: function(m) {
40+
m({
41+
instance: new d(f)
3842
});
3943
}
4044
};
41-
})(b.wasm, {
45+
})(c.wasm, {
4246
a: {
43-
a: a => {
44-
var h = console, n = h.log;
45-
if (a) {
46-
for (var f = a + void 0, e = a; !(e >= f) && d[e]; ) ++e;
47-
a = l.decode(d.subarray(a, e));
48-
} else a = "";
49-
n.call(h, a);
50-
},
51-
memory: g
47+
a: b => {
48+
var f = console, m = f.log;
49+
if (b) {
50+
for (var e = b + void 0, a = b; !(a >= e) && g[a]; ) ++a;
51+
b = k.decode(g.subarray(b, a));
52+
} else b = "";
53+
m.call(f, b);
54+
}
5255
}
53-
}).then((a => {
54-
a = a.instance.exports;
55-
m = a.c;
56-
d.set(new Uint8Array(b.mem), 1024);
57-
a.b();
58-
m();
56+
}).then((b => {
57+
b = b.instance.exports;
58+
l = b.d;
59+
h = b.b;
60+
g = new Uint8Array(h.buffer);
61+
g.set(new Uint8Array(c.mem), 1024);
62+
b.c();
63+
l();
5964
}));
Lines changed: 4 additions & 4 deletions
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": 692,
5-
"a.js.gz": 430,
4+
"a.js": 708,
5+
"a.js.gz": 444,
66
"a.mem": 6,
77
"a.mem.gz": 32,
8-
"total": 1369,
9-
"total_gz": 892
8+
"total": 1385,
9+
"total_gz": 906
1010
}
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
2-
"a.html": 17264,
3-
"a.html.gz": 7529,
4-
"total": 17264,
5-
"total_gz": 7529
2+
"a.html": 17279,
3+
"a.html.gz": 7541,
4+
"total": 17279,
5+
"total_gz": 7541
66
}

test/test_browser.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3664,7 +3664,7 @@ def test_modularize_and_preload_files(self):
36643664
create_file('dummy_file', 'dummy')
36653665
# compile the code with the modularize feature and the preload-file option enabled
36663666
# no wasm, since this tests customizing total memory at runtime
3667-
self.compile_btest('test.c', ['-sWASM=0', '-sMODULARIZE', '-sEXPORT_NAME="Foo"', '--preload-file', 'dummy_file'] + opts, reporting=Reporting.JS_ONLY)
3667+
self.compile_btest('test.c', ['-sWASM=0', '-sIMPORTED_MEMORY', '-sMODULARIZE', '-sEXPORT_NAME="Foo"', '--preload-file', 'dummy_file'] + opts, reporting=Reporting.JS_ONLY)
36683668
create_file('a.html', '''
36693669
<script src="a.out.js"></script>
36703670
<script>
@@ -4817,7 +4817,7 @@ def test_webgpu_get_device_pthreads(self):
48174817
# Preallocating the buffer in this was is asm.js only (wasm needs a Memory).
48184818
@requires_wasm2js
48194819
def test_preallocated_heap(self):
4820-
self.btest_exit('test_preallocated_heap.cpp', args=['-sWASM=0', '-sINITIAL_MEMORY=16MB', '-sABORTING_MALLOC=0', '--shell-file', test_file('test_preallocated_heap_shell.html')])
4820+
self.btest_exit('test_preallocated_heap.cpp', args=['-sWASM=0', '-sIMPORTED_MEMORY', '-sINITIAL_MEMORY=16MB', '-sABORTING_MALLOC=0', '--shell-file', test_file('test_preallocated_heap_shell.html')])
48214821

48224822
# Tests emscripten_fetch() usage to XHR data directly to memory without persisting results to IndexedDB.
48234823
@also_with_wasm2js

tools/link.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1445,8 +1445,7 @@ def check_memory_setting(setting):
14451445
(options.shell_path == DEFAULT_SHELL_HTML or options.shell_path == utils.path_from_root('src/shell_minimal.html')):
14461446
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)')
14471447

1448-
# TODO(sbc): Remove WASM2JS here once the size regression it would introduce has been fixed.
1449-
if settings.SHARED_MEMORY or settings.RELOCATABLE or settings.ASYNCIFY_LAZY_LOAD_CODE or settings.WASM2JS:
1448+
if settings.SHARED_MEMORY or settings.RELOCATABLE or settings.ASYNCIFY_LAZY_LOAD_CODE:
14501449
settings.IMPORTED_MEMORY = 1
14511450

14521451
if settings.WASM_BIGINT:

0 commit comments

Comments
 (0)