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
Define enums for optimization levels
  • Loading branch information
kentosugama committed Apr 18, 2023
commit 7d2c353c01fade44cdbeb3826691e3561308a779
7 changes: 4 additions & 3 deletions docs/dfx-json-schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -419,9 +419,10 @@
"optimize": {
"title": "Optimize Canister WASM",
"description": "Invoke wasm level optimizations after building the Canister. Optimization level can be set to \"cycles\" to optimize for cycle usage, \"size\" to optimize for binary size, or any of \"O4, O3, O2, O1, O0, Oz, Os\". Disabled by default.",
"type": [
"string",
"null"
"allOf": [
{
"$ref": "#/definitions/WasmOptLevel"
}
]
}
}
Expand Down
28 changes: 27 additions & 1 deletion src/dfx-core/src/config/model/dfinity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,31 @@ pub struct ConfigCanistersCanisterRemote {
pub id: BTreeMap<String, Principal>,
}

#[derive(Copy, Clone, Debug, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
pub enum WasmOptLevel {
// Defaults to O3
#[serde(rename = "cycles")]
Cycles,
// Defaults to Oz
#[serde(rename = "size")]
Size,
// Specific performance levels
O4,
O3,
O2,
O1,
O0,
// Specific size levels
Oz,
Os,
}

impl std::fmt::Display for WasmOptLevel {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
std::fmt::Debug::fmt(self, f)
}
}

#[derive(Copy, Clone, Debug, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
#[serde(rename_all = "lowercase")]
pub enum MetadataVisibility {
Expand Down Expand Up @@ -198,7 +223,8 @@ pub struct ConfigCanistersCanister {
/// # Optimize Canister WASM
/// Invoke wasm level optimizations after building the Canister. Optimization level can be set to \"cycles\" to optimize for cycle usage, \"size\" to optimize for binary size, or any of \"O4, O3, O2, O1, O0, Oz, Os\". Disabled by default.
/// Disabled by default.
pub optimize: Option<String>,
#[serde(default)]
pub optimize: Option<WasmOptLevel>,

/// # Metadata
/// Defines metadata sections to set in the canister .wasm
Expand Down
12 changes: 6 additions & 6 deletions src/dfx/src/lib/builders/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use crate::lib::environment::Environment;
use crate::lib::error::DfxResult;
use crate::lib::models::canister::CanisterPool;
use crate::util::check_candid_file;
use dfx_core::config::model::dfinity::{Config, Profile};
use dfx_core::config::model::dfinity::{Config, Profile, WasmOptLevel};
use dfx_core::network::provider::get_network_context;
use dfx_core::util;

Expand Down Expand Up @@ -492,16 +492,16 @@ fn shrink_wasm(wasm_path: impl AsRef<Path>) -> DfxResult {
}

#[context("Failed to optimize wasm at {}.", &wasm_path.as_ref().display())]
fn optimize_wasm(wasm_path: impl AsRef<Path>, level: &str) -> DfxResult {
fn optimize_wasm(wasm_path: impl AsRef<Path>, level: &WasmOptLevel) -> 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.")?;
match level {
// O3 empirically gives best cycle savings
"cycles" => ic_wasm::shrink::shrink_with_wasm_opt(&mut module, "O3"),
"size" => ic_wasm::shrink::shrink_with_wasm_opt(&mut module, "Oz"),
_ => ic_wasm::shrink::shrink_with_wasm_opt(&mut module, level),
// O3 and Oz empirically give best cycle savings and code size savings respectively
WasmOptLevel::Cycles => ic_wasm::shrink::shrink_with_wasm_opt(&mut module, "O3"),
WasmOptLevel::Size => ic_wasm::shrink::shrink_with_wasm_opt(&mut module, "Oz"),
_ => ic_wasm::shrink::shrink_with_wasm_opt(&mut module, &level.to_string()),
}
.context("Could not optimize the WASM module.")?;

Expand Down
5 changes: 3 additions & 2 deletions src/dfx/src/lib/canister_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use crate::lib::canister_info::motoko::MotokoCanisterInfo;
use crate::lib::error::DfxResult;
use dfx_core::config::model::dfinity::{
CanisterDeclarationsConfig, CanisterMetadataSection, CanisterTypeProperties, Config,
WasmOptLevel,
};
use dfx_core::network::provider::get_network_context;
use dfx_core::util;
Expand Down Expand Up @@ -52,7 +53,7 @@ pub struct CanisterInfo {
post_install: Vec<String>,
main: Option<PathBuf>,
shrink: Option<bool>,
optimize: Option<String>,
optimize: Option<WasmOptLevel>,
metadata: CanisterMetadataConfig,
pull_ready: bool,
pull_dependencies: Vec<(String, CanisterId)>,
Expand Down Expand Up @@ -231,7 +232,7 @@ impl CanisterInfo {
self.shrink
}

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

Expand Down