Skip to content

Commit

Permalink
feat(wasm-builder): Set optimisation params (#2973)
Browse files Browse the repository at this point in the history
  • Loading branch information
ukint-vs authored Jul 22, 2023
1 parent 96b0558 commit e1059c0
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 20 deletions.
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

0 comments on commit e1059c0

Please sign in to comment.