Skip to content

Commit

Permalink
Replace ic-cdk-optimizer with ic-wasm-shrink (#2868)
Browse files Browse the repository at this point in the history
# Motivation
`ic-cdk-optimizer` has been deprecated for a long time. However, as the
replacement did not support our use cases we could not migrate to the
proposed replacement tools. Now two things encourage the migration:

* There is now a drop-in replacement in the form of `ic-wasm shrink`
* We cannot make some changes to the Rust code, because
`ic-cdk-optimizer` does not support the corresponding opcodes.

# Changes
* Replace `ic-cdk-optimizer` with `ic-wasm shrink`

# Tests
* CI should check correctness.

# Todos

- [x] Add entry to changelog (if necessary).
- [x] The wasm size needs to be compared before and after this change.

---------

Co-authored-by: Max Murphy-Skvorzov <[email protected]>
  • Loading branch information
bitdivine and bitdivine authored Jul 17, 2023
1 parent 8be867a commit 4b99d56
Show file tree
Hide file tree
Showing 6 changed files with 13 additions and 19 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG-Nns-Dapp.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ The NNS Dapp is released through proposals in the Network Nervous System. Theref

#### Changed

* Replace `ic-cdk-optimizer` with `ic-wasm shrink`.

#### Deprecated
#### Removed

Expand Down
3 changes: 0 additions & 3 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,6 @@ RUN curl --fail https://sh.rustup.rs -sSf \
| sh -s -- -y --no-modify-path
ENV PATH=/cargo/bin:$PATH
RUN cargo --version
# Install IC CDK optimizer
# TODO: Make ic-cdk-optimizer support binstall, then use cargo binstall --no-confirm ic-cdk-optimizer here.
RUN curl -L --fail --retry 5 "https://github.com/dfinity/cdk-rs/releases/download/$(cat config/optimizer_version)/ic-cdk-optimizer-$(cat config/optimizer_version)-ubuntu-20.04.tar.gz" | gunzip | tar -x "ic-cdk-optimizer-$(cat config/optimizer_version)-ubuntu-20.04/ic-cdk-optimizer" --to-stdout | install -m755 /dev/stdin /usr/local/bin/ic-cdk-optimizer
# Pre-build all cargo dependencies. Because cargo doesn't have a build option
# to build only the dependencies, we pretend that our project is a simple, empty
# `lib.rs`. Then we remove the dummy source files to make sure cargo rebuild
Expand Down
4 changes: 2 additions & 2 deletions build-rs.sh
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ echo "Compiling rust package ${canister_name}"
cargo build "${cargo_args[@]}"

####################
# ic-cdk-optimizer # (output: ${canister_name}.wasm.gz)
# ic-wasm shrink # (output: ${canister_name}.wasm.gz)
####################
echo Optimising wasm
wasm_path="$(canister_name="$canister_name" jq -r '.canisters[env.canister_name].wasm' dfx.json)"
Expand All @@ -40,7 +40,7 @@ wasm_path="$(canister_name="$canister_name" jq -r '.canisters[env.canister_name]
exit 1
} >&2
mkdir -p "$(dirname "$wasm_path")"
ic-cdk-optimizer "./target/wasm32-unknown-unknown/release/${canister_name}.wasm" -o "${wasm_path%.gz}"
ic-wasm "./target/wasm32-unknown-unknown/release/${canister_name}.wasm" -o "${wasm_path%.gz}" shrink
gzip -f -n "${wasm_path%.gz}"
rm -f "${wasm_path%.gz}"
ls -sh "${wasm_path}"
Expand Down
6 changes: 3 additions & 3 deletions build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@
#
# The frontend is built into public/. The dapp is bundled into a tarball,
# assets.tar.xz. This tarball is baked into the wasm binary output at build
# time by cargo, and finally the wasm binary is read by ic-cdk-optimizer and
# optimizer. This scripts outputs a single file, nns-dapp.wasm.
# time by cargo, and finally the wasm binary is read by ic-wasm and
# optimized. This scripts outputs a single file, nns-dapp.wasm.
#
# frontend◄─────────── assets.tar.xz
#
#
#
# cargo build
#
# │ ic-cdk-optimizer
# │ ic-wasm shrink
#
# nns-dapp.wasm
#
Expand Down
11 changes: 6 additions & 5 deletions e2e-tests/scripts/nns-canister.Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ RUN apt -yq update && \
rm -rf /tmp/* /var/lib/apt/lists/* /var/tmp/*

RUN curl -L --proto '=https' --tlsv1.2 -sSf https://raw.githubusercontent.com/cargo-bins/cargo-binstall/181b5293e73cfe16f7a79c5b3a4339bd522d31f3/install-from-binstall-release.sh | bash && cargo binstall -V
RUN cargo binstall --no-confirm --version 0.3.2 ic-cdk-optimizer
# Note: The version should match dfx.json, however it is expected that the old e2e tests, including this file, will be deleted soon so there is no point in adding the code to get the version.
RUN cargo binstall --no-confirm "[email protected]"

ARG IC_COMMIT

Expand Down Expand Up @@ -35,20 +36,20 @@ WORKDIR /ic/rs
RUN binary="governance-canister" && \
features="test" && \
cargo build --target wasm32-unknown-unknown --profile canister-release --bin "$binary" --features "$features" && \
ic-cdk-optimizer -o "$CARGO_TARGET_DIR/${binary}_${features}.wasm" "$CARGO_TARGET_DIR/wasm32-unknown-unknown/canister-release/${binary}.wasm"
ic-wasm -o "$CARGO_TARGET_DIR/${binary}_${features}.wasm" "$CARGO_TARGET_DIR/wasm32-unknown-unknown/canister-release/${binary}.wasm" shrink

## Note: This is available as a download however we patch the source code to make testing easier.
RUN binary=cycles-minting-canister && \
cargo build --target wasm32-unknown-unknown --profile canister-release --bin "$binary" && \
ic-cdk-optimizer -o "$CARGO_TARGET_DIR/${binary}.wasm" "$CARGO_TARGET_DIR/wasm32-unknown-unknown/canister-release/${binary}.wasm"
ic-wasm -o "$CARGO_TARGET_DIR/${binary}.wasm" "$CARGO_TARGET_DIR/wasm32-unknown-unknown/canister-release/${binary}.wasm" shrink

RUN binary=sns-swap-canister && \
cargo build --target wasm32-unknown-unknown --profile canister-release --bin "$binary" && \
ic-cdk-optimizer -o "$CARGO_TARGET_DIR/${binary}.wasm" "$CARGO_TARGET_DIR/wasm32-unknown-unknown/canister-release/${binary}.wasm"
ic-wasm -o "$CARGO_TARGET_DIR/${binary}.wasm" "$CARGO_TARGET_DIR/wasm32-unknown-unknown/canister-release/${binary}.wasm" shrink

RUN binary=sns-wasm-canister && \
cargo build --target wasm32-unknown-unknown --profile canister-release --bin "$binary" && \
ic-cdk-optimizer -o "$CARGO_TARGET_DIR/${binary}.wasm" "$CARGO_TARGET_DIR/wasm32-unknown-unknown/canister-release/${binary}.wasm"
ic-wasm -o "$CARGO_TARGET_DIR/${binary}.wasm" "$CARGO_TARGET_DIR/wasm32-unknown-unknown/canister-release/${binary}.wasm" shrink

FROM scratch AS scratch
COPY --from=builder /ic/rs/nns/governance/canister/governance.did /governance.did
Expand Down
6 changes: 0 additions & 6 deletions scripts/setup
Original file line number Diff line number Diff line change
Expand Up @@ -185,11 +185,6 @@ install_tar_linux() {
sudo apt-get install tar
}

install_ic_cdk_optimizer() {
# Nit: Needs rust to be in a working state.
cargo binstall --no-confirm --version 0.3.2 ic-cdk-optimizer
}

is_docker_installed() {
command -v docker >/dev/null 2>&1 || return 1
local major_version
Expand Down Expand Up @@ -306,7 +301,6 @@ command -v xz || "install_xz_$OS"
have_correct_dfx || install_dfx
command -v gtar || tar --help | grep GNU >/dev/null || "install_tar_$OS"
command -v cmake || "install_cmake_$OS"
command -V ic-cdk-optimizer || install_ic_cdk_optimizer
command -V ic-admin || "install_ic_admin_$OS"
command -v sns || "install_sns_$OS"
command -v idl2json || install_idl2json
Expand Down

0 comments on commit 4b99d56

Please sign in to comment.