减少 WASM 二进制大小

  • Project: Building Warp, an intelligent terminal in Rust and cross-compiling it to web via WASM.
  • Challenges: The Warp executable was too large (21.4MB after gzip compression), and bringing it to the web brought engineering challenges.
  • Engineering Choices:

    • For showing a background theme image directly in the app, bundling it into the executable is better for the user experience on desktop but not on web due to loading time.
    • In cargo build configuration:

      • Set opt-level to "s" for a more conservative profile to avoid performance hits at runtime instead of aggressively optimizing for size by disabling loop vectorization.
      • Enabled lto to allow LLVM to perform link-time optimizations and won back about one megabyte pre-compression.
      • Introduced wasm-split with flags –strip and –strip-names to extricate debug information from the binary.
      • Decided not to use build-std as it is still under development and considered unstable.
  • Asset Management:

    • Re-designed how Warp manages assets by introducing async assets.
    • Bundled assets using RustEmbed in non-web builds and made them fetchable from a URL in web builds.
    • Used an enum to identify asset sources and built a Rust macro to resolve file names to AssetSource at compile time based on the build target.
  • User Experience: Removing assets from the binary may make some operations network-bound, but the difference in user experience between bundled and fetched assets is mostly indistinguishable.
  • Results: The WASM binary is about 65% smaller than the desktop binaries, and the team is looking to make more improvements in the future. They are excited to introduce Warp on the web with good performance.
阅读 4
0 条评论