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

Compress wasm files for upload #6

Merged
merged 3 commits into from
Mar 31, 2024
Merged

Compress wasm files for upload #6

merged 3 commits into from
Mar 31, 2024

Conversation

rossta
Copy link
Contributor

@rossta rossta commented Mar 31, 2024

We observed that Amazon Cloudfront was not compressing the Rails WASM download so that the browser was having to transfer 130MB. Chrome was also not caching the file on disk for subsequent page loads.

We first added Cache-Control header for the upload to S3 after removing the previous uploads in #5.

We then updated the S3 bucket CORS policy to include allow all request headers to satisfy the Cloudfront compressed files requirement that the Origin include the Content-Length header.

We also learned that the Rails WASM upload is too large to be compressed by Cloudfront, per the Cloudfront docs:

CloudFront compresses objects that are between 1,000 bytes and 10,000,000 bytes in size

From a StackOverflow answer, we also learned that we could compress eagerly and set the Content-Encoding header on the upload.

We experimented with different brotli compression levels, i.e. brotli -q <1-11> and settled on level 8 compression as a good tradeoff between final compressed size and compression execution time:

For brotli -q <LEVEL> rails-7.1-ruby-3.3-web.wasm

  • LEVEL=4 time: 01.5s, size: 41M
  • LEVEL=6 time: 03.8s, size: 37M
  • LEVEL=8 time: 10.8s, size: 36M
  • LEVEL=9 time: 43.0s, size: 35M
  • LEVEL=10 time: 3m, size: 33M
  • LEVEL=11 time: 9m, size: 30M

We also looked at using the WebAssembly Binaryen wasm-opt utility as an optimization step but decided to omit as the step only reduced a small % of size while adding several minutes to the build.

@rossta rossta force-pushed the feat/optimize-wasm branch 7 times, most recently from eaf90f8 to ee4a61b Compare March 31, 2024 13:10
@rossta rossta merged commit 43e4cae into main Mar 31, 2024
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant