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

Optimize binary with wasm-opt in release mode #206

Merged
merged 5 commits into from
Dec 29, 2024
Merged

Conversation

TimJentzsch
Copy link
Collaborator

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.

@TimJentzsch TimJentzsch added C-Feature Make something new possible A-Run Related to the bevy run command A-Build Related to the bevy build command A-Web Building or running Bevy apps targeting the browser labels Dec 27, 2024
@TimJentzsch
Copy link
Collaborator Author

On my laptop on a simple game example, it currently takes 250s and reduces the file size by 34%.

These numbers will probably change quite a bit with #199 though, so I wouldn't read into it too much at this stage.

src/web/wasm_opt.rs Outdated Show resolved Hide resolved
src/build/args.rs Show resolved Hide resolved
@@ -19,6 +19,12 @@ impl RunArgs {
matches!(self.subcommand, Some(RunSubcommands::Web(_)))
}

/// Whether to build with optimizations.
#[cfg(feature = "wasm-opt")]
pub(crate) fn is_release(&self) -> bool {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here

@BD103
Copy link
Member

BD103 commented Dec 27, 2024

How do I use this? I ran cargo build -F wasm-opt then bevy build web --release, but it gave an error:

$ bevy build web --release
error: unexpected argument '--release' found

Usage: bevy build web

For more information, try '--help'.

@DaAlbrecht
Copy link
Contributor

How do I use this? I ran cargo build -F wasm-opt then bevy build web --release, but it gave an error:

$ bevy build web --release

error: unexpected argument '--release' found



Usage: bevy build web



For more information, try '--help'.

Cant test it right now but if i remember correctly it was 'bevy build -r web'

@TimJentzsch
Copy link
Collaborator Author

TimJentzsch commented Dec 27, 2024

How do I use this? I ran cargo build -F wasm-opt then bevy build web --release, but it gave an error:

$ bevy build web --release

error: unexpected argument '--release' found



Usage: bevy build web



For more information, try '--help'.

Cant test it right now but if i remember correctly it was 'bevy build -r web'

That's right, the --release flag needs to come before the web subcommand (because it can also be used on native builds)

Copy link
Member

@BD103 BD103 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah yup, bevy build --release web did the trick! This looks good, merge it once you're happy with the state it's in. :)

@TimJentzsch TimJentzsch enabled auto-merge (squash) December 29, 2024 17:24
@TimJentzsch TimJentzsch merged commit 4e740c7 into main Dec 29, 2024
8 checks passed
@TimJentzsch TimJentzsch deleted the 196-wasm-opt branch December 29, 2024 17:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-Build Related to the bevy build command A-Run Related to the bevy run command A-Web Building or running Bevy apps targeting the browser C-Feature Make something new possible
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add option to run wasm-opt when building for the web
3 participants