Skip to content

perf: reduce Go Wasm size and enable -Oz builds - #52

Open
johnlanni wants to merge 3 commits into
mainfrom
perf/reduce-wasm-size
Open

johnlanni wants to merge 3 commits into
mainfrom
perf/reduce-wasm-size

Conversation

@johnlanni

@johnlanni johnlanni commented Jul 11, 2026 •

Copy link
Copy Markdown
Contributor

Summary

  • preserve the existing pkg/wrapper HTTP API, including net/http.Header, in the default build
  • add an opt-in lightweight HTTP callout package at pkg/wrapper/httpcall
  • add the wasm_lite_http build tag to exclude the legacy net/http implementation only for migrated plugins
  • replace google/uuid inside the wrapper with a small RFC 9562 UUID v4 implementation backed by WASI random_get
  • replace internal content sniffing with the lightweight pkg/litehttp implementation
  • add a reusable release builder that runs Binaryen 130 with -Oz --enable-bulk-memory
  • make optimized Wasm the default for the request-block example and Wasm-mode tests
  • install and verify Binaryen 130 in CI while retaining WASM_SKIP_OPTIMIZATION=1 for diagnostics

Compatibility and opt-in usage

Existing plugins do not need to change. Without a build tag, wrapper.HttpClient, wrapper.NewClusterClient, wrapper.ResponseCallback, and their net/http.Header callback API remain available.

Plugins that want to remove net/http from the Wasm dependency graph migrate HTTP callouts to:

import "github.com/higress-group/wasm-go/pkg/wrapper/httpcall"

and build with:

GO_BUILD_TAGS=wasm_lite_http ./scripts/build-wasm.sh -o main.wasm .

GO_BUILD_TAGS is passed through as Go's -tags; direct builds can use go build -tags=wasm_lite_http .... The build tag is required because importing the parent pkg/wrapper package otherwise compiles its legacy net/http source file even when no legacy HTTP client method is called.

The tag only selects the HTTP API implementation. Binaryen -Oz runs in both profiles, and the Proxy-Wasm host ABI is unchanged.

Benchmark

Minimal wrapper plugin using wrapper.SetCtx, Go 1.24.4, Binaryen 130 -Oz, Envoy v1.27.2 V8 with a 0.25 CPU limit. Values are three alternating runs measured to starting main dispatch loop using the final PR code:

Profile Optimized Wasm size Runs Mean startup
default compatibility 4,973,632 B 13.656 / 13.330 / 13.542s 13.509s
wasm_lite_http 3,502,843 B 10.314 / 10.107 / 10.118s 10.180s
change -1,470,789 B (-29.6%) -3.329s (-24.6%)

The default dependency graph contains net/http; the lightweight graph does not. A bundled request-block build also decreased from 5,566,586 B to 4,300,419 B (-22.7%), showing that the package-level saving applies beyond the synthetic minimal plugin.

Verification

  • go test -race -count=1 -timeout=8m ./pkg/...
  • go test -tags=wasm_lite_http ./pkg/...
  • go vet ./pkg/...
  • go vet -tags=wasm_lite_http ./pkg/...
  • Go and optimized-Wasm tests for complex-http-call, http-call, rebuild-example, request-block, and safe-log-http-call
  • optimized-Wasm request-block test with GO_BUILD_TAGS=wasm_lite_http
  • WASI compilation of pkg/wrapper/httpcall
  • dependency-graph assertion that the lightweight profile does not include net/http
  • git diff --check, gofmt, and shell syntax checks

Signed-off-by: zty98751 <zty98751@alibaba-inc.com>
Signed-off-by: zty98751 <zty98751@alibaba-inc.com>
Signed-off-by: zty98751 <zty98751@alibaba-inc.com>
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