trunk icon indicating copy to clipboard operation
trunk copied to clipboard

feat: Add compression support for wasm binary

Open bigsaltyfishes opened this issue 5 months ago • 2 comments

This is an attempt to add compression support for wasm binary.

  • Add data-compression-algorithm and data-compression-level attributes.
  • Enable gzip compression by default on release build, can be disabled by set data-compression-level="0".
  • Use DecompressionStream API to decompress at runtime.

bigsaltyfishes avatar Jun 07 '25 08:06 bigsaltyfishes

Thanks for the PR. I guess this could be a valuable addition. There are two things:

  • I think it would make sense doing a quick check/proof on how much this would safe. Showing one or two examples.
  • It looks like this is missing out on the else when using the more complex initializer. It also seems to repeat code from the initializer, which does something similar. Maybe it's time to collapse both, and have the "non initializer" version be a special (simpler) form of the initializer version?

ctron avatar Jun 10 '25 07:06 ctron

Thanks for the PR. I guess this could be a valuable addition. There are two things:

  • I think it would make sense doing a quick check/proof on how much this would safe. Showing one or two examples.
  • It looks like this is missing out on the else when using the more complex initializer. It also seems to repeat code from the initializer, which does something similar. Maybe it's time to collapse both, and have the "non initializer" version be a special (simpler) form of the initializer version?

I've merged repeat code and add missing compression support for custom initializer. Here are the file size savings with compression enabled. All examples were built with the release profile. By default, gzip compression is enabled with the default compression level:

Example Raw wasm-opt processed (opt-level='z') Compressed
initializer 35 KB 23 KB 11 KB
leptos 137 KB 93 KB 36 KB
yew 450 KB 307 KB 104 KB
My Blog (Leptos 0.8) 1349 KB 819 KB 330 KB

All examples save about 50% to 70% in size.

bigsaltyfishes avatar Jun 10 '25 10:06 bigsaltyfishes