From 38b2a73491c096df8e06fd29def1640692ca3b39 Mon Sep 17 00:00:00 2001 From: Kento Sugama <107421898+kentosugama@users.noreply.github.com> Date: Fri, 30 Jun 2023 10:38:03 -0700 Subject: [PATCH] Enable wasm optimizer from `dfx 0.14.0` (#55) --- .github/workflows/perf.yml | 1 + README.md | 24 ++++++++++++++++++++++++ collections/Makefile | 4 ++++ dapps/Makefile | 4 ++++ heartbeat/Makefile | 4 ++++ motoko/Makefile | 2 ++ motoko/README.md | 2 +- pub-sub/Makefile | 4 ++++ 8 files changed, 44 insertions(+), 1 deletion(-) diff --git a/.github/workflows/perf.yml b/.github/workflows/perf.yml index 002a3005..65978ac8 100644 --- a/.github/workflows/perf.yml +++ b/.github/workflows/perf.yml @@ -59,6 +59,7 @@ jobs: cd $(dfx cache show) wget https://github.com/dfinity/motoko/releases/download/$MOC_VERSION/motoko-linux64-$MOC_VERSION.tar.gz tar zxvf motoko-linux64-$MOC_VERSION.tar.gz + cargo install --git https://github.com/dfinity/ic-wasm.git - name: Start dfx run: | dfx start --background diff --git a/README.md b/README.md index c5738661..650b953f 100644 --- a/README.md +++ b/README.md @@ -48,3 +48,27 @@ Benchmark_name/ src/ lib.rs ``` + +## Wasm Optimizer + +A Wasm optimizer is applied to each Wasm binary before instrumentation (except the GC benchmarks). The optimizer can be found in [ic-wasm](https://github.com/dfinity/ic-wasm), which wraps [wasm-opt](https://github.com/WebAssembly/binaryen). + +The following optimizations are applied: +``` +ic-wasm -o shrink --optimize O3 --keep-name-section +``` + +Note that the name section is preserved in the optimization process. This is because the name section is used by the profiler to produce the flame graphs. + +For users who wish to use the optimizer, the easiest way is to enable it via a field in `dfx.json`: + +``` +{ + "canisters": { + "my_canister": { + "optimize": "cycles" + } + } +} +``` +This, as in most real world uses, removes the name section to minimize the binary size. diff --git a/collections/Makefile b/collections/Makefile index 1518fd1a..d3a45ef1 100644 --- a/collections/Makefile +++ b/collections/Makefile @@ -9,6 +9,8 @@ motoko: mops install; \ dfx canister create --all; \ dfx build; \ + echo "Optimize with ic-wasm level 3"; \ + for f in .dfx/local/canisters/*/*.wasm; do ic-wasm -o $$f $$f shrink --optimize O3 --keep-name-section; done; \ rm mops.toml; \ cd .. @@ -16,6 +18,8 @@ rust: cd rust; \ dfx canister create --all; \ dfx build; \ + echo "Optimize with ic-wasm level 3"; \ + for f in .dfx/local/canisters/*/*.wasm; do ic-wasm -o $$f $$f shrink --optimize O3 --keep-name-section; done; \ cd .. build: motoko rust diff --git a/dapps/Makefile b/dapps/Makefile index 2e21b173..d60b6bd1 100644 --- a/dapps/Makefile +++ b/dapps/Makefile @@ -7,12 +7,16 @@ motoko: cd motoko; \ dfx canister create --all; \ dfx build; \ + echo "Optimize with ic-wasm level 3"; \ + for f in .dfx/local/canisters/*/*.wasm; do ic-wasm -o $$f $$f shrink --optimize O3 --keep-name-section; done; \ cd .. rust: cd rust; \ dfx canister create --all; \ dfx build; \ + echo "Optimize with ic-wasm level 3"; \ + for f in .dfx/local/canisters/*/*.wasm; do ic-wasm -o $$f $$f shrink --optimize O3 --keep-name-section; done; \ cd .. build: motoko rust diff --git a/heartbeat/Makefile b/heartbeat/Makefile index de9bb412..fbdb7ff8 100644 --- a/heartbeat/Makefile +++ b/heartbeat/Makefile @@ -9,6 +9,8 @@ motoko: mops install; \ dfx canister create --all; \ dfx build; \ + echo "Optimize with ic-wasm level 3"; \ + for f in .dfx/local/canisters/*/*.wasm; do ic-wasm -o $$f $$f shrink --optimize O3 --keep-name-section; done; \ rm mops.toml; \ cd .. @@ -16,6 +18,8 @@ rust: cd rust; \ dfx canister create --all; \ dfx build; \ + echo "Optimize with ic-wasm level 3"; \ + for f in .dfx/local/canisters/*/*.wasm; do ic-wasm -o $$f $$f shrink --optimize O3 --keep-name-section; done; \ cd .. build: motoko rust diff --git a/motoko/Makefile b/motoko/Makefile index 87be2f7f..e3a98ef9 100644 --- a/motoko/Makefile +++ b/motoko/Makefile @@ -8,6 +8,8 @@ build: mops install; \ dfx canister create --all; \ dfx build; \ + echo "Optimize with ic-wasm level 3"; \ + for f in .dfx/local/canisters/*/*.wasm; do ic-wasm -o $$f $$f shrink --optimize O3 --keep-name-section; done; \ rm mops.toml; \ cd .. diff --git a/motoko/README.md b/motoko/README.md index 4112bec3..776e97d5 100644 --- a/motoko/README.md +++ b/motoko/README.md @@ -2,7 +2,7 @@ Measure various features only available in Motoko. -* Garbage Collection. Measure Motoko garbage collection cost using the [Triemap benchmark](https://github.com/dfinity/canister-profiling/blob/main/collections/motoko/src/triemap.mo). The max mem column reports `rts_max_live_size` after `generate` call. The cycle cost numbers reported here are garbage collection cost only. Some flamegraphs are truncated due to the 2M log size limit. +* Garbage Collection. Measure Motoko garbage collection cost using the [Triemap benchmark](https://github.com/dfinity/canister-profiling/blob/main/collections/motoko/src/triemap.mo). The max mem column reports `rts_max_live_size` after `generate` call. The cycle cost numbers reported here are garbage collection cost only. Some flamegraphs are truncated due to the 2M log size limit. The `dfx`/`ic-wasm` optimizer is disabled for the garbage collection test cases due to how the optimizer affects function names, making profiling trickier. - default. Compile with the default GC option. With the current GC scheduler, `generate` will trigger the copying GC. The rest of the methods will not trigger GC. - copying. Compile with `--force-gc --copying-gc`. diff --git a/pub-sub/Makefile b/pub-sub/Makefile index a5e33bf8..5133bd18 100644 --- a/pub-sub/Makefile +++ b/pub-sub/Makefile @@ -7,12 +7,16 @@ motoko: cd motoko; \ dfx canister create --all; \ dfx build; \ + echo "Optimize with ic-wasm level 3"; \ + for f in .dfx/local/canisters/*/*.wasm; do ic-wasm -o $$f $$f shrink --optimize O3 --keep-name-section; done; \ cd .. rust: cd rust; \ dfx canister create --all; \ dfx build; \ + echo "Optimize with ic-wasm level 3"; \ + for f in .dfx/local/canisters/*/*.wasm; do ic-wasm -o $$f $$f shrink --optimize O3 --keep-name-section; done; \ cd .. build: motoko rust