Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
3e04c64
fix broken changes from ic-wasm update
kentosugama Apr 11, 2023
919a69e
Merge branch 'master' into wasm-opt
kentosugama Apr 14, 2023
62aa6ca
update ic-wasm dependency
kentosugama Apr 14, 2023
4ad8f13
Expose optimize feature
kentosugama Apr 14, 2023
329e853
add cycles and size optimization defaults as options
kentosugama Apr 14, 2023
41555b6
reword description
kentosugama Apr 17, 2023
20b3436
update expected dynamic libraries
kentosugama Apr 17, 2023
2a9de10
Merge branch 'master' into wasm-opt
kentosugama Apr 18, 2023
75a8895
add tests
kentosugama Apr 18, 2023
0830aab
update changelog
kentosugama Apr 18, 2023
3c4d9a2
fix tests
kentosugama Apr 18, 2023
1f3a749
Update description
kentosugama Apr 18, 2023
8456c73
handle invalid opt level
kentosugama Apr 18, 2023
ed5fb73
error test
kentosugama Apr 18, 2023
7d2c353
Define enums for optimization levels
kentosugama Apr 18, 2023
56579cf
fix error test
kentosugama Apr 18, 2023
b782d96
fix error test
kentosugama Apr 18, 2023
9b58944
fix invalid opt test
kentosugama Apr 19, 2023
dddfc63
remove unnecessary clone
kentosugama Apr 19, 2023
71885b1
Merge branch 'master' into wasm-opt
kentosugama Apr 19, 2023
e4bb808
different back tick notations
kentosugama Apr 19, 2023
99fe887
Merge branch 'master' into wasm-opt
kentosugama Apr 19, 2023
daa8a94
fix documentation
kentosugama Apr 20, 2023
cb3b012
simplify default optimization levels logic
kentosugama Apr 20, 2023
8baab3d
small change to ic-wasm
kentosugama Apr 20, 2023
83d54d7
remove use of serde alias
kentosugama Apr 20, 2023
65fef03
Merge branch 'master' into wasm-opt
kentosugama Apr 21, 2023
5993a8c
Use released ic-wasm
kentosugama Apr 21, 2023
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
Prev Previous commit
Next Next commit
Expose optimize feature
  • Loading branch information
kentosugama committed Apr 14, 2023
commit 4ad8f1374d280ec7ec2c73a395eec4931573b558
10 changes: 9 additions & 1 deletion docs/dfx-json-schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -415,6 +415,14 @@
"boolean",
"null"
]
},
"optimize": {
"title": "Optimize Canister WASM",
"description": "Whether run `ic-wasm shrink --optimize <level>` after building the Canister. Disabled by default.",
"type": [
"string",
"null"
]
}
}
},
Expand Down Expand Up @@ -858,4 +866,4 @@
]
}
}
}
}
5 changes: 5 additions & 0 deletions src/dfx-core/src/config/model/dfinity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,11 @@ pub struct ConfigCanistersCanister {
/// Disabled by default for custom canisters.
pub shrink: Option<bool>,

/// # Optimize Canister WASM
/// Whether run `ic-wasm shrink --optimize <level>` after building the Canister.
/// Disabled by default.
pub optimize: Option<String>,

/// # Metadata
/// Defines metadata sections to set in the canister .wasm
#[serde(default)]
Expand Down
15 changes: 12 additions & 3 deletions src/dfx/src/lib/builders/custom.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,11 +146,20 @@ impl CanisterBuilder for CustomBuilder {
}
}

let optimize = info.get_optimize();
let shrink = info.get_shrink().unwrap_or(false);
// Custom canister may have WASM gzipped
if shrink && is_wasm_format(&wasm)? {
info!(self.logger, "Shrink WASM module size.");
super::shrink_wasm(&wasm)?;
if is_wasm_format(&wasm)? {
if let Some(level) = optimize {
info!(
self.logger,
"Optimize and shrink WASM module at level {}", level
);
super::optimize_wasm(&wasm, level)?;
} else if shrink {
info!(self.logger, "Shrink WASM module size.");
super::shrink_wasm(&wasm)?;
}
}

Ok(BuildOutput {
Expand Down
14 changes: 14 additions & 0 deletions src/dfx/src/lib/builders/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -491,6 +491,20 @@ fn shrink_wasm(wasm_path: impl AsRef<Path>) -> DfxResult {
Ok(())
}

#[context("Failed to optimize wasm at {}.", &wasm_path.as_ref().display())]
fn optimize_wasm(wasm_path: impl AsRef<Path>, level: &str) -> DfxResult {
let wasm_path = wasm_path.as_ref();
let wasm = std::fs::read(wasm_path).context("Could not read the WASM module.")?;
let mut module =
ic_wasm::utils::parse_wasm(&wasm, true).context("Could not parse the WASM module.")?;
ic_wasm::shrink::shrink_with_wasm_opt(&mut module, level)
.context("Could not optimize the WASM module.")?;
module
.emit_wasm_file(wasm_path)
.with_context(|| format!("Could not write optimized WASM to {:?}.", wasm_path))?;
Ok(())
}

pub struct BuilderPool {
builders: BTreeMap<&'static str, Arc<dyn CanisterBuilder>>,
}
Expand Down
9 changes: 8 additions & 1 deletion src/dfx/src/lib/builders/motoko.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,8 +166,15 @@ impl CanisterBuilder for MotokoBuilder {
};
motoko_compile(&self.logger, cache.as_ref(), &params)?;

let optimize = canister_info.get_optimize();
let shrink = canister_info.get_shrink().unwrap_or(true);
if shrink {
if let Some(level) = optimize {
info!(
self.logger,
"Optimize and shrink WASM module at level {}", level
);
super::optimize_wasm(motoko_info.get_output_wasm_path(), level)?;
} else if shrink {
info!(self.logger, "Shrink WASM module size.");
super::shrink_wasm(motoko_info.get_output_wasm_path())?;
}
Expand Down
9 changes: 8 additions & 1 deletion src/dfx/src/lib/builders/rust.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,15 @@ impl CanisterBuilder for RustBuilder {
);
let output = cargo.output().context("Failed to run 'cargo build'. You might need to run `cargo update` (or a similar command like `cargo vendor`) if you have updated `Cargo.toml`, because `dfx build` uses the --locked flag with Cargo.")?;

let optimize = canister_info.get_optimize();
let shrink = canister_info.get_shrink().unwrap_or(true);
if shrink {
if let Some(level) = optimize {
info!(
self.logger,
"Optimize and shrink WASM module at level {}", level
);
super::optimize_wasm(rust_info.get_output_wasm_path(), level)?;
} else if shrink {
info!(self.logger, "Shrink WASM module size.");
super::shrink_wasm(rust_info.get_output_wasm_path())?;
}
Expand Down
6 changes: 6 additions & 0 deletions src/dfx/src/lib/canister_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ pub struct CanisterInfo {
post_install: Vec<String>,
main: Option<PathBuf>,
shrink: Option<bool>,
optimize: Option<String>,
metadata: CanisterMetadataConfig,
pull_ready: bool,
pull_dependencies: Vec<(String, CanisterId)>,
Expand Down Expand Up @@ -154,6 +155,7 @@ impl CanisterInfo {
post_install,
main: canister_config.main.clone(),
shrink: canister_config.shrink,
optimize: canister_config.optimize.clone(),
metadata,
pull_ready: canister_config.pull_ready,
pull_dependencies,
Expand Down Expand Up @@ -229,6 +231,10 @@ impl CanisterInfo {
self.shrink
}

pub fn get_optimize(&self) -> &Option<String> {
&self.optimize
}

pub fn get_build_wasm_path(&self) -> PathBuf {
self.output_root.join(&self.name).with_extension("wasm")
}
Expand Down