Skip to content

Commit

Permalink
Enable wasm optimizer from dfx 0.14.0 (#55)
Browse files Browse the repository at this point in the history
  • Loading branch information
kentosugama authored Jun 30, 2023
1 parent e391e7f commit 38b2a73
Show file tree
Hide file tree
Showing 8 changed files with 44 additions and 1 deletion.
1 change: 1 addition & 0 deletions .github/workflows/perf.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
24 changes: 24 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 <wasm> <wasm> 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.
4 changes: 4 additions & 0 deletions collections/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,17 @@ 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 ..

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
Expand Down
4 changes: 4 additions & 0 deletions dapps/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions heartbeat/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,17 @@ 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 ..

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
Expand Down
2 changes: 2 additions & 0 deletions motoko/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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 ..

Expand Down
2 changes: 1 addition & 1 deletion motoko/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`.
Expand Down
4 changes: 4 additions & 0 deletions pub-sub/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 38b2a73

Please sign in to comment.