Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(wasm-builder): Set optimisation params #2973

Merged
merged 3 commits into from
Jul 22, 2023
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
33 changes: 17 additions & 16 deletions examples/fungible-token/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,42 +54,44 @@ impl FungibleToken {
}

fn mint(&mut self, amount: u128) {
let source = msg::source();
self.balances
.entry(msg::source())
.entry(source)
.and_modify(|balance| *balance += amount)
.or_insert(amount);
self.total_supply += amount;
msg::reply(
FTEvent::Transfer {
from: ZERO_ID,
to: msg::source(),
to: source,
amount,
},
0,
)
.unwrap();
}

/// Executed on receiving `fungible-token-messages::BurnInput`.
fn burn(&mut self, amount: u128) {
if self.balances.get(&msg::source()).unwrap_or(&0) < &amount {
let source = msg::source();
if self.balances.get(&source).unwrap_or(&0) < &amount {
panic!("Amount exceeds account balance");
}
self.balances
.entry(msg::source())
.entry(source)
.and_modify(|balance| *balance -= amount);
self.total_supply -= amount;

msg::reply(
FTEvent::Transfer {
from: msg::source(),
from: source,
to: ZERO_ID,
amount,
},
0,
)
.unwrap();
}

/// Executed on receiving `fungible-token-messages::TransferInput` or `fungible-token-messages::TransferFromInput`.
/// Transfers `amount` tokens from `sender` account to `recipient` account.
fn transfer(&mut self, from: &ActorId, to: &ActorId, amount: u128) {
if from == &ZERO_ID || to == &ZERO_ID {
Expand Down Expand Up @@ -119,17 +121,19 @@ impl FungibleToken {
.unwrap();
}

/// Executed on receiving `fungible-token-messages::ApproveInput`.
fn approve(&mut self, to: &ActorId, amount: u128) {
if to == &ZERO_ID {
panic!("Approve to zero address");
}
let source = msg::source();
self.allowances
.entry(msg::source())
.entry(source)
.or_default()
.insert(*to, amount);
msg::reply(
FTEvent::Approve {
from: msg::source(),
from: source,
to: *to,
amount,
},
Expand All @@ -139,17 +143,14 @@ impl FungibleToken {
}

fn can_transfer(&mut self, from: &ActorId, amount: u128) -> bool {
if from == &msg::source() || self.balances.get(&msg::source()).unwrap_or(&0) >= &amount {
let source = msg::source();
if from == &source || self.balances.get(&source).unwrap_or(&0) >= &amount {
return true;
}
if let Some(allowed_amount) = self
.allowances
.get(from)
.and_then(|m| m.get(&msg::source()))
{
if let Some(allowed_amount) = self.allowances.get(from).and_then(|m| m.get(&source)) {
if allowed_amount >= &amount {
self.allowances.entry(*from).and_modify(|m| {
m.entry(msg::source()).and_modify(|a| *a -= amount);
m.entry(source).and_modify(|a| *a -= amount);
});
return true;
}
Expand Down
1 change: 1 addition & 0 deletions utils/wasm-builder/src/optimize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,7 @@ pub fn do_optimization(
"z" => OptimizationOptions::new_optimize_for_size_aggressively(),
_ => panic!("Invalid optimization level {}", optimization_level),
}
.shrink_level(wasm_opt::ShrinkLevel::Level2)
.add_pass(Pass::Dae)
.add_pass(Pass::Vacuum)
// the memory in our module is imported, `wasm-opt` needs to be told that
Expand Down
11 changes: 7 additions & 4 deletions utils/wasm-builder/src/wasm_project.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ use std::{
};
use toml::value::Table;

const OPT_LEVEL: &str = "z";

/// Enum defining type of binary compiling: production program or metawasm.
pub enum ProjectType {
Program(Option<MetadataRepr>),
Expand Down Expand Up @@ -158,11 +160,12 @@ impl WasmProject {
lib.insert("crate-type".into(), vec!["cdylib".to_string()].into());

let mut dev_profile = Table::new();
dev_profile.insert("opt-level".into(), "s".into());
dev_profile.insert("opt-level".into(), OPT_LEVEL.into());

let mut release_profile = Table::new();
release_profile.insert("lto".into(), true.into());
release_profile.insert("opt-level".into(), "s".into());
release_profile.insert("lto".into(), "fat".into());
release_profile.insert("opt-level".into(), OPT_LEVEL.into());
release_profile.insert("codegen-units".into(), 1.into());

let mut production_profile = Table::new();
production_profile.insert("inherits".into(), "release".into());
Expand Down Expand Up @@ -316,7 +319,7 @@ extern "C" fn metahash() {{
let path = optimize::optimize_wasm(
original_copy_wasm_path.clone(),
opt_wasm_path.clone(),
"s",
"4",
true,
)
.map(|res| {
Expand Down