Skip to content
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

Initial new INITIAL_HEAP setting #21071

Merged
merged 29 commits into from
Mar 1, 2024
Merged
Show file tree
Hide file tree
Changes from 17 commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
b43181e
Initial INITIAL_HEAP support
SingleAccretion Jan 12, 2024
79bf8d7
Comment out the memory size assert
SingleAccretion Jan 13, 2024
4def2c7
Adjust a couple tests
SingleAccretion Jan 13, 2024
5ddd199
Rebaseline a couple code size tests
SingleAccretion Jan 13, 2024
d893b4d
A better INITIAL_HEAP description
SingleAccretion Jan 28, 2024
79958b2
Remove the INITIAL_MEMORY assert
SingleAccretion Jan 28, 2024
a4e3cfc
Single quotes in python
SingleAccretion Jan 28, 2024
f994f46
Prettify the code a little
SingleAccretion Jan 28, 2024
1433dd8
Make test_sysconf_phys_pages the default initial memory test
SingleAccretion Jan 28, 2024
c8b285e
Add a change log entry
SingleAccretion Jan 28, 2024
eae41e0
Recommend INITIAL_HEAP
SingleAccretion Jan 28, 2024
3da94a8
Also update the doc
SingleAccretion Jan 28, 2024
0906505
Merge branch 'main' into Initial-Heap
SingleAccretion Jan 28, 2024
a3786ec
Merge fix...
SingleAccretion Jan 28, 2024
3c76f5b
Merge branch 'main' into Initial-Heap
SingleAccretion Jan 30, 2024
db45ed2
Rebaseline once more
SingleAccretion Jan 30, 2024
d71d239
More rebaselining
SingleAccretion Jan 30, 2024
47ec651
Work around the WASM2JS regression
SingleAccretion Jan 30, 2024
b443e06
Better comments and documentation
SingleAccretion Jan 30, 2024
0dc6457
Merge branch 'main' into Initial-Heap
SingleAccretion Feb 15, 2024
9a885f1
Fix up the change log
SingleAccretion Feb 15, 2024
d2cdd4e
Use @parameterized for the test
SingleAccretion Feb 15, 2024
a37cafe
Brush-ups
SingleAccretion Feb 15, 2024
1ce8cd6
Delete some files commited by mistake
SingleAccretion Feb 17, 2024
a36cf25
Merge branch 'main' into Initial-Heap
SingleAccretion Mar 1, 2024
d89c454
Use --no-growable-memory when memory growth is not allowed
SingleAccretion Mar 1, 2024
ea3b557
Change log fixup
SingleAccretion Mar 1, 2024
d688893
Rebaseline a test
SingleAccretion Mar 1, 2024
37eeea5
Test nits and fixes
SingleAccretion Mar 1, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions ChangeLog.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,10 @@ See docs/process.md for more on how version tagging works.
- C++ objects passed into embind's val via constructors, methods, and call
function will not be automatically destroyed after the function call. This
makes the behavior consistent for invocations.
- `INITIAL_HEAP` setting is introduced to control the amount of initial
memory available for dynamic allocation without capping it. If you are
using `INITIAL_MEMORY`, consider switching to `INITIAL_HEAP`. Note that
it is currently not supported in all configurations (#21071).
SingleAccretion marked this conversation as resolved.
Show resolved Hide resolved
- The `SUPPORT_ERRNO` setting is now deprecated as it only controlled setting
errno from JS library functions and emscripten no longer requires this.
(#21074)
Expand Down
14 changes: 14 additions & 0 deletions site/source/docs/tools_reference/settings_reference.rst
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,17 @@ Note that this setting does not affect the behavior of operator new in C++.
This function will always abort on allocation failure if exceptions are disabled.
If you want new to return 0 on failure, use it with std::nothrow.

.. _initial_heap:

INITIAL_HEAP
============

The initial amount of heap memory available to the program. This is the
memory region available for dynamic allocations via `sbrk`, `malloc` and `new`.

Unlike INITIAL_MEMORY, this setting does not place a hard cap on the total
amount of memory available at startup, and so should be preferred.
SingleAccretion marked this conversation as resolved.
Show resolved Hide resolved

.. _initial_memory:

INITIAL_MEMORY
Expand All @@ -162,6 +173,9 @@ we need to copy the old heap into a new one in that case.
If ALLOW_MEMORY_GROWTH is set, this initial amount of memory can increase
later; if not, then it is the final and total amount of memory.

By default, this value is calculated based on INITIAL_HEAP, STACK_SIZE,
as well the size of static data in input modules.
SingleAccretion marked this conversation as resolved.
Show resolved Hide resolved

(This option was formerly called TOTAL_MEMORY.)

.. _maximum_memory:
Expand Down
1 change: 0 additions & 1 deletion src/postamble_minimal.js
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,6 @@ WebAssembly.instantiate(Module['wasm'], imports).then((output) => {
wasmMemory = wasmExports['memory'];
#if ASSERTIONS
assert(wasmMemory);
assert(wasmMemory.buffer.byteLength === {{{ INITIAL_MEMORY }}});
#endif
updateMemoryViews();
#endif
Expand Down
4 changes: 0 additions & 4 deletions src/preamble.js
Original file line number Diff line number Diff line change
Expand Up @@ -983,10 +983,6 @@ function createWasm() {
{{{ receivedSymbol('wasmMemory') }}}
#if ASSERTIONS
assert(wasmMemory, 'memory not found in wasm exports');
// This assertion doesn't hold when emscripten is run in --post-link
// mode.
// TODO(sbc): Read INITIAL_MEMORY out of the wasm file in post-link mode.
//assert(wasmMemory.buffer.byteLength === {{{ INITIAL_MEMORY }}});
#endif
updateMemoryViews();
#endif
Expand Down
14 changes: 13 additions & 1 deletion src/settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -154,15 +154,27 @@ var MALLOC = "dlmalloc";
// [link]
var ABORTING_MALLOC = true;

// The initial amount of heap memory available to the program. This is the
// memory region available for dynamic allocations via `sbrk`, `malloc` and `new`.
//
// Unlike INITIAL_MEMORY, this setting does not place a hard cap on the total
// amount of memory available at startup, and so should be preferred.
//
// [link]
var INITIAL_HEAP = 16777216;

// The initial amount of memory to use. Using more memory than this will
// cause us to expand the heap, which can be costly with typed arrays:
// we need to copy the old heap into a new one in that case.
// If ALLOW_MEMORY_GROWTH is set, this initial amount of memory can increase
// later; if not, then it is the final and total amount of memory.
//
// By default, this value is calculated based on INITIAL_HEAP, STACK_SIZE,
// as well the size of static data in input modules.
//
// (This option was formerly called TOTAL_MEMORY.)
// [link]
var INITIAL_MEMORY = 16777216;
var INITIAL_MEMORY = -1;

// Set the maximum size of memory in the wasm module (in bytes). This is only
// relevant when ALLOW_MEMORY_GROWTH is set, as without growth, the size of
Expand Down
8 changes: 4 additions & 4 deletions test/code_size/embind_val_wasm.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
"a.html.gz": 431,
"a.js": 7387,
"a.js.gz": 3112,
"a.wasm": 11433,
"a.wasm.gz": 5725,
"total": 19493,
"total_gz": 9268
"a.wasm": 11434,
"a.wasm.gz": 5726,
"total": 19494,
"total_gz": 9269
}
8 changes: 4 additions & 4 deletions test/code_size/hello_webgl2_wasm.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
"a.html.gz": 379,
"a.js": 4584,
"a.js.gz": 2351,
"a.wasm": 10451,
"a.wasm.gz": 6724,
"total": 15604,
"total_gz": 9454
"a.wasm": 10452,
"a.wasm.gz": 6725,
"total": 15605,
"total_gz": 9455
}
8 changes: 4 additions & 4 deletions test/code_size/hello_webgl2_wasm2js.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{
"a.html": 567,
"a.html.gz": 379,
"a.js": 17790,
"a.js.gz": 7987,
"a.js": 18102,
"a.js.gz": 8079,
"a.mem": 3123,
"a.mem.gz": 2693,
"total": 21480,
"total_gz": 11059
"total": 21792,
"total_gz": 11151
}
8 changes: 4 additions & 4 deletions test/code_size/hello_webgl_wasm.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
"a.html.gz": 379,
"a.js": 4071,
"a.js.gz": 2179,
"a.wasm": 10451,
"a.wasm.gz": 6724,
"total": 15091,
"total_gz": 9282
"a.wasm": 10452,
"a.wasm.gz": 6725,
"total": 15092,
"total_gz": 9283
}
8 changes: 4 additions & 4 deletions test/code_size/hello_webgl_wasm2js.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{
"a.html": 567,
"a.html.gz": 379,
"a.js": 17263,
"a.js.gz": 7819,
"a.js": 17575,
"a.js.gz": 7906,
"a.mem": 3123,
"a.mem.gz": 2693,
"total": 20953,
"total_gz": 10891
"total": 21265,
"total_gz": 10978
}
8 changes: 4 additions & 4 deletions test/code_size/hello_world_wasm.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
"a.html.gz": 431,
"a.js": 291,
"a.js.gz": 249,
"a.wasm": 103,
"a.wasm.gz": 112,
"total": 1067,
"total_gz": 792
"a.wasm": 104,
"a.wasm.gz": 114,
"total": 1068,
"total_gz": 794
}
72 changes: 40 additions & 32 deletions test/code_size/hello_world_wasm2js.js
Original file line number Diff line number Diff line change
@@ -1,64 +1,72 @@
var c = Module, g, h, k = new TextDecoder("utf8"), l;

function d(b) {
this.exports = function(f) {
function m(e) {
e.set = function(a, n) {
this[a] = n;
function d(e) {
this.exports = function(m) {
function n(f) {
f.set = function(a, p) {
this[a] = p;
};
e.get = function(a) {
f.get = function(a) {
return this[a];
};
return e;
return f;
}
return function(e) {
var a = new ArrayBuffer(16777216), n = e.a.a;
e = m([]);
return function(f) {
var a = new ArrayBuffer(16908288), p = new Int8Array(a), r = Math.imul, t = f.a.a;
f = n([]);
return {
b: Object.create(Object.prototype, {
grow: {},
grow: {
value: function(b) {
var q = a.byteLength / 65536 | 0;
b = q + (b | 0) | 0;
q < b && 65536 > b && (b = new ArrayBuffer(r(b, 65536)), new Int8Array(b).set(p),
p = new Int8Array(b), a = b);
return q;
}
},
SingleAccretion marked this conversation as resolved.
Show resolved Hide resolved
buffer: {
get: function() {
return a;
}
}
}),
c: function() {},
d: function(p, q) {
n(1024);
d: function(b, q) {
t(1024);
return 0;
},
e: e
e: f
};
}(f);
}(b);
}(m);
}(e);
}

(function(b, f) {
(function(e, m) {
return {
then: function(m) {
m({
instance: new d(f)
then: function(n) {
n({
instance: new d(m)
});
}
};
})(c.wasm, {
a: {
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);
a: e => {
var m = console, n = m.log;
if (e) {
for (var f = e + void 0, a = e; !(a >= f) && g[a]; ) ++a;
e = k.decode(g.subarray(e, a));
} else e = "";
n.call(m, e);
}
}
}).then((b => {
b = b.instance.exports;
l = b.d;
h = b.b;
}).then((e => {
e = e.instance.exports;
l = e.d;
h = e.b;
g = new Uint8Array(h.buffer);
g.set(new Uint8Array(c.mem), 1024);
b.c();
e.c();
l();
}));
8 changes: 4 additions & 4 deletions test/code_size/hello_world_wasm2js.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{
"a.html": 671,
"a.html.gz": 430,
"a.js": 708,
"a.js.gz": 444,
"a.js": 898,
"a.js.gz": 529,
"a.mem": 6,
"a.mem.gz": 32,
"total": 1385,
"total_gz": 906
"total": 1575,
"total_gz": 991
}
8 changes: 4 additions & 4 deletions test/code_size/math_wasm.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
"a.html.gz": 431,
"a.js": 110,
"a.js.gz": 125,
"a.wasm": 2728,
"a.wasm.gz": 1672,
"total": 3511,
"total_gz": 2228
"a.wasm": 2729,
"a.wasm.gz": 1673,
"total": 3512,
"total_gz": 2229
}
8 changes: 4 additions & 4 deletions test/code_size/random_printf_wasm2js.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"a.html": 17279,
"a.html.gz": 7541,
"total": 17279,
"total_gz": 7541
"a.html": 17576,
"a.html.gz": 7653,
"total": 17576,
"total_gz": 7653
}
3 changes: 2 additions & 1 deletion test/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -2103,7 +2103,7 @@ def test_memorygrowth_geometric_step(self):
if not self.is_wasm():
self.skipTest('wasm memory specific test')

self.emcc_args += ['-sALLOW_MEMORY_GROWTH', '-sMEMORY_GROWTH_GEOMETRIC_STEP=8.5', '-sMEMORY_GROWTH_GEOMETRIC_CAP=32MB']
self.emcc_args += ['-sINITIAL_MEMORY=16MB', '-sALLOW_MEMORY_GROWTH', '-sMEMORY_GROWTH_GEOMETRIC_STEP=8.5', '-sMEMORY_GROWTH_GEOMETRIC_CAP=32MB']
self.do_core_test('test_memorygrowth_geometric_step.c')

def test_memorygrowth_3_force_fail_reallocBuffer(self):
Expand Down Expand Up @@ -5997,6 +5997,7 @@ def test_unistd_sysconf_phys_pages(self):
assert self.get_setting('INITIAL_MEMORY') == '2200mb'
expected = (2200 * 1024 * 1024) // webassembly.WASM_PAGE_SIZE
else:
self.set_setting('INITIAL_MEMORY', '16mb')
expected = 16 * 1024 * 1024 // webassembly.WASM_PAGE_SIZE
self.do_runf(filename, str(expected) + ', errno: 0')

Expand Down
31 changes: 29 additions & 2 deletions test/test_other.py
Original file line number Diff line number Diff line change
Expand Up @@ -6507,7 +6507,7 @@ def test_failing_alloc(self):
printf("managed another malloc!\n");
}
''' % (pre_fail, post_fail))
args = [EMXX, 'main.cpp', '-sEXPORTED_FUNCTIONS=_main,_sbrk'] + opts + aborting_args
args = [EMXX, 'main.cpp', '-sEXPORTED_FUNCTIONS=_main,_sbrk', '-sINITIAL_MEMORY=16MB'] + opts + aborting_args
args += ['-sTEST_MEMORY_GROWTH_FAILS'] # In this test, force memory growing to fail
if growth:
args += ['-sALLOW_MEMORY_GROWTH']
Expand Down Expand Up @@ -8026,6 +8026,30 @@ def test_binaryen_warn_mem(self):
self.run_process([EMCC, test_file('hello_world.c'), '-sINITIAL_MEMORY=' + str(16 * 1024 * 1024), '--pre-js', 'pre.js', '-sALLOW_MEMORY_GROWTH', '-sWASM_ASYNC_COMPILATION=0', '-sIMPORTED_MEMORY'])
self.assertContained('hello, world!', self.run_js('a.out.js'))

def test_initial_heap(self):
for args, expected_initial_heap in [
([], 16 * 1024 * 1024), # Default behavior: 16MB initial heap
(['-sINITIAL_MEMORY=40MB'], 0), # Backwards compatibility: no initial heap (we can't tell if it'll fit)
(['-sMAXIMUM_MEMORY=40MB'], 0), # Backwards compatibility: no initial heap (we can't tell if it'll fit)
(['-sINITIAL_HEAP=64KB'], 64 * 1024), # Explicitly set initial heap is passed
(['-sINITIAL_HEAP=128KB', '-sINITIAL_MEMORY=20MB', '-sMAXIMUM_MEMORY=40MB'], 128 * 1024),
(['-sINITIAL_HEAP=10MB', '-sINITIAL_MEMORY=10MB'], -1), # Not enough space for stack
(['-sINITIAL_HEAP=5MB', '-sMAXIMUM_MEMORY=5MB'], -1), # Not enough space for stack
]:
SingleAccretion marked this conversation as resolved.
Show resolved Hide resolved
cmd = [EMCC, test_file('hello_world.c'), '-v'] + args
print(' '.join(cmd))

if expected_initial_heap < 0:
out = self.expect_fail(cmd)
self.assertContained('wasm-ld: error:', out)
SingleAccretion marked this conversation as resolved.
Show resolved Hide resolved
continue

out = self.run_process(cmd, stderr=PIPE)
if expected_initial_heap != 0:
self.assertContained('--initial-heap=' + str(expected_initial_heap), out.stderr)
else:
self.assertNotContained('--initial-heap=', out.stderr)
Copy link
Collaborator

Choose a reason for hiding this comment

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

If possible, I'd prefer not to test this by peeking inside like this but I guess there is no other easy way?

I suppose we could compare __heap_base with __builtin_wasm_memory_size within the code? But that might not work for == 0 case here.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It was an intentional decision do this sort of "white box" testing. There were two reasons:

  1. It's easier. As you mention, it is somewhat non-obvious how to test the 0 case.
  2. It's composable in a sense that it tests only the functionality we're adding (i. e. we're not adding the feature that lays out a memory in a particular way, we're adding a feature that makes it such that a particular argument is passed to the linker).

The upside is that it is more resilient against changes in the other parts of the system. The downside is that it assumes the process involves invoking the linker, but it seems unlikely that the linker will go away any time soon (interestingly, writing a test with __heap_base would also assume a very particular linker implementation).


def test_memory_size(self):
for args, expect_initial, expect_max in [
([], 320, 320),
Expand Down Expand Up @@ -8055,6 +8079,9 @@ def test_invalid_mem(self):
self.assertContained('hello, world!', self.run_js('a.out.js'))

# Must be a multiple of 64KB
ret = self.expect_fail([EMCC, test_file('hello_world.c'), '-sINITIAL_HEAP=32505857', '-sALLOW_MEMORY_GROWTH']) # 31MB + 1 byte
self.assertContained('INITIAL_HEAP must be a multiple of WebAssembly page size (64KiB)', ret)

ret = self.expect_fail([EMCC, test_file('hello_world.c'), '-sINITIAL_MEMORY=33554433']) # 32MB + 1 byte
self.assertContained('INITIAL_MEMORY must be a multiple of WebAssembly page size (64KiB)', ret)

Expand Down Expand Up @@ -8505,7 +8532,7 @@ def run(args, expected):
result = self.run_js('a.out.js').strip()
self.assertEqual(result, f'{expected}, errno: 0')

run([], 256)
run([], 258)
run(['-sINITIAL_MEMORY=32MB'], 512)
run(['-sINITIAL_MEMORY=32MB', '-sALLOW_MEMORY_GROWTH'], (2 * 1024 * 1024 * 1024) // webassembly.WASM_PAGE_SIZE)
run(['-sINITIAL_MEMORY=32MB', '-sALLOW_MEMORY_GROWTH', '-sWASM=0'], (2 * 1024 * 1024 * 1024) // webassembly.WASM_PAGE_SIZE)
Expand Down