From fced95dd48254a94841697d5a896ffebb15bfd47 Mon Sep 17 00:00:00 2001 From: Zxilly Date: Mon, 27 May 2024 13:50:47 +0800 Subject: [PATCH] perf: optimize wasm with binaryen --- .github/workflows/build-explorer.yml | 33 +++++++++++++++----- .github/workflows/build-webui.yml | 21 ++++++++++--- scripts/wasm.py | 46 ++++++++++++++++++++++++++-- 3 files changed, 86 insertions(+), 14 deletions(-) diff --git a/.github/workflows/build-explorer.yml b/.github/workflows/build-explorer.yml index b44de7d306..e2469a7790 100644 --- a/.github/workflows/build-explorer.yml +++ b/.github/workflows/build-explorer.yml @@ -19,6 +19,11 @@ jobs: with: fetch-depth: 0 + - name: Set up Node.js + uses: actions/setup-node@v4 + with: + node-version: '21' + - name: Set up pnpm uses: pnpm/action-setup@v4 with: @@ -26,12 +31,18 @@ jobs: package_json_file: 'ui/package.json' version: latest - - name: Set up Node.js - uses: actions/setup-node@v4 + - name: Get pnpm store directory + shell: bash + run: | + echo "STORE_PATH=$(pnpm store path --silent)" >> $GITHUB_ENV + + - uses: actions/cache@v4 + name: Setup pnpm cache with: - node-version: '21' - cache: 'pnpm' - cache-dependency-path: './ui/pnpm-lock.yaml' + path: ${{ env.STORE_PATH }} + key: ${{ runner.os }}-pnpm-store-${{ hashFiles('**/pnpm-lock.yaml') }} + restore-keys: | + ${{ runner.os }}-pnpm-store- - name: Setup Go uses: actions/setup-go@v5 @@ -48,9 +59,17 @@ jobs: cache: 'pip' cache-dependency-path: 'scripts/requirements.txt' - - name: Install dependencies + - name: Install node dependencies working-directory: ./ui - run: pnpm install + run: | + pnpm install + + - name: Setup Binaryen + # fixme: use acifani/setup-tinygo after pr acceptance + uses: Zxilly/setup-tinygo@main + with: + install-tinygo: false + binaryen-version: '117' - name: Add python dependencies run: | diff --git a/.github/workflows/build-webui.yml b/.github/workflows/build-webui.yml index 7eb10929d4..5465cb970f 100644 --- a/.github/workflows/build-webui.yml +++ b/.github/workflows/build-webui.yml @@ -34,6 +34,11 @@ jobs: with: fetch-depth: 0 + - name: Set up Node.js + uses: actions/setup-node@v4 + with: + node-version: '21' + - name: Set up pnpm uses: pnpm/action-setup@v4 with: @@ -41,12 +46,18 @@ jobs: package_json_file: 'ui/package.json' version: latest - - name: Set up Node.js - uses: actions/setup-node@v4 + - name: Get pnpm store directory + shell: bash + run: | + echo "STORE_PATH=$(pnpm store path --silent)" >> $GITHUB_ENV + + - uses: actions/cache@v4 + name: Setup pnpm cache with: - node-version: '21' - cache: 'pnpm' - cache-dependency-path: './ui/pnpm-lock.yaml' + path: ${{ env.STORE_PATH }} + key: ${{ runner.os }}-pnpm-store-${{ hashFiles('**/pnpm-lock.yaml') }} + restore-keys: | + ${{ runner.os }}-pnpm-store- - name: Install dependencies working-directory: ./ui diff --git a/scripts/wasm.py b/scripts/wasm.py index 0d3e6b26de..d74467536a 100644 --- a/scripts/wasm.py +++ b/scripts/wasm.py @@ -1,5 +1,7 @@ import os.path +import shutil import subprocess +import tempfile from lib.utils import get_project_root, require_go @@ -8,8 +10,17 @@ def wasm_location() -> str: return os.path.join(get_project_root(), "ui", "gsa.wasm") +def require_binaryen(): + o = shutil.which("wasm-opt") + if o is None: + print("wasm-opt not found in PATH. Please install binaryen.") + exit(1) + return o + + if __name__ == '__main__': go = require_go() + opt = require_binaryen() env = { "GOOS": "js", @@ -17,13 +28,16 @@ def wasm_location() -> str: } env.update(os.environ) + tmp_file = tempfile.mktemp(prefix="gsa", suffix=".wasm") + try: + print("Building wasm binary") + result = subprocess.run( [ go, "build", - "-o", wasm_location(), - "-ldflags=-s -w", + "-o", tmp_file, "./cmd/wasm/main_wasm.go" ], text=True, @@ -35,8 +49,36 @@ def wasm_location() -> str: ) result.check_returncode() + + print("Wasm binary built successfully") except subprocess.CalledProcessError as e: print("Error building wasm:") print(f"stdout: {e.stdout}") print(f"stderr: {e.stderr}") exit(1) + + try: + print("Optimizing wasm") + + result = subprocess.run( + [ + opt, + tmp_file, + "-O4", + "--enable-bulk-memory", + "-o", wasm_location() + ], + text=True, + stderr=subprocess.PIPE, + stdout=subprocess.PIPE, + timeout=120 + ) + + result.check_returncode() + + print("Wasm optimized successfully") + except subprocess.CalledProcessError as e: + print("Error optimizing wasm:") + print(f"stdout: {e.stdout}") + print(f"stderr: {e.stderr}") + exit(1)