How to Optimize Go-Compiled WASM Binary Size and Execution Speed?

Problem Statement

When I compiled Go code to WASM using:

bashGOOS=wasip1 GOARCH=wasm go build -o contract.wasm main.go

I found that Go-compiled WASM files are significantly larger (3–5×) and exhibit slower execution speed in runtimes like wasmer than those compiled with TinyGo for the same codebase.

Questions

  1. Why is Go’s WASM binary so large?

    • Upon converting the WASM files to C using wasm2c, it was observed that Go-compiled files contain a significant number of switch-case statements, along with a large amount of runtime-related functions. Do these code be the reason for the bloated WASM size and low runtime efficiency?
  2. How to optimize Go-Compiled Are there compiler flags to reduce WASM size/improve speed?

    • Are there any compiler flags available to reduce WASM size/improve execution speed?
    • Could Go’s WASM backend leverage LLVM (like TinyGo) for better optimizations?

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.