Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGELOG.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@

== DFX

=== refactor: optimize from ic-wasm

Optimize Rust canister WASM module via ic-wasm library instead of ic-cdk-optimizer.

The actual optimization was kept the same.

=== fix: Use default setting for BTC adapter idle seconds

A lower threshold was no longer necessary.
Expand Down
74 changes: 70 additions & 4 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 1 addition & 7 deletions e2e/tests-dfx/rust.bash
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@

load ../utils/_

[ -e "${assets:?}/installed/bin/ic-cdk-optimizer" ] || cargo install ic-cdk-optimizer --root "$assets/installed" &> /dev/null

setup() {
standard_setup
}
Expand All @@ -21,10 +19,8 @@ teardown() {
dfx_start
dfx canister create --all
assert_command dfx build hello
assert_match "ic-cdk-optimizer not installed"
export PATH="$assets/installed/bin/:$PATH"
assert_command dfx build hello
assert_match "Executing: ic-cdk-optimizer"
assert_match "Optimizing WASM module."
assert_command dfx canister install hello
assert_command dfx canister call hello greet dfinity
assert_match '("Hello, dfinity!")'
Expand All @@ -33,7 +29,6 @@ teardown() {
@test "rust canister can resolve dependencies" {
dfx_new_rust rust_deps
install_asset rust_deps
export PATH="$assets/installed/bin/:$PATH"
dfx_start
assert_command dfx deploy
assert_command dfx canister call multiply_deps read
Expand All @@ -48,7 +43,6 @@ teardown() {
dfx_new_rust
CARGO_TARGET_DIR="$(echo -ne '\x81')"
export CARGO_TARGET_DIR
export PATH="$assets/installed/bin/:$PATH"
dfx_start
assert_command dfx deploy
assert_command dfx canister call e2e_project greet dfinity
Expand Down
2 changes: 1 addition & 1 deletion src/dfx/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ futures = "0.3.21"
garcon = { version = "0.2", features = ["async"] }
hex = {version = "0.4.2", features = ["serde"] }
ic-types = "0.3.0"
ic-wasm = "0.1.0"
ic-wasm = { version = "0.1.3", default-features = false, features = ["optimize"]}
indicatif = "0.16.0"
itertools = "0.10.3"
lazy-init = "0.5.0"
Expand Down
43 changes: 10 additions & 33 deletions src/dfx/src/lib/builders/rust.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use anyhow::{anyhow, bail, Context};
use fn_error_context::context;
use ic_types::principal::Principal as CanisterId;
use serde::Deserialize;
use slog::{info, o, warn};
use slog::{info, o};
use std::path::PathBuf;
use std::process::Command;
use std::process::Stdio;
Expand Down Expand Up @@ -100,38 +100,15 @@ impl CanisterBuilder for RustBuilder {
);
let output = cargo.output().context("Failed to run 'cargo build'.")?;

if Command::new("ic-cdk-optimizer")
.arg("--version")
.output()
.is_ok()
{
let mut optimizer = Command::new("ic-cdk-optimizer");
let wasm_path = rust_info.get_output_wasm_path();
optimizer
.stdout(Stdio::inherit())
.stderr(Stdio::inherit())
.arg("-o")
.arg(wasm_path)
.arg(wasm_path);
// The optimized wasm overwrites the original wasm.
// Because the `get_output_wasm_path` must give the same path,
// no matter optimized or not.
info!(
self.logger,
"Executing: ic-cdk-optimizer -o {0} {0}",
wasm_path.display()
);
if !matches!(optimizer.status(), Ok(status) if status.success()) {
warn!(self.logger, "Failed to run ic-cdk-optimizer.");
}
} else {
warn!(
self.logger,
"ic-cdk-optimizer not installed, the output WASM module is not optimized in size.
Run `cargo install ic-cdk-optimizer` to install it.
"
);
}
info!(self.logger, "Optimizing WASM module.");
let wasm_path = rust_info.get_output_wasm_path();
let wasm = std::fs::read(wasm_path).expect("Could not read the WASM module.");
let wasm_optimized =
ic_wasm::optimize::optimize(&wasm).map_err(|e| anyhow!(e.to_string()))?;
// The optimized wasm overwrites the original wasm.
// Because the `get_output_wasm_path` must give the same path,
// no matter optimized or not.
std::fs::write(wasm_path, wasm_optimized).expect("Could not write optimized WASM module.");

if !output.status.success() {
bail!("Failed to compile the rust package: {}", package);
Expand Down