Skip to content

Commit f8910ac

Browse files
Performance: Pin binaryen 121, two-pass wasm-opt, and version.json cache-busting.
1 parent abea722 commit f8910ac

5 files changed

Lines changed: 56 additions & 19 deletions

File tree

.github/workflows/pages.yml

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,20 +35,46 @@ jobs:
3535
working-directory: compiler
3636

3737
- name: Install wasm-opt
38-
run: sudo apt-get update && sudo apt-get install -y binaryen
38+
run: |
39+
curl -sSL https://github.com/WebAssembly/binaryen/releases/download/version_121/binaryen-version_121-x86_64-linux.tar.gz \
40+
| tar -xz --strip-components=2 -C /usr/local/bin binaryen-version_121/bin/wasm-opt
41+
wasm-opt --version
3942
4043
- name: Build wasm
4144
run: cargo build --target wasm32-unknown-unknown --features wasm --lib --release
4245
working-directory: compiler
4346

47+
- name: Size (unoptimized)
48+
run: ls -lh target/wasm32-unknown-unknown/release/compiler_lib.wasm
49+
working-directory: compiler
50+
4451
- name: Optimize wasm
4552
run: |
46-
wasm-opt -Oz --converge --strip-debug --strip-producers \
53+
INPUT=compiler/target/wasm32-unknown-unknown/release/compiler_lib.wasm
54+
55+
wasm-opt -Oz --converge \
56+
--generate-global-effects \
57+
--strip-debug --strip-producers \
4758
--enable-bulk-memory \
4859
--enable-nontrapping-float-to-int \
4960
--enable-sign-ext \
50-
-o compiler/target/wasm32-unknown-unknown/release/compiler_lib.wasm \
51-
compiler/target/wasm32-unknown-unknown/release/compiler_lib.wasm
61+
-tnh \
62+
-o /tmp/wasm_stage1.wasm "$INPUT"
63+
64+
wasm-opt --flatten --rereloop -Oz -Oz \
65+
-o "$INPUT" /tmp/wasm_stage1.wasm
66+
67+
rm /tmp/wasm_stage1.wasm
68+
69+
- name: Write version manifest
70+
run: |
71+
HASH=$(sha256sum compiler/target/wasm32-unknown-unknown/release/compiler_lib.wasm \
72+
| cut -c1-12)
73+
printf '{"v":"%s"}\n' "$HASH" > demo/version.json
74+
75+
- name: Size (optimized)
76+
run: ls -lh target/wasm32-unknown-unknown/release/compiler_lib.wasm
77+
working-directory: compiler
5278

5379
- name: Stage demo
5480
run: cp compiler/target/wasm32-unknown-unknown/release/compiler_lib.wasm demo/

.github/workflows/wasm.yml

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,9 @@ jobs:
2727
- name: Install CLI Tools
2828
run: |
2929
cargo install cargo-shear
30-
sudo apt update
31-
sudo apt-get install -y binaryen
30+
curl -sSL https://github.com/WebAssembly/binaryen/releases/download/version_121/binaryen-version_121-x86_64-linux.tar.gz \
31+
| tar -xz --strip-components=2 -C /usr/local/bin binaryen-version_121/bin/wasm-opt
32+
wasm-opt --version
3233
working-directory: compiler
3334

3435
- name: Build
@@ -39,18 +40,27 @@ jobs:
3940
run: cargo shear && cargo clippy --lib --target wasm32-unknown-unknown --features wasm
4041
working-directory: compiler
4142

42-
- name: Size
43+
- name: Size (unoptimized)
4344
run: ls -lh target/wasm32-unknown-unknown/release/compiler_lib.wasm
4445
working-directory: compiler
4546

4647
- name: Optimize
4748
run: |
48-
wasm-opt -Oz --converge --strip-debug --strip-producers \
49+
INPUT=target/wasm32-unknown-unknown/release/compiler_lib.wasm
50+
51+
wasm-opt -Oz --converge \
52+
--generate-global-effects \
53+
--strip-debug --strip-producers \
4954
--enable-bulk-memory \
5055
--enable-nontrapping-float-to-int \
5156
--enable-sign-ext \
52-
-o target/wasm32-unknown-unknown/release/compiler_lib.wasm \
53-
target/wasm32-unknown-unknown/release/compiler_lib.wasm
57+
-tnh \
58+
-o /tmp/wasm_stage1.wasm "$INPUT"
59+
60+
wasm-opt --flatten --rereloop -Oz -Oz \
61+
-o "$INPUT" /tmp/wasm_stage1.wasm
62+
63+
rm /tmp/wasm_stage1.wasm
5464
working-directory: compiler
5565

5666
- name: Size (optimized)

demo/index.html

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,6 @@
2424
<link rel="preconnect" href="https://cdn.tailwindcss.com" crossorigin>
2525
<link rel="dns-prefetch" href="https://cdn.tailwindcss.com">
2626
<link rel="modulepreload" href="./main.js">
27-
<link rel="preload" href="./compiler_lib.wasm" as="fetch" type="application/wasm" crossorigin>
28-
29-
<link rel="preconnect" href="https://cdn.tailwindcss.com" crossorigin>
30-
<link rel="dns-prefetch" href="https://cdn.tailwindcss.com">
31-
<link rel="modulepreload" href="./main.js">
32-
<link rel="preload" href="./compiler_lib.wasm" as="fetch" type="application/wasm" crossorigin>
3327

3428
<link rel="icon" type="image/svg+xml" href="./icon.svg">
3529

demo/main.js

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,17 @@ import { CodeJar } from 'https://esm.sh/codejar@4';
22

33
const SZ = 1 << 20;
44
const DEV = !['demo.edgepython.com'].includes(location.hostname);
5-
const WASM_SOURCES = DEV
6-
? ['https://demo.edgepython.com/compiler_lib.wasm']
7-
: ['./compiler_lib.wasm'];
85
const FETCH_OPTS = DEV ? { cache: 'no-store' } : undefined;
96

7+
const _ver = await fetch('./version.json', { cache: 'no-store' })
8+
.then(r => r.ok ? r.json() : {})
9+
.catch(() => ({}));
10+
const _bust = _ver.v ? `?v=${_ver.v}` : '';
11+
12+
const WASM_SOURCES = DEV
13+
? [`https://demo.edgepython.com/compiler_lib.wasm${_bust}`]
14+
: [`./compiler_lib.wasm${_bust}`];
15+
1016
const DEFAULT_CODE = `"""\nImplements a pure functional pipeline using head-tail recursion to avoid state mutation.\n\nReferences:\n* Backus, J. (1978). Algebra of Programs (Function composition).\n"""\n\ndef double(n: int) -> int:\n return n * 2\n\ndef square(n: int) -> int:\n return n * n\n\ndef apply_pipeline(value: int, steps: list[int]) -> int:\n # Stop recursion when no steps remain\n if not steps:\n return value\n\n first_fn = steps[0]\n remaining_steps = steps[1:]\n\n return apply_pipeline(first_fn(value), remaining_steps)\n\ndef transform_list(elements: list[int], steps: list[int]) -> list[int]:\n # Stop recursion when list is empty\n if not elements:\n return []\n\n head = elements[0]\n tail = elements[1:]\n\n transformed = apply_pipeline(head, steps)\n\n return [transformed] + transform_list(tail, steps)\n\ndata: list[int] = [1, 2, 3]\npipeline: list[int] = [double, square]\n\nresult = transform_list(data, pipeline)\n\nprint(f"Input: {data}")\nprint(f"Output: {result}")`;
1117

1218
const CLS = { ok: 'ml-auto text-[#7daf7a]', err: 'ml-auto text-[#d67f6d]' };

demo/version.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{ "v" : "" }

0 commit comments

Comments
 (0)