How to create smaller WebAssembly binaries?

Hi Haskell Discourse,

I learned Haskell this year, and am trying to use it in my business.

I’m using Shopify Functions, which allows me to add custom functionality to Shopify, as long as I produce a WebAssembly binary. So, I tried to build a Shopify Function in Haskell using its new WASM backend.

Turns out a “Hello, world!” app is 1.1M (928k after running wasm-opt). :sweat_smile:

This far exceeds Shopify’s limit of 256kb.

Anybody know how to reduce WebAssembly filesize? Is it okay to use the GHC -dynamic flag when compiling to WASM, maybe?

8 Likes

I could be very wrong, feel free to correct anything I say below:

I don’t think -dynamic would help here because whether the assembly code for the GHC runtime is in the same file as the assembly code for your application or it’s in a different file, you’d still have a total 1.1M of code.

Wizer might have some additional arguments for optimizing for size. I think GHC does too. But it might be possible that the GHC runtime is just that big.

1 Like

I second the above answer.

If you wish to achieve very small size, you need to choose a language without a runtime (C, Rust, etc). I don’t know what the minimal size of compiled Haskell runtime is, but even with empty program doing hello world, you won’t get smaller than that.

Could a custom build of GHC with optimizations (such as inlining) disabled for base help? It should cut down the size radically at the expense of performance, which seems to be a lesser issue here.

Thanks for all your responses.

It looks like there might be a fundamental mismatch between my use case, and what Haskell is intended to do.

My goal is essentially: “read some JSON configuration via stdin, and execute logic using as few instructions as possible, while ensuring correctness.”

If I ever come up with a Haskell-compatible solution, I’ll come back and update this thread.

5 Likes

FWIW I don’t think wasm will help here because wasm doesnt have a notion of file handles/stdin

While this is true, Shopify Functions, which I’m targeting, is based on WASI, which does have handles.

Thanks for all your input so far.

1 Like

C has a runtime, it’s just very lightweight.

3 Likes