Shrink the release WASM (35.5 MiB) below the 10 MB CDN compression limit #4

Open opened by topos-backsword.test.pds.did.bot

The release protokill_wasm_frontend_bg.wasm is 37,202,477 bytes (35.5 MiB). Amplify serves through CloudFront, which only auto-compresses objects between 1,000 and 10,000,000 bytes, so players currently download the full 35.5 MiB uncompressed on every cold load.

I measured the options with bevy build --yes --release web --bundle. Each config was checked in a headless browser: the game loads and a keypress advances T:0 → T:1.

Config raw gzip -9 brotli -11
Today 37,202,477 10,064,915 6,336,538
Drop bevy debug feature 35,626,044 9,828,780 6,205,014
+ size-tuned web-release profile + wasm-opt -Oz 19,118,735 6,643,741 4,675,456
+ bevy_ratatui with an explicit Bevy feature list (local patch) 11,506,255 3,846,095 2,645,707
+ bevy_ratatui presenting the terminal as a Sprite instead of a UI ImageNode (local patch) 8,860,363 2,952,206 2,031,995

The last row was pixel-identical to the current build at 1280×800.

Step 1: build profile (no dependency changes) #

 [workspace.dependencies.bevy]
 version = "0.18"
 default-features = false
 features = [
-    # TODO figure out binary size issues
-    "debug",
     "webgpu",
 ]
+
+[profile.web-release]
+inherits = "release"
+opt-level = "z"
+lto = "fat"
+codegen-units = 1
+panic = "abort"
+strip = "debuginfo"

In scripts/deploy.sh: bevy build --yes --release web --bundle --wasm-opt=--strip-debug --wasm-opt=-Oz.

Gotchas:

  • strip = true drops the target_features section that wasm-opt needs, and the build fails. Use strip = "debuginfo".
  • The bevy CLI only accepts the = form of --wasm-opt=….
  • opt-level = "s" came out 5.6 MB larger than "z".

That gets to about 18.2 MiB, which is still above the CloudFront limit. Two ways to go further:

Step 2a: pre-compress on Amplify (no Rust changes) #

Compress the wasm in deploy.sh and add a customHttp.yml header so CloudFront passes it through:

customHeaders:
  - pattern: 'build/*.wasm'
    headers:
      - key: Content-Encoding
        value: br   # or gzip

Transfer becomes about 4.7 MB with brotli or 6.6 MB with gzip. Someone needs to check in a deployed environment that Amplify keeps the header and MIME type.

Step 2b: trim what bevy_ratatui pulls in (dependency decision) #

bevy_ratatui 0.11.1's windowed feature enables bevy/default, which brings in 2d, 3d, pbr, gltf, scene, animation, audio and more. It also draws the terminal through bevy_ui. A small [patch.crates-io] fork gets to 8.86 MB, which is under the limit, so CloudFront compresses it to about 2–3 MB automatically. The fork needs (a) an explicit feature list and (b) a one-line Sprite swap.

Upstream: bevy_ratatui 0.13 fixes the feature list but needs Bevy 0.20 (0.12 needs 0.19), and it is still UI-based. The feature-list half would make a clean upstream PR or 0.11.x backport. The Sprite half fits better as an optional feature.

How to handle step 2b is a maintainer call: carry the fork, upstream the fixes first, or skip it in favour of 2a.

(Investigation by an agent working on protokill; I can open the step 1 PR if wanted.)

Sign up or Sign in to add to the discussion.