Skip to content

Commit c4c8052

Browse files
committed
[wasm2js] Remove all handling for external memory file
We have a lot of support code and complexity in emscripten for handling of external memory files. However, its only ever used in wasm2js mode which is a legacy mode. The only reason we continue to support it here IIUC is because its slightly more space efficient than embedding the data as base64. For small programs like hello_world this is an over codesize win. For larger programs there is a regression in overall size in proportion to the amount of static data in the program.
1 parent a3b9509 commit c4c8052

16 files changed

+109
-371
lines changed

ChangeLog.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ See docs/process.md for more on how version tagging works.
2121
3.1.55 (in development)
2222
-----------------------
2323
- Update sdl2-mixer port from 2.6.0 to 2.8.0
24+
- Emscripten no longer supported extracting static data and serving it as a
25+
separate `.mem` data file. The feature was already only available under
26+
wasm2js (`-sWASM=0`) so this change will only effect users of this setting.
2427

2528
3.1.54 - 02/15/24
2629
-----------------
@@ -58,7 +61,6 @@ See docs/process.md for more on how version tagging works.
5861
- Allow comments in response files. Any line starting with `#` is now ignored.
5962
This is useful when listing exported symbols. (#21330)
6063

61-
6264
3.1.53 - 01/29/24
6365
-----------------
6466
- The llvm version that emscripten uses was updated to 19.0.0 trunk. (#21165)

emcc.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,6 @@ def __init__(self):
145145
self.emrun = False
146146
self.cpu_profiler = False
147147
self.memory_profiler = False
148-
self.memory_init_file = None
149148
self.use_preload_cache = False
150149
self.use_preload_plugins = False
151150
self.valid_abspaths = []
@@ -1316,7 +1315,7 @@ def consume_arg_file():
13161315
ports.show_ports()
13171316
should_exit = True
13181317
elif check_arg('--memory-init-file'):
1319-
options.memory_init_file = int(consume_arg())
1318+
exit_with_error('--memory-init-file is no longer supported')
13201319
elif check_flag('--proxy-to-worker'):
13211320
settings_changes.append('PROXY_TO_WORKER=1')
13221321
elif check_arg('--valid-abspath'):

src/postamble_minimal.js

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -221,13 +221,6 @@ WebAssembly.instantiate(Module['wasm'], imports).then((output) => {
221221
updateMemoryViews();
222222
#endif
223223

224-
#if !MEM_INIT_IN_WASM && !SINGLE_FILE
225-
#if ASSERTIONS
226-
if (!Module['mem']) throw 'Must load memory initializer as an ArrayBuffer in to variable Module.mem before adding compiled output .js script to the DOM';
227-
#endif
228-
HEAPU8.set(new Uint8Array(Module['mem']), {{{ GLOBAL_BASE }}});
229-
#endif
230-
231224
initRuntime(wasmExports);
232225
#if PTHREADS
233226
// Export Wasm module for pthread creation to access.

src/preamble.js

Lines changed: 0 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -990,9 +990,6 @@ function createWasm() {
990990
#endif
991991
updateMemoryViews();
992992
#endif
993-
#if !MEM_INIT_IN_WASM
994-
runMemoryInitializer();
995-
#endif
996993

997994
#if '$wasmTable' in addedLibraryItems && !RELOCATABLE
998995
wasmTable = wasmExports['__indirect_function_table'];
@@ -1140,88 +1137,6 @@ function getCompilerSetting(name) {
11401137
}
11411138
#endif // RETAIN_COMPILER_SETTINGS
11421139

1143-
#if !MEM_INIT_IN_WASM
1144-
var memoryInitializer = <<< MEM_INITIALIZER >>>;
1145-
1146-
function runMemoryInitializer() {
1147-
#if PTHREADS
1148-
if (ENVIRONMENT_IS_PTHREAD) return;
1149-
#endif
1150-
if (!isDataURI(memoryInitializer)) {
1151-
memoryInitializer = locateFile(memoryInitializer);
1152-
}
1153-
if (ENVIRONMENT_IS_NODE || ENVIRONMENT_IS_SHELL) {
1154-
var data = readBinary(memoryInitializer);
1155-
HEAPU8.set(data, {{{ GLOBAL_BASE }}});
1156-
} else {
1157-
addRunDependency('memory initializer');
1158-
var applyMemoryInitializer = (data) => {
1159-
if (data.byteLength) data = new Uint8Array(data);
1160-
#if ASSERTIONS
1161-
for (var i = 0; i < data.length; i++) {
1162-
assert(HEAPU8[{{{ GLOBAL_BASE }}} + i] === 0, "area for memory initializer should not have been touched before it's loaded");
1163-
}
1164-
#endif
1165-
HEAPU8.set(data, {{{ GLOBAL_BASE }}});
1166-
// Delete the typed array that contains the large blob of the memory initializer request response so that
1167-
// we won't keep unnecessary memory lying around. However, keep the XHR object itself alive so that e.g.
1168-
// its .status field can still be accessed later.
1169-
if (Module['memoryInitializerRequest']) delete Module['memoryInitializerRequest'].response;
1170-
removeRunDependency('memory initializer');
1171-
};
1172-
var doBrowserLoad = () => {
1173-
readAsync(memoryInitializer, applyMemoryInitializer, () => {
1174-
var e = new Error('could not load memory initializer ' + memoryInitializer);
1175-
#if MODULARIZE
1176-
readyPromiseReject(e);
1177-
#else
1178-
throw e;
1179-
#endif
1180-
});
1181-
};
1182-
#if SUPPORT_BASE64_EMBEDDING
1183-
var memoryInitializerBytes = tryParseAsDataURI(memoryInitializer);
1184-
if (memoryInitializerBytes) {
1185-
applyMemoryInitializer(memoryInitializerBytes.buffer);
1186-
} else
1187-
#endif
1188-
if (Module['memoryInitializerRequest']) {
1189-
// a network request has already been created, just use that
1190-
var useRequest = () => {
1191-
var request = Module['memoryInitializerRequest'];
1192-
var response = request.response;
1193-
if (request.status !== 200 && request.status !== 0) {
1194-
#if SUPPORT_BASE64_EMBEDDING
1195-
var data = tryParseAsDataURI(Module['memoryInitializerRequestURL']);
1196-
if (data) {
1197-
response = data.buffer;
1198-
} else {
1199-
#endif
1200-
// If you see this warning, the issue may be that you are using locateFile and defining it in JS. That
1201-
// means that the HTML file doesn't know about it, and when it tries to create the mem init request early, does it to the wrong place.
1202-
// Look in your browser's devtools network console to see what's going on.
1203-
console.warn('a problem seems to have happened with Module.memoryInitializerRequest, status: ' + request.status + ', retrying ' + memoryInitializer);
1204-
doBrowserLoad();
1205-
return;
1206-
#if SUPPORT_BASE64_EMBEDDING
1207-
}
1208-
#endif
1209-
}
1210-
applyMemoryInitializer(response);
1211-
};
1212-
if (Module['memoryInitializerRequest'].response) {
1213-
setTimeout(useRequest, 0); // it's already here; but, apply it asynchronously
1214-
} else {
1215-
Module['memoryInitializerRequest'].addEventListener('load', useRequest); // wait for it
1216-
}
1217-
} else {
1218-
// fetch it from the network ourselves
1219-
doBrowserLoad();
1220-
}
1221-
}
1222-
}
1223-
#endif // MEM_INIT_IN_WASM == 0
1224-
12251140
#if MAIN_MODULE && ASYNCIFY
12261141
// With MAIN_MODULE + ASYNCIFY the normal method of placing stub functions in
12271142
// wasmImports for as-yet-undefined symbols doesn't work since ASYNCIFY then

src/settings_internal.js

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -143,11 +143,6 @@ var AUDIO_WORKLET_FILE = '';
143143
// Base URL the source mapfile, if relevant
144144
var SOURCE_MAP_BASE = '';
145145

146-
// When this is false we use an external memory init file
147-
// See --memory-init-file. When not using wasm2js this flag is ignored, and
148-
// this setting will always be true.
149-
var MEM_INIT_IN_WASM = true;
150-
151146
// If set to 1, src/base64Utils.js will be included in the bundle.
152147
// This is set internally when needed (SINGLE_FILE)
153148
var SUPPORT_BASE64_EMBEDDING = false;

src/shell_minimal.js

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ if (ENVIRONMENT_IS_NODE && ENVIRONMENT_IS_SHELL) {
8282
#endif
8383

8484
#if !SINGLE_FILE
85-
#if ENVIRONMENT_MAY_BE_NODE && ((WASM == 1 && (!WASM2JS || !MEM_INIT_IN_WASM)) || WASM == 2)
85+
#if ENVIRONMENT_MAY_BE_NODE && ((WASM == 1 && !WASM2JS) || WASM == 2)
8686
// Wasm or Wasm2JS loading:
8787

8888
if (ENVIRONMENT_IS_NODE) {
@@ -95,13 +95,10 @@ if (ENVIRONMENT_IS_NODE) {
9595
Module['wasm'] = fs.readFileSync(__dirname + '/{{{ TARGET_BASENAME }}}.wasm');
9696
#endif
9797
#endif
98-
#if !MEM_INIT_IN_WASM
99-
Module['mem'] = fs.readFileSync(__dirname + '/{{{ TARGET_BASENAME }}}.mem');
100-
#endif
10198
}
10299
#endif
103100

104-
#if ENVIRONMENT_MAY_BE_SHELL && ((WASM == 1 && (!WASM2JS || !MEM_INIT_IN_WASM)) || WASM == 2)
101+
#if ENVIRONMENT_MAY_BE_SHELL && ((WASM == 1 && !WASM2JS) || WASM == 2)
105102
if (ENVIRONMENT_IS_SHELL) {
106103
#if WASM == 2
107104
if (typeof WebAssembly != 'undefined') Module['wasm'] = read('{{{ TARGET_BASENAME }}}.wasm', 'binary');
@@ -111,9 +108,6 @@ if (ENVIRONMENT_IS_SHELL) {
111108
Module['wasm'] = read('{{{ TARGET_BASENAME }}}.wasm', 'binary');
112109
#endif
113110
#endif
114-
#if !MEM_INIT_IN_WASM
115-
Module['mem'] = read('{{{ TARGET_BASENAME }}}.mem', 'binary');
116-
#endif
117111
}
118112
#endif
119113

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
{
2-
"a.html": 567,
3-
"a.html.gz": 379,
4-
"a.js": 17800,
5-
"a.js.gz": 7981,
6-
"a.mem": 3123,
7-
"a.mem.gz": 2693,
8-
"total": 21490,
9-
"total_gz": 11053
2+
"a.html": 354,
3+
"a.html.gz": 266,
4+
"a.js": 22323,
5+
"a.js.gz": 11632,
6+
"total": 22677,
7+
"total_gz": 11898
108
}
Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
{
2-
"a.html": 567,
3-
"a.html.gz": 379,
4-
"a.js": 17272,
5-
"a.js.gz": 7814,
6-
"a.mem": 3123,
7-
"a.mem.gz": 2693,
8-
"total": 20962,
9-
"total_gz": 10886
2+
"a.html": 354,
3+
"a.html.gz": 266,
4+
"a.js": 21794,
5+
"a.js.gz": 11450,
6+
"total": 22148,
7+
"total_gz": 11716
108
}

test/code_size/hello_world_wasm2js.js

Lines changed: 35 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,30 @@
1-
var c = Module, g, h, k = new TextDecoder("utf8"), l;
1+
var d = Module, g, h, k = new TextDecoder("utf8"), l;
22

3-
function d(b) {
4-
this.exports = function(f) {
5-
function m(e) {
6-
e.set = function(a, n) {
7-
this[a] = n;
3+
function e(b) {
4+
this.exports = function(r) {
5+
function u(c) {
6+
c.set = function(a, f) {
7+
this[a] = f;
88
};
9-
e.get = function(a) {
9+
c.get = function(a) {
1010
return this[a];
1111
};
12-
return e;
12+
return c;
1313
}
14-
return function(e) {
15-
var a = new ArrayBuffer(16777216), n = e.a.a;
16-
e = m([]);
14+
function x(c, a, f) {
15+
for (var v, p = 0, t = a, w = f.length, y = a + (3 * w >> 2) - ("=" == f[w - 2]) - ("=" == f[w - 1]); p < w; p += 4) a = m[f.charCodeAt(p + 1)],
16+
v = m[f.charCodeAt(p + 2)], c[t++] = m[f.charCodeAt(p)] << 2 | a >> 4, t < y && (c[t++] = a << 4 | v >> 2),
17+
t < y && (c[t++] = v << 6 | m[f.charCodeAt(p + 3)]);
18+
}
19+
for (var q, m = new Uint8Array(123), n = 25; 0 <= n; --n) m[48 + n] = 52 + n, m[65 + n] = n,
20+
m[97 + n] = 26 + n;
21+
m[43] = 62;
22+
m[47] = 63;
23+
return function(c) {
24+
var a = new ArrayBuffer(16777216), f = new Uint8Array(a), v = c.a.a;
25+
q = f;
26+
x(q, 1024, "aGVsbG8h");
27+
c = u([]);
1728
return {
1829
b: Object.create(Object.prototype, {
1930
grow: {},
@@ -24,41 +35,40 @@ function d(b) {
2435
}
2536
}),
2637
c: function() {},
27-
d: function(p, q) {
28-
n(1024);
38+
d: function(p, t) {
39+
v(1024);
2940
return 0;
3041
},
31-
e: e
42+
e: c
3243
};
33-
}(f);
44+
}(r);
3445
}(b);
3546
}
3647

37-
(function(b, f) {
48+
(function(b, r) {
3849
return {
39-
then: function(m) {
40-
m({
41-
instance: new d(f)
50+
then: function(u) {
51+
u({
52+
instance: new e(r)
4253
});
4354
}
4455
};
45-
})(c.wasm, {
56+
})(d.wasm, {
4657
a: {
4758
a: b => {
48-
var f = console, m = f.log;
59+
var r = console, u = r.log;
4960
if (b) {
50-
for (var e = b + void 0, a = b; !(a >= e) && g[a]; ) ++a;
51-
b = k.decode(g.subarray(b, a));
61+
for (var x = b + void 0, q = b; !(q >= x) && g[q]; ) ++q;
62+
b = k.decode(g.subarray(b, q));
5263
} else b = "";
53-
m.call(f, b);
64+
u.call(r, b);
5465
}
5566
}
5667
}).then((b => {
5768
b = b.instance.exports;
5869
l = b.d;
5970
h = b.b;
6071
g = new Uint8Array(h.buffer);
61-
g.set(new Uint8Array(c.mem), 1024);
6272
b.c();
6373
l();
6474
}));
Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
{
2-
"a.html": 671,
3-
"a.html.gz": 430,
4-
"a.js": 708,
5-
"a.js.gz": 444,
6-
"a.mem": 6,
7-
"a.mem.gz": 32,
8-
"total": 1385,
9-
"total_gz": 906
2+
"a.html": 323,
3+
"a.html.gz": 253,
4+
"a.js": 1060,
5+
"a.js.gz": 636,
6+
"total": 1383,
7+
"total_gz": 889
108
}

test/common.py

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -847,17 +847,6 @@ def setup_node_pthreads(self):
847847
self.js_engines = [nodejs]
848848
self.node_args += shared.node_pthread_flags(nodejs)
849849

850-
def uses_memory_init_file(self):
851-
if self.get_setting('SIDE_MODULE') or self.is_wasm():
852-
return False
853-
854-
if '--memory-init-file' in self.emcc_args:
855-
return int(self.emcc_args[self.emcc_args.index('--memory-init-file') + 1])
856-
857-
# side modules handle memory differently; binaryen puts the memory in the wasm module
858-
opt_supports = any(opt in self.emcc_args for opt in ('-O2', '-O3', '-Os', '-Oz'))
859-
return opt_supports
860-
861850
def set_temp_dir(self, temp_dir):
862851
self.temp_dir = temp_dir
863852
self.canonical_temp_dir = get_canonical_temp_dir(self.temp_dir)
@@ -1104,11 +1093,6 @@ def build(self, filename, libraries=None, includes=None, force_c=False, js_outfi
11041093
self.run_process(cmd, stderr=self.stderr_redirect if not DEBUG else None)
11051094
self.assertExists(output)
11061095

1107-
if js_outfile and self.uses_memory_init_file():
1108-
src = read_file(output)
1109-
# side memory init file, or an empty one in the js
1110-
assert ('/* memory initializer */' not in src) or ('/* memory initializer */ allocate([]' in src)
1111-
11121096
return output
11131097

11141098
def get_func(self, src, name):

0 commit comments

Comments
 (0)