-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Optimize binary with
wasm-opt
in release mode (#206)
# Objective Closes #196, unblocks <TheBevyFlock/bevy_new_2d#312>. With `wasm-opt`, we can further increase the performance and reduce the file size of the Wasm binary we use for web builds. This speeds ups the app both in-game and the loading times. # Solution As a simple first solution, we add a hard-coded size optimization pass in release mode. In future PRs, we can make this more configurable. To the user, we log the time the optimization took as well as the file size reduction as percentage. This is behind the `wasm-opt` feature flag (currently disabled by default), to give the user a way to turn this off and because this increases compile times of the CLI quite a bit.
- Loading branch information
1 parent
5d99f41
commit 4e740c7
Showing
9 changed files
with
281 additions
and
28 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,3 +5,4 @@ pub mod external_cli; | |
pub mod lint; | ||
pub mod run; | ||
pub mod template; | ||
pub(crate) mod web; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
#[cfg(feature = "wasm-opt")] | ||
pub(crate) mod wasm_opt; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
use std::{fs, path::Path, time::Instant}; | ||
|
||
use anyhow::Context as _; | ||
|
||
use crate::run::BinTarget; | ||
|
||
/// Optimize the binary with wasm-opt. | ||
pub(crate) fn optimize_bin(bin_target: &BinTarget) -> anyhow::Result<()> { | ||
let wasm_path = bin_target | ||
.artifact_directory | ||
.clone() | ||
.join(format!("{}_bg.wasm", bin_target.bin_name)); | ||
|
||
optimize_path(&wasm_path) | ||
} | ||
|
||
/// Optimize the Wasm binary at the given path with wasm-opt. | ||
fn optimize_path(path: &Path) -> anyhow::Result<()> { | ||
println!("Optimizing with wasm-opt..."); | ||
|
||
let start = Instant::now(); | ||
let size_before = fs::metadata(path)?.len(); | ||
|
||
wasm_opt::OptimizationOptions::new_optimize_for_size() | ||
.run(path, path) | ||
.context("failed to optimize with wasm-opt")?; | ||
|
||
let size_after = fs::metadata(path)?.len(); | ||
let size_reduction = 1. - (size_after as f32) / (size_before as f32); | ||
let duration = start.elapsed(); | ||
|
||
println!( | ||
" Finished in {duration:.2?}. Size reduced by {:.0}%.", | ||
size_reduction * 100. | ||
); | ||
|
||
Ok(()) | ||
} |