web: size-optimize the wasm profile and report the biggest contributors

The web build's wasm has been over the 24 MiB gate in packaging/web/package.sh
since the nightly Release dry run of 23 Nov. This change brings it down by about
4.2 MB; it does not bring it under the gate on its own.

- Cargo.toml: the wasm-release profile uses opt-level "z" instead of "s". The
  crates that rely on inlining (fearless_simd, vello_cpu, vello_common,
  craft-image-codecs, texturecraft-core) stay at "s", because they come out
  larger at "z". The per-pixel crates stay at opt-level 3.
- packaging/web/wasm_size_report.py: lists the wasm's sections and its largest
  crates and functions.
- packaging/web/package.sh: when the size gate fails, it prints that report.
This commit is contained in:
FredApps committed 2026-11-28 08:23:58 +01:00
1 parent 7f16522d87
commit c1a395f3fa
3 files changed
+108 -1

No files matched your search

+15 -1
View File
@@ -138,12 +138,26 @@ codegen-units = 16
[profile.wasm-release]
inherits = "release"
opt-level = "s"
# "z" over "s": the browser build must stay under the 24 MiB gate in packaging/web/package.sh.
# Measured on main 7c8dd1ae: 30,812,862 bytes with "s", 26,654,524 bytes with the levels below.
opt-level = "z"
lto = "fat"
codegen-units = 1
strip = "debuginfo"
[profile.wasm-release.package."*"]
opt-level = "z"
# Kept at "s": these rely on inlining, and at "z" they come out larger, not smaller.
[profile.wasm-release.package.fearless_simd]
opt-level = "s"
[profile.wasm-release.package.vello_cpu]
opt-level = "s"
[profile.wasm-release.package.vello_common]
opt-level = "s"
[profile.wasm-release.package.craft-image-codecs]
opt-level = "s"
[profile.wasm-release.package.texturecraft-core]
opt-level = "s"
[profile.wasm-release.package.texturecraft-texel]
+12
View File
@@ -36,6 +36,18 @@ for wasm in "$SITE"/*.wasm; do
echo "$(basename "$wasm"): $size bytes ($((size / 1048576)) MiB; limit $MAX_WASM_BYTES)"
if [[ "$size" -gt "$MAX_WASM_BYTES" ]]; then
echo "error: $(basename "$wasm") is $size bytes, over the $MAX_WASM_BYTES-byte limit (Cloudflare's per-file cap is 25 MiB)" >&2
# Where the bytes go: the optimized file has no function names, so the crate breakdown reads
# cargo's own output (before wasm-bindgen and wasm-opt), which keeps them. Best effort only.
report="$HERE/wasm_size_report.py"
if command -v python3 >/dev/null && [[ -f "$report" ]]; then
echo "--- sections of $(basename "$wasm") ---" >&2
python3 "$report" "$wasm" >&2 || true
raw="$CARGO_TARGET_DIR/wasm32-unknown-unknown/wasm-release/texturecraft-web.wasm"
if [[ -f "$raw" ]]; then
echo "--- largest crates and functions in cargo's output (before wasm-opt) ---" >&2
python3 "$report" "$raw" >&2 || true
fi
fi
exit 1
fi
done
+81
View File
@@ -0,0 +1,81 @@
#!/usr/bin/env python3
"""Print where a wasm module's bytes go: sections, data, and code by crate."""
import re, sys
from collections import defaultdict
def leb(b, i):
r = s = 0
while True:
x = b[i]; i += 1
r |= (x & 0x7f) << s; s += 7
if x < 0x80: return r, i
def crate_of(name):
if name.startswith("_ZN"):
m = re.match(r"_ZN(\d+)", name)
if m:
n = int(m.group(1)); s = name[3 + len(m.group(1)):3 + len(m.group(1)) + n]
if s.startswith("_$LT$"):
inner = re.match(r"_\$LT\$(?:\$RF\$|mut\$u20\$)*([A-Za-z_][A-Za-z0-9_]*)", s)
return (inner.group(1) if inner else s) + " (impl)"
return s
if name.startswith("_R"):
m = re.search(r"C(?:s[0-9A-Za-z]*_)?(\d+)_?([A-Za-z_][A-Za-z0-9_]*)", name)
if m: return m.group(2)[:int(m.group(1))]
return "<other: " + name.split("::")[0][:40] + ">" if "::" in name else "<unmangled>"
def main(path, top=25):
b = open(path, "rb").read()
assert b[:4] == b"\0asm"
i = 8; secs = []; names = {}; code_sizes = []; nimports = 0; data_total = 0
while i < len(b):
sid = b[i]; i += 1
size, i = leb(b, i); body = b[i:i + size]
label = {0:"custom",1:"type",2:"import",3:"function",4:"table",5:"memory",6:"global",7:"export",8:"start",9:"element",10:"code",11:"data",12:"datacount"}.get(sid, str(sid))
if sid == 0:
nl, j = leb(body, 0); cname = body[j:j+nl].decode(); label = "custom:" + cname
if cname == "name":
j += nl
while j < len(body):
sub = body[j]; j += 1; sl, j = leb(body, j); end = j + sl
if sub == 1:
cnt, k = leb(body, j)
for _ in range(cnt):
idx, k = leb(body, k); ln, k = leb(body, k); names[idx] = body[k:k+ln].decode("utf-8", "replace"); k += ln
j = end
elif sid == 2:
cnt, j = leb(body, 0)
for _ in range(cnt):
l, j = leb(body, j); j += l; l, j = leb(body, j); j += l
kind = body[j]; j += 1
if kind == 0: nimports += 1; _, j = leb(body, j)
elif kind == 1: j += 1; fl, j = leb(body, j); _, j = leb(body, j); j = leb(body, j)[1] if fl & 1 else j
elif kind == 2: fl, j = leb(body, j); _, j = leb(body, j); j = leb(body, j)[1] if fl & 1 else j
elif kind == 3: j += 2
elif sid == 10:
cnt, j = leb(body, 0)
for _ in range(cnt):
fs, j2 = leb(body, j); code_sizes.append(fs + (j2 - j)); j = j2 + fs
elif sid == 11:
data_total = size
secs.append((label, size)); i += size
total = len(b)
print(f"{path}: {total} bytes")
for label, size in sorted(secs, key=lambda s: -s[1])[:8]:
print(f" {label:<24}{size:>12} bytes {100*size/total:5.1f}%")
by_crate = defaultdict(int); funcs = []
for k, sz in enumerate(code_sizes):
n = names.get(nimports + k, f"func[{nimports+k}]")
by_crate[crate_of(n)] += sz; funcs.append((sz, n))
if not names:
print(" (no name section: pass the wasm cargo wrote, before wasm-opt, to attribute code to crates)")
return
print(f" largest crates by code size (of {sum(code_sizes)} code bytes):")
for c, sz in sorted(by_crate.items(), key=lambda x: -x[1])[:top]:
print(f" {sz:>10} {c}")
print(" largest functions:")
for sz, n in sorted(funcs, reverse=True)[:10]:
print(f" {sz:>10} {n[:140]}")
if __name__ == "__main__":
main(sys.argv[1], int(sys.argv[2]) if len(sys.argv) > 2 else 25)