Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wasm-opt integration #28

Merged
merged 28 commits into from
Apr 13, 2023
Merged

Wasm-opt integration #28

merged 28 commits into from
Apr 13, 2023

Conversation

kentosugama
Copy link
Contributor

@kentosugama kentosugama commented Apr 6, 2023

Profiling data suggests that it would be useful to take advantage of the performance and binary size optimizations offered by wasm-opt.

See also results in the canister profiling repo.

To do so, it is necessary to correctly preserve the ic custom sections. Furthermore, it would be best to recursively optimize embedded modules in the data sections used by Motoko actor classes.

ic-wasm is a good tool for this job since it already parses wasm modules, including the extraction of embedded actors in the data section.

TODO items left:

  • Allow user to specify optimization level
  • Use temp file
  • Optimize actor classes in the data sections
  • Handle errors nicely (check uses of unwrap)
  • Add basic tests
  • Test different optimization levels
  • Add more complex test cases
  • Test preservation of custom sections
  • Document

Also check:

  • Warn of any dwarf custom sections
  • If possible, pass wasm module to and from wasm-opt in memory only. see here

@kentosugama kentosugama self-assigned this Apr 7, 2023
tests/tests.rs Outdated Show resolved Hide resolved
@kentosugama kentosugama marked this pull request as ready for review April 10, 2023 18:50
@kentosugama kentosugama requested a review from a team as a code owner April 10, 2023 18:50
src/bin/main.rs Outdated Show resolved Hide resolved
src/optimize.rs Outdated Show resolved Hide resolved
src/optimize.rs Outdated Show resolved Hide resolved
src/optimize.rs Outdated Show resolved Hide resolved
tests/deployable.ic-repl.sh Outdated Show resolved Hide resolved
tests/deployable.ic-repl.sh Show resolved Hide resolved
tests/tests.rs Outdated Show resolved Hide resolved
tests/tests.rs Outdated Show resolved Hide resolved
Copy link
Contributor

@chenyan-dfinity chenyan-dfinity left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remember to bump ic-wasm version number.

src/shrink.rs Outdated Show resolved Hide resolved
src/shrink.rs Outdated Show resolved Hide resolved
Copy link
Contributor

@chenyan-dfinity chenyan-dfinity left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@kentosugama
Copy link
Contributor Author

@chenyan-dfinity thanks for all feedback!

@kentosugama kentosugama merged commit 3c5acec into main Apr 13, 2023
@kentosugama kentosugama deleted the wasm-opt branch April 13, 2023 20:08
mergify bot pushed a commit to dfinity/sdk that referenced this pull request Apr 21, 2023
[LANG-124](https://dfinity.atlassian.net/browse/LANG-124)

[Profiling data](https://docs.google.com/document/d/1ICXF083-hfRZr2OfRUk8OxmZqYDQL4XINHQ0FXuDqTA/edit) suggests that it would be useful to take advantage of the performance and binary size wasm optimizations offered by [`wasm-opt`](https://github.com/WebAssembly/binaryen).

See also results in the [canister profiling repo](dfinity/canister-profiling#35).

This tool has been integrated with `ic-wasm` in this PR: dfinity/ic-wasm#28. The tool is added under the `shrink` command that is currently used in `dfx` to perform binary size reduction of canisters.

The next step is to expose this feature to users by allowing users to opt into the optimizer and specify the optimization level in `dfx.json`. Then to invoke `ic-wasm` in `dfx` according to this argument, similarly to `shrink`.

Note: This tool has been used in the past to perform binary size reductions for canisters, but was replaced because it deletes the custom metadata sections from wasm modules. The `ic-wasm` feature invokes `wasm-opt` while preserving these sections.

Example usage in `dfx.json`:
```
"canisters" : {
"backend" : {
"optimize" : "cycles"
}
}
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants