perf(wasm): optimize build flags for runtime speed

- Add -gcflags="-l=4 -trimpath" for aggressive inlining - Add -ldflags="-s -w -buildid=" to strip symbols and build ID - Add -tags=prod to enable production-only code path - Implement dual-pass wasm-opt: -O4 for speed, then -Oz for size - Enable bulk-memory, sign-ext, and reference-types features This prioritizes runtime performance over binary size.

cognitive committed Dec 30, 2025 at 00:22 UTC f5d1dffae4797780c12d25a62142eee2883b2600
1 file changed +24 -8
Makefile
+24 -8
@@ -30,19 +30,35 @@ build-protoc:
30 portal/core/proto/rdverb/rdverb.proto
31
32 # Build WASM artifacts with wasm-opt optimization and generate manifest
33 +# Production build: strips all debug symbols, prioritizes runtime speed
34 build-wasm:
34 - @echo "[wasm] building webclient WASM..."
35 + @echo "[wasm] building webclient WASM (production, stripped symbols)..."
36 @mkdir -p cmd/relay-server/dist/wasm
36 - GOOS=js GOARCH=wasm go build -trimpath -ldflags "-s -w" -o cmd/relay-server/dist/wasm/portal.wasm ./cmd/webclient
37 -
38 - @echo "[wasm] optimizing with wasm-opt..."
37 + @# -trimpath: removes file system paths from stack traces
38 + @# -gcflags="-l=4": inlining optimization (level 4 = aggressive)
39 + @# -ldflags "-s -w": -s strips symbol table, -w strips DWARF debug info
40 + @# -buildid=: removes build ID for reproducible builds
41 + @# -tags=prod: enables production-only code (no logging, no HTML injection in WASM)
42 + GOOS=js GOARCH=wasm go build \
43 + -trimpath \
44 + -gcflags="-l=4 -trimpath" \
45 + -ldflags="-s -w -buildid=" \
46 + -tags=prod \
47 + -o cmd/relay-server/dist/wasm/portal.wasm ./cmd/webclient
48 +
49 + @echo "[wasm] optimizing with wasm-opt (dual-pass: O4 then Oz)..."
50 @if command -v wasm-opt >/dev/null 2>&1; then \
40 - wasm-opt -Oz --enable-bulk-memory cmd/relay-server/dist/wasm/portal.wasm -o cmd/relay-server/dist/wasm/portal.wasm.tmp && \
41 - mv cmd/relay-server/dist/wasm/portal.wasm.tmp cmd/relay-server/dist/wasm/portal.wasm; \
42 - echo "[wasm] optimization complete"; \
51 + echo "[wasm] pass 1: -O4 for runtime performance..."; \
52 + wasm-opt -O4 --enable-bulk-memory --enable-sign-ext --enable-reference-types \
53 + cmd/relay-server/dist/wasm/portal.wasm -o cmd/relay-server/dist/wasm/portal.wasm.tmp && \
54 + echo "[wasm] pass 2: -Oz for size optimization..."; \
55 + wasm-opt -Oz --enable-bulk-memory --enable-sign-ext --enable-reference-types \
56 + cmd/relay-server/dist/wasm/portal.wasm.tmp -o cmd/relay-server/dist/wasm/portal.wasm && \
57 + rm -f cmd/relay-server/dist/wasm/portal.wasm.tmp; \
58 + echo "[wasm] dual-pass optimization complete (O4 -> Oz)"; \
59 else \
60 echo "[wasm] WARNING: wasm-opt not found, skipping optimization"; \
45 - echo "[wasm] Install binaryen for smaller WASM files: brew install binaryen (macOS) or apt-get install binaryen (Linux)"; \
61 + echo "[wasm] Install binaryen for WASM optimization: brew install binaryen (macOS) or apt-get install binaryen (Linux)"; \
62 fi
63
64 @echo "[wasm] calculating SHA256 hash..."