Skip to content

Commit 7465dee

Browse files
authored
Update and document Emscripten link-time options (#174)
Set INITIAL_HEAP=64KB and ALLOW_MEMORY_GROWTH=1 to reduce starting memory usage of plugins, but allow the heap to grow. Use default Emscripten STACK_SIZE of 64KB, see this emscripten-discuss thread for background behind that value. Also add comments documenting the purpose of each link-time option. Signed-off-by: Michael Warres <mpw@google.com>
1 parent 55cb70e commit 7465dee

File tree

2 files changed

+20
-4
lines changed

2 files changed

+20
-4
lines changed

Makefile

+10-4
Original file line numberDiff line numberDiff line change
@@ -29,17 +29,23 @@ PKG_CONFIG_PATH = ${EMSDK}/upstream/emscripten/cache/sysroot/lib/pkgconfig
2929
WASM_LIBS = $(shell $(PKG_CONFIG) $(WASM_DEPS) $(PROTO_DEPS) \
3030
--with-path=$(PKG_CONFIG_PATH) --libs | sed -e 's/-pthread //g')
3131

32+
# See proxy_wasm_cc_binary build rule definition in bazel/defs.bzl for
33+
# explanation of emscripten link options.
34+
EMSCRIPTEN_LINK_OPTS := --no-entry \
35+
--js-library ${PROXY_WASM_CPP_SDK}/proxy_wasm_intrinsics.js \
36+
-sSTANDALONE_WASM -sEXPORTED_FUNCTIONS=_malloc \
37+
-sALLOW_MEMORY_GROWTH=1 -sINITIAL_HEAP=64KB
38+
39+
3240
debug-deps:
3341
# WASM_DEPS : ${WASM_DEPS}
3442
# WASM_LIBS : ${WASM_LIBS}
3543
# PROTO_DEPS: ${PROTO_DEPS}
3644
# PROTO_OPTS: ${PROTO_OPTS}
3745

38-
# TODO(mpwarres): Add Emscripten stack/heap size params in PR#174.
3946
%.wasm %.wat: %.cc
40-
em++ --no-entry -sSTANDALONE_WASM -sEXPORTED_FUNCTIONS=_malloc \
41-
--std=c++17 -O3 -flto \
42-
--js-library ${PROXY_WASM_CPP_SDK}/proxy_wasm_intrinsics.js \
47+
em++ --std=c++17 -O3 -flto \
48+
${EMSCRIPTEN_LINK_OPTS} \
4349
-I${PROXY_WASM_CPP_SDK} \
4450
${CPP_CONTEXT_LIB} \
4551
${PROTO_OPTS} \

bazel/defs.bzl

+10
Original file line numberDiff line numberDiff line change
@@ -90,10 +90,20 @@ def proxy_wasm_cc_binary(
9090
"@proxy_wasm_cpp_sdk//:proxy_wasm_intrinsics_js",
9191
],
9292
linkopts = linkopts + [
93+
# Setting to indicate module is a "reactor library" without a main() entry point:
94+
# https://emscripten.org/docs/tools_reference/settings_reference.html#standalone-wasm
9395
"--no-entry",
96+
# File listing additional functions that Emscripten should expect to be implemented by the host:
97+
# https://emscripten.org/docs/porting/connecting_cpp_and_javascript/Interacting-with-code.html#implement-c-in-javascript
9498
"--js-library=$(location @proxy_wasm_cpp_sdk//:proxy_wasm_intrinsics_js)",
99+
# Emit Wasm module that can run without JavaScript
95100
"-sSTANDALONE_WASM",
101+
# Give host code access to Emscripten's _malloc() function
96102
"-sEXPORTED_FUNCTIONS=_malloc",
103+
# Allow allocating memory past initial heap size
104+
"-sALLOW_MEMORY_GROWTH=1",
105+
# Initial amount of heap memory. 64KB matches Rust SDK starting heap size.
106+
"-sINITIAL_HEAP=64KB",
97107
],
98108
tags = tags + [
99109
"manual",

0 commit comments

Comments
 (0)