Skip to content

Commit

Permalink
feat: add --wasm-opt option to further optimize the size (#20)
Browse files Browse the repository at this point in the history
* feat: add `--wasm-opt` option to further optimize the size
  • Loading branch information
willemneal authored Nov 21, 2022
1 parent 1ac1260 commit 9bb5f48
Show file tree
Hide file tree
Showing 4 changed files with 177 additions and 12 deletions.
150 changes: 143 additions & 7 deletions Cargo.lock

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

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,15 @@ keywords = ["WebAssembly", "Wasm", "wit", "WasmInterfaceTypes", "NEAR"]
license = "MIT"
name = "raen"
repository = "https://github.com/raendev/raen"
version = "0.1.0"
version = "0.1.1"
default-run = "raen"

[dependencies]
## Wit/Wasm tools
witme = {version = "0.3.1"}
witgen = "0.12.0"
cargo-witgen = "0.13.0"
wasm-opt = "0.110.1"

## Errors
anyhow = "1.0.51"
Expand Down
31 changes: 28 additions & 3 deletions src/raen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use cargo_metadata::{camino::Utf8PathBuf, DependencyKind, Package, Target};
use cargo_witgen::Witgen;
use clap::{Args, Parser};
use clap_cargo_extra::{ClapCargo, TargetTools};
use wasm_opt::OptimizationOptions;
use witme::app::NearCommand;

use crate::ext::{compress_file, get_time, PackageExt};
Expand Down Expand Up @@ -48,6 +49,9 @@ pub struct Build {
/// Only print build file path
#[clap(long, short = 'q')]
pub quiet: bool,
/// Use wasm-opt to further optimize the size of generated the Wasm binary
#[clap(long, short = 'w')]
pub wasm_opt: bool,
}

impl Raen {
Expand Down Expand Up @@ -191,7 +195,7 @@ struct Foo {}
let input = self.cargo.built_bin(t)?;
let output = self.output_bin(t)?;
let cmd = NearCommand::Inject {
input,
input: input.clone(),
output,
data: None,
file: Some(file),
Expand All @@ -200,10 +204,20 @@ struct Foo {}
cmd.run()?;
let output = format!("{:?}", bin_dir.join(bin_name));
let output = output.trim_matches('"');
let extra = if self.wasm_opt {
let diff = std::fs::metadata(&input)?.len() as i64 - self.optimize(output)? as i64;
if diff < 0 {
"The orginal file was smaller than the added types. Report to the `https://github.com/raendev/raen/issues` for help.".to_owned()
} else {
format!("Saved {diff} bytes")
}
} else {
"".to_owned()
};
if self.quiet {
println!("{}", output);
} else {
println!("Built to:\n{}", output);
println!("Built to:\n{}\n{}", output, extra);
}
Ok(())
}
Expand Down Expand Up @@ -274,6 +288,17 @@ struct Foo {}
Ok(res)
})
.collect::<Result<HashSet<_>>>()
.and_then(|set| Ok(set.into_iter().collect::<Vec<_>>()))
.map(IntoIterator::into_iter)
.map(Iterator::collect::<Vec<_>>)
}

fn optimize(&self, file: &str) -> Result<u64> {
let input = PathBuf::from(file);
let output = &input;
let wasm_out = PathBuf::from(output);
let mut options = OptimizationOptions::new_optimize_for_size_aggressively();
options.converge = true;
options.run(input, &wasm_out)?;
Ok(std::fs::metadata(wasm_out)?.len())
}
}
5 changes: 4 additions & 1 deletion tests/it/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@ fn compile() {
use std::fs;
fs::remove_dir_all("./target/res").unwrap_or_default();
fs::remove_dir_all("./target/wit").unwrap_or_default();
let mut build = Build::default();
let mut build = Build {
wasm_opt: true,
..Build::default()
};
build.cargo.cargo_build.release = true;
build.cargo.workspace.exclude.push("raen".to_string());
build.cargo.workspace.workspace = true;
Expand Down

0 comments on commit 9bb5f48

Please sign in to comment.