Skip to content

Commit

Permalink
perf: optimize wasm with binaryen
Browse files Browse the repository at this point in the history
  • Loading branch information
Zxilly committed May 27, 2024
1 parent 14ccba3 commit fced95d
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 14 deletions.
33 changes: 26 additions & 7 deletions .github/workflows/build-explorer.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,30 @@ 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:
standalone: true
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
Expand All @@ -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: |
Expand Down
21 changes: 16 additions & 5 deletions .github/workflows/build-webui.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,19 +34,30 @@ 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:
standalone: true
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
Expand Down
46 changes: 44 additions & 2 deletions scripts/wasm.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import os.path
import shutil
import subprocess
import tempfile

from lib.utils import get_project_root, require_go

Expand All @@ -8,22 +10,34 @@ 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",
"GOARCH": "wasm",
}
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,
Expand All @@ -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)

0 comments on commit fced95d

Please sign in to comment.