Skip to content

Conversation

@kentosugama
Copy link
Contributor

@kentosugama kentosugama commented Apr 14, 2023

Description

LANG-124

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

See also results in the canister profiling repo.

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"
  }
}

How Has This Been Tested?

The following are build.bash tests, ensuring that builds succeed with optimizations turned on, but that the feature is turned off by default, especially for custom canisters.

@test "build succeeds if enable optimize" {
    jq '.canisters.e2e_project_backend.optimize="cycles"' dfx.json | sponge dfx.json
    dfx_start
    dfx canister create --all
    assert_command dfx build
}

@test "build custom canister default no optimize" {
    install_asset custom_canister
    install_asset wasm/identity

    dfx_start
    dfx canister create --all
    assert_command dfx build custom
    assert_not_match "Optimize"

    jq '.canisters.custom.optimize="size"' dfx.json | sponge dfx.json
    assert_command dfx build custom
    assert_match "Optimize"
}

The following is a test that ensures that dfx start correctly rejects invalid optimization options in dfx.json.

@test "invalid optimization level" {
    jq '.canisters.bad_optimization_level.optimize="bad_level"' dfx.json | sponge dfx.json
    assert_command_fail dfx_start
    assert_match "expected one of "
}

Checklist:

  • The title of this PR complies with Conventional Commits.
  • I have edited the CHANGELOG accordingly.
  • I have made corresponding changes to the documentation.
  • Add e2e tests
  • Add e2e error tests
  • Update ic-wasm dependency
  • Add default "cycles" and "size" options
  • Error handle invalid options
  • Double check expected dynamic libraries check
  • Add unit tests in addition to e2e?
  • Release vs debug mode?
  • Make ic-wasm release and use the released crate

@kentosugama kentosugama changed the title Expose wasm-opt optimizer in ic-wasm to users feat: expose wasm-opt optimizer in ic-wasm to users Apr 14, 2023
@kentosugama kentosugama marked this pull request as ready for review April 19, 2023 18:17
@kentosugama kentosugama requested a review from a team as a code owner April 19, 2023 18:17
@kentosugama kentosugama requested a review from a user April 19, 2023 18:17
@lwshang lwshang mentioned this pull request Apr 19, 2023
3 tasks
@kentosugama kentosugama self-assigned this Apr 20, 2023
Copy link

@ghost ghost left a comment

Choose a reason for hiding this comment

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

Looks good. Approved assuming the update to ic-wasm 0.3.6 is applied. The rust e2e test failures will be fixed once #3099 is merged.

@ghost ghost added the automerge-squash label Apr 21, 2023
@mergify mergify bot merged commit d3fc977 into master Apr 21, 2023
@mergify mergify bot deleted the wasm-opt branch April 21, 2023 18:14
@kentosugama
Copy link
Contributor Author

@ericswanson-dfinity Thank you for the reviews!

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.

1 participant