From 5b97823ba8f3fe01f745ae60825a931d644dc265 Mon Sep 17 00:00:00 2001 From: Brian Carroll Date: Tue, 15 Mar 2022 18:58:41 +0000 Subject: [PATCH 1/5] repl_wasm: prevent console_error_panic_hook slowing down non-wasm builds --- repl_wasm/Cargo.toml | 1 - repl_wasm/src/lib.rs | 2 +- repl_wasm/src/repl.rs | 2 +- repl_www/build.sh | 4 ++-- 4 files changed, 4 insertions(+), 5 deletions(-) diff --git a/repl_wasm/Cargo.toml b/repl_wasm/Cargo.toml index 944d550955..61dad966be 100644 --- a/repl_wasm/Cargo.toml +++ b/repl_wasm/Cargo.toml @@ -26,7 +26,6 @@ roc_target = {path = "../compiler/roc_target"} roc_types = {path = "../compiler/types"} [features] -default = ["console_error_panic_hook"] wasmer = ["futures"] [package.metadata.wasm-pack.profile.profiling] diff --git a/repl_wasm/src/lib.rs b/repl_wasm/src/lib.rs index 0d1c6d9461..a1d66e6f8d 100644 --- a/repl_wasm/src/lib.rs +++ b/repl_wasm/src/lib.rs @@ -3,7 +3,7 @@ mod repl; // // Interface with external JS in the browser // -#[cfg(not(feature = "wasmer"))] +#[cfg(feature = "console_error_panic_hook")] extern crate console_error_panic_hook; #[cfg(not(feature = "wasmer"))] mod externs_js; diff --git a/repl_wasm/src/repl.rs b/repl_wasm/src/repl.rs index 9bbd2891af..95cb77be11 100644 --- a/repl_wasm/src/repl.rs +++ b/repl_wasm/src/repl.rs @@ -155,7 +155,7 @@ impl<'a> ReplApp<'a> for WasmReplApp<'a> { } pub async fn entrypoint_from_js(src: String) -> Result { - #[cfg(not(feature = "wasmer"))] + #[cfg(feature = "console_error_panic_hook")] console_error_panic_hook::set_once(); let arena = &Bump::new(); diff --git a/repl_www/build.sh b/repl_www/build.sh index 52a2121631..d9e157271c 100755 --- a/repl_www/build.sh +++ b/repl_www/build.sh @@ -26,11 +26,11 @@ cp -r repl_www/public/* $WWW_ROOT if [ -n "${REPL_DEBUG:-}" ] then # Leave out wasm-opt since it takes too long when debugging, and provide some debug options - cargo build --target wasm32-unknown-unknown -p roc_repl_wasm --release + cargo build --target wasm32-unknown-unknown -p roc_repl_wasm --release --features console_error_panic_hook wasm-bindgen --target web --keep-debug target/wasm32-unknown-unknown/release/roc_repl_wasm.wasm --out-dir repl_wasm/pkg/ else # A `--profiling` build is optimized and has debug info, so we get stack traces for compiler `todo!()` - wasm-pack build --profiling --target web repl_wasm + wasm-pack build --profiling --target web repl_wasm -- --features console_error_panic_hook fi cp repl_wasm/pkg/*.wasm $WWW_ROOT From 72b620086f831494f14559b3cefae7fb9d0a3614 Mon Sep 17 00:00:00 2001 From: Brian Carroll Date: Tue, 15 Mar 2022 19:01:31 +0000 Subject: [PATCH 2/5] Set Cargo config to optimize Wasm builds for size (web REPL) --- .cargo/config | 3 +++ 1 file changed, 3 insertions(+) diff --git a/.cargo/config b/.cargo/config index 7f9d89b452..f37e09f542 100644 --- a/.cargo/config +++ b/.cargo/config @@ -2,3 +2,6 @@ test-gen-llvm = "test -p test_gen" test-gen-dev = "test -p roc_gen_dev -p test_gen --no-default-features --features gen-dev" test-gen-wasm = "test -p roc_gen_wasm -p test_gen --no-default-features --features gen-wasm" + +[target.wasm32-unknown-unknown] +rustflags = ["-Copt-level=s"] From 52e17ae6b8754612c61e3c40b0521124bf7819e2 Mon Sep 17 00:00:00 2001 From: Brian Carroll Date: Tue, 15 Mar 2022 19:19:29 +0000 Subject: [PATCH 3/5] Further optimize wasm build for web REPL --- .cargo/config | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.cargo/config b/.cargo/config index f37e09f542..184815587d 100644 --- a/.cargo/config +++ b/.cargo/config @@ -4,4 +4,4 @@ test-gen-dev = "test -p roc_gen_dev -p test_gen --no-default-features --features test-gen-wasm = "test -p roc_gen_wasm -p test_gen --no-default-features --features gen-wasm" [target.wasm32-unknown-unknown] -rustflags = ["-Copt-level=s"] +rustflags = ["-Copt-level=s", "-Cpanic=abort", "-Clto=fat"] From 7542610953f49b27b029f3c3a88047f02bebeaec Mon Sep 17 00:00:00 2001 From: Brian Carroll Date: Wed, 16 Mar 2022 12:51:24 +0000 Subject: [PATCH 4/5] Add comments about Wasm compiler flags --- .cargo/config | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/.cargo/config b/.cargo/config index 184815587d..231ffccea0 100644 --- a/.cargo/config +++ b/.cargo/config @@ -4,4 +4,8 @@ test-gen-dev = "test -p roc_gen_dev -p test_gen --no-default-features --features test-gen-wasm = "test -p roc_gen_wasm -p test_gen --no-default-features --features gen-wasm" [target.wasm32-unknown-unknown] +# Rust compiler flags for minimum-sized .wasm binary in the web REPL +# opt-level=s Optimizations should focus more on size than speed +# panic=abort Don't emit code to unwind the stack on panic, just exit +# lto=fat Spend extra effort on link-time optimization across crates rustflags = ["-Copt-level=s", "-Cpanic=abort", "-Clto=fat"] From 8ca0b1cfcf46edc8f2cbb4bce0f3e151c8b1caf5 Mon Sep 17 00:00:00 2001 From: Brian Carroll Date: Wed, 16 Mar 2022 17:11:15 +0000 Subject: [PATCH 5/5] Remove panic=abort from wasm flags in case of memory leaks --- .cargo/config | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/.cargo/config b/.cargo/config index 231ffccea0..584d158f1c 100644 --- a/.cargo/config +++ b/.cargo/config @@ -6,6 +6,5 @@ test-gen-wasm = "test -p roc_gen_wasm -p test_gen --no-default-features --featur [target.wasm32-unknown-unknown] # Rust compiler flags for minimum-sized .wasm binary in the web REPL # opt-level=s Optimizations should focus more on size than speed -# panic=abort Don't emit code to unwind the stack on panic, just exit # lto=fat Spend extra effort on link-time optimization across crates -rustflags = ["-Copt-level=s", "-Cpanic=abort", "-Clto=fat"] +rustflags = ["-Copt-level=s", "-Clto=fat"]