Skip to content

Commit 986d9c0

Browse files
committed
Increase node stack size of 8Mb when running js_optimizer
On windows the default stack size is still limited to 1MB in older versions of node. See nodejs/node#43632 which made it into v19.0.0. Fixes: emscripten-core#17897
1 parent 38ab7c1 commit 986d9c0

File tree

3 files changed

+19
-10
lines changed

3 files changed

+19
-10
lines changed

site/source/docs/getting_started/FAQ.rst

+4-4
Original file line numberDiff line numberDiff line change
@@ -654,10 +654,10 @@ Why do I get a stack size error when optimizing: ``RangeError: Maximum call stac
654654

655655
You may need to increase the stack size for :term:`node.js`.
656656

657-
On Linux and Mac macOS, you can just do ``NODE_JS = ['node',
658-
'--stack_size=8192']`` in the :ref:`compiler-configuration-file`. On Windows,
659-
you will also need ``--max-stack-size=8192``, and also run ``editbin
660-
/stack:33554432 node.exe``.
657+
On Linux and Mac macOS, you can just do ``NODE_JS = ['/path/to/node',
658+
'--stack_size=8192']`` in the :ref:`compiler-configuration-file`. On Windows
659+
(for node versions older than v19), you will also need
660+
``--max-stack-size=8192``, and also run ``editbin /stack:33554432 node.exe``.
661661

662662

663663
How do I pass int64_t and uint64_t values from js into Wasm functions?

tools/building.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -342,7 +342,7 @@ def opt_level_to_str(opt_level, shrink_level=0):
342342
def js_optimizer(filename, passes):
343343
from . import js_optimizer
344344
try:
345-
return js_optimizer.run_on_js(filename, passes)
345+
return js_optimizer.run_on_file(filename, passes)
346346
except subprocess.CalledProcessError as e:
347347
exit_with_error("'%s' failed (%d)", ' '.join(e.cmd), e.returncode)
348348

tools/js_optimizer.py

+14-5
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,15 @@
3737
import_sig = re.compile(r'(var|const) ([_\w$]+ *=[^;]+);')
3838

3939

40+
def get_acorn_cmd():
41+
node = config.NODE_JS
42+
if not any('--stack-size' in arg for arg in node):
43+
# Use an 8Mb stack (rather than the ~1Mb default) when running the
44+
# js optimizer since larger inputs can cause terser to use a lot of stack.
45+
node.append('--stack-size=8192')
46+
return node + [ACORN_OPTIMIZER]
47+
48+
4049
def split_funcs(js):
4150
# split properly even if there are no newlines,
4251
# which is important for deterministic builds (as which functions
@@ -88,7 +97,7 @@ def minify_shell(self, shell, minify_whitespace):
8897
f.write('\n')
8998
f.write('// EXTRA_INFO:' + json.dumps(self.serialize()))
9099

91-
cmd = config.NODE_JS + [ACORN_OPTIMIZER, temp_file, 'minifyGlobals']
100+
cmd = get_acorn_cmd() + [temp_file, 'minifyGlobals']
92101
if minify_whitespace:
93102
cmd.append('minifyWhitespace')
94103
output = shared.run_process(cmd, stdout=subprocess.PIPE).stdout
@@ -139,8 +148,8 @@ def chunkify(funcs, chunk_size):
139148
return [''.join(func[1] for func in chunk) for chunk in chunks] # remove function names
140149

141150

142-
@ToolchainProfiler.profile_block('js_optimizer.run_on_js')
143-
def run_on_js(filename, passes, extra_info=None):
151+
@ToolchainProfiler.profile_block('js_optimizer.run_on_file')
152+
def run_on_file(filename, passes, extra_info=None):
144153
with ToolchainProfiler.profile_block('js_optimizer.split_markers'):
145154
if not isinstance(passes, list):
146155
passes = [passes]
@@ -261,7 +270,7 @@ def write_chunk(chunk, i):
261270
filenames = [write_chunk(chunk, i) for i, chunk in enumerate(chunks)]
262271

263272
with ToolchainProfiler.profile_block('run_optimizer'):
264-
commands = [config.NODE_JS + [ACORN_OPTIMIZER, f] + passes for f in filenames]
273+
commands = [get_acorn_cmd() + [f] + passes for f in filenames]
265274
filenames = shared.run_multiple_processes(commands, route_stdout_to_temp_files_suffix='js_opt.jo.js')
266275

267276
with ToolchainProfiler.profile_block('split_closure_cleanup'):
@@ -348,7 +357,7 @@ def main():
348357
sys.argv = sys.argv[:-1]
349358
else:
350359
extra_info = None
351-
out = run_on_js(sys.argv[1], sys.argv[2:], extra_info=extra_info)
360+
out = run_on_file(sys.argv[1], sys.argv[2:], extra_info=extra_info)
352361
shutil.copyfile(out, sys.argv[1] + '.jsopt.js')
353362
return 0
354363

0 commit comments

Comments
 (0)