perf(build): 4h05m -> 1h15m full CI build — docker caps, pipelined wasm-opt, Binaryen 130

Three orchestration fixes, validated end-to-end on the Hetzner ccx53
(run 27280051992, 1h14m41s vs 4h05m baseline, e2e identical):

- docker-compose CPU/memory caps were hardcoded to dev-Mac defaults
  (10 CPUs / 32G); now env-tunable via KICAD_DOCKER_CPUS/KICAD_DOCKER_MEM
  (CI exports nproc/110G — the 32-core runner was compiling on 10 cores).
- docker/build.sh: split build_app into compile_app (container) +
  postprocess_app (host-side dyncall shims + finalize + asyncify + -O2)
  and added KICAD_PIPELINE=1 mode that overlaps each tool's host-side
  wasm-opt with the next tool's container compile (max 2 concurrent
  postprocesses — pcbnew -O2 peaks ~34G RSS). Also: comma-separated app
  lists for cheap pipeline repros.
- get-wasm-opt.sh: Binaryen default 121 -> 130 (fixes the v121 -O2 lock
  convoy, ~9x) and BINARYEN_BUILD_FROM_SOURCE=1 support: the official
  x86_64-linux release tarballs (Alpine/musl, no LTO, assertions on) run
  asyncify 4x slower than a stock gcc -O3+LTO build with sha256-identical
  output; self-build takes ~5 min and is instantly repaid.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
Istvan Matejcsok 2026-06-11 07:46:48 +02:00
commit 680eb9dc15
6 changed files with 289 additions and 33 deletions

View file

@ -14,6 +14,61 @@ PROJECT_ROOT="$(cd "${SCRIPT_DIR}/../.." && pwd)"
# Get wasm-opt path
WASM_OPT=$("${SCRIPT_DIR}/get-wasm-opt.sh")
# Bound Binaryen's host thread pool. wasm-opt runs function-parallel passes, and
# each worker holds the optimization working-set of one function at a time — so
# peak RAM scales with thread count. Binaryen reads BINARYEN_CORES to size the
# pool; default to 8 for memory-constrained dev machines, overridable via the
# environment (CI sets it to $(nproc) on the 128 GB Hetzner runner).
export BINARYEN_CORES="${BINARYEN_CORES:-8}"
# Preload a scalable allocator on Linux. wasm-opt churns a ~40 GB high-water mark
# of short-lived allocations across all worker threads; glibc malloc serializes
# concurrent alloc/free on per-arena locks, so under many threads ~half of every
# core's cycles collapse into futex lock-spin (strace: ~99% kernel time in futex)
# instead of optimization work — the more cores, the worse it gets. jemalloc and
# mimalloc are built for exactly this many-thread churn and eliminate the storm,
# roughly halving wall-clock. macOS already ships a scalable allocator
# (libmalloc/nano-zone), so only Linux needs this. Honor an externally-set
# WASM_OPT_PRELOAD; otherwise auto-detect a system jemalloc/mimalloc.
#
# WASM_OPT_PRELOAD=none (or 0) forces NO preload — a clean glibc baseline for
# benchmarking the allocator A/B (see scripts/bench/).
if [[ "${WASM_OPT_PRELOAD:-}" == "none" || "${WASM_OPT_PRELOAD:-}" == "0" ]]; then
WASM_OPT_PRELOAD=""
_PRELOAD_FORCED_OFF=1
fi
if [[ -z "${WASM_OPT_PRELOAD:-}" && -z "${_PRELOAD_FORCED_OFF:-}" && "$(uname -s)" == "Linux" ]]; then
for _alloc in \
"/usr/lib/$(uname -m)-linux-gnu/libjemalloc.so.2" \
"/usr/lib/$(uname -m)-linux-gnu/libmimalloc.so.2" \
/usr/lib/libjemalloc.so.2 \
/usr/lib/libmimalloc.so.2; do
if [[ -e "${_alloc}" ]]; then
WASM_OPT_PRELOAD="${_alloc}"
break
fi
done
fi
# Build the command prefix that injects the allocator (preserving any existing
# LD_PRELOAD). Empty when no scalable allocator was found — wasm-opt then runs
# under the default allocator, just slower.
if [[ -n "${WASM_OPT_PRELOAD:-}" ]]; then
PRELOAD_CMD=(env "LD_PRELOAD=${WASM_OPT_PRELOAD}${LD_PRELOAD:+:${LD_PRELOAD}}")
else
PRELOAD_CMD=()
fi
# Wrap wasm-opt in GNU `time -v` when available (Linux CI) so the log records
# peak RSS + wall-clock for each pass. macOS `time` lacks -v, so fall back to
# running wasm-opt directly there.
if /usr/bin/time -v true >/dev/null 2>&1; then
TIME_CMD=(/usr/bin/time -v)
else
TIME_CMD=()
fi
INPUT_WASM="${1:-output/pcbnew.wasm}"
OUTPUT_WASM="${2:-${INPUT_WASM}}"
@ -54,13 +109,25 @@ ASYNCIFY_REMOVE_ARG=$(echo "${ASYNCIFY_REMOVE}" | tr '\n' ',' | sed 's/,$//')
echo ""
echo "Running wasm-opt --asyncify..."
echo "This may take several minutes and use significant RAM..."
echo " BINARYEN_CORES=${BINARYEN_CORES}"
echo " LD_PRELOAD=${WASM_OPT_PRELOAD:-<none>}"
"${WASM_OPT}" --asyncify \
"${PRELOAD_CMD[@]}" "${TIME_CMD[@]}" "${WASM_OPT}" --asyncify \
"--pass-arg=asyncify-imports@${ASYNCIFY_IMPORTS}" \
"--pass-arg=asyncify-removelist@${ASYNCIFY_REMOVE_ARG}" \
--pass-arg=asyncify-propagate-addlist \
"${INPUT_WASM}" -o "${OUTPUT_WASM}"
# ASYNCIFY_ONLY=1 stops after the asyncify pass (skips -O2). Used by the
# benchmark harness (scripts/bench/) to time/compare just the asyncify pass,
# whose RAM fits where the -O2 pass on the bloated module would not.
if [[ "${ASYNCIFY_ONLY:-0}" == "1" ]]; then
echo ""
echo "ASYNCIFY_ONLY=1 → skipping -O2 pass (benchmark mode)."
ls -lh "${OUTPUT_WASM}"
exit 0
fi
echo ""
echo "Running wasm-opt -O2 on the asyncified wasm..."
echo " Purpose: shrink asyncify-instrumented functions back under V8's"
@ -68,8 +135,10 @@ echo " per-function locals limit (otherwise large coroutine-entry and"
echo " similar functions silently stall in Chrome's V8). See docs/debugging/DEBUG.md §7"
echo " and memory/bundle-size-asyncify-optimization.md."
echo " This pass also takes several minutes and ~10-15 GB RAM."
echo " BINARYEN_CORES=${BINARYEN_CORES}"
echo " LD_PRELOAD=${WASM_OPT_PRELOAD:-<none>}"
"${WASM_OPT}" -O2 "${OUTPUT_WASM}" -o "${OUTPUT_WASM}"
"${PRELOAD_CMD[@]}" "${TIME_CMD[@]}" "${WASM_OPT}" -O2 "${OUTPUT_WASM}" -o "${OUTPUT_WASM}"
echo ""
echo "Asyncify + -O2 complete: ${OUTPUT_WASM}"

View file

@ -100,12 +100,37 @@ download_file() {
log_info "Downloading $(basename "$dest")..."
mkdir -p "$(dirname "$dest")"
if ! curl -L -o "$dest" "$url"; then
log_error "Failed to download $url"
# -f: fail (non-zero exit) on HTTP >= 400 instead of silently saving the error
# page as the file — otherwise a transient GitHub 504 gets written as the
# "tarball" and only blows up later at `tar`/`unzip` ("not in gzip format").
# --retry-all-errors + --retry: ride out transient 5xx from release CDNs
# (GitHub release assets intermittently 504) within a single call.
if ! curl -fL --retry 5 --retry-all-errors --retry-delay 5 \
--connect-timeout 30 -o "$dest" "$url"; then
log_error "Failed to download $url after retries"
rm -f "$dest"
return 1
fi
# Defense in depth: validate archive integrity so a bad download fails here
# with a clear message rather than deep in a later build step.
case "$dest" in
*.tar.gz|*.tgz)
if ! gzip -t "$dest" 2>/dev/null; then
log_error "Downloaded file is not a valid gzip archive: $dest"
rm -f "$dest"
return 1
fi
;;
*.zip)
if command -v unzip >/dev/null 2>&1 && ! unzip -tqq "$dest" >/dev/null 2>&1; then
log_error "Downloaded file is not a valid zip archive: $dest"
rm -f "$dest"
return 1
fi
;;
esac
if [ -n "$expected_sha256" ]; then
local actual_sha256
actual_sha256=$(shasum -a 256 "$dest" | cut -d' ' -f1)

View file

@ -10,7 +10,7 @@
# (e.g. v121+72 dev) and a standalone release (v121) corrupts asyncify
# metadata, causing "func is not a function" errors at runtime.
#
# Falls back to downloading standalone Binaryen v121 if emsdk is not installed
# Falls back to downloading standalone Binaryen v130 if emsdk is not installed
# locally (e.g. CI environments that only use Docker).
set -e
@ -19,8 +19,13 @@ SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
PROJECT_ROOT="$(cd "${SCRIPT_DIR}/../.." && pwd)"
# --- Prefer emsdk-bundled Binaryen (matches the Emscripten that compiled the WASM) ---
# Override: set BINARYEN_VERSION in the env to FORCE a specific standalone release
# (skips the emsdk preference). Used to A/B Binaryen versions — e.g. v121 carries a
# wasm::Type lock-contention bug that makes the host-side -O2 pass ~9x slower than
# v130 (see docs/ci-build-slowness-findings.md). Validate any bump with the e2e
# suite: a Binaryen/emsdk skew can corrupt asyncify metadata ("func is not a function").
EMSDK_WASM_OPT="${PROJECT_ROOT}/tools/emsdk/upstream/bin/wasm-opt"
if [ -x "${EMSDK_WASM_OPT}" ]; then
if [ -z "${BINARYEN_VERSION:-}" ] && [ -x "${EMSDK_WASM_OPT}" ]; then
EMSDK_VERSION=$("${EMSDK_WASM_OPT}" --version 2>&1 || true)
echo "Using emsdk-bundled Binaryen: ${EMSDK_VERSION}" >&2
echo "${EMSDK_WASM_OPT}"
@ -30,7 +35,50 @@ fi
# --- Fallback: download standalone Binaryen (for CI or environments without local emsdk) ---
echo "emsdk Binaryen not found at ${EMSDK_WASM_OPT}, falling back to standalone download..." >&2
BINARYEN_VERSION="121"
# Default v130: v121 has a wasm::Type lock convoy that makes -O2 ~9x slower on
# many-core Linux (docs/ci-build-slowness-findings.md). v130 output validated by
# the full e2e suite locally (31/31) and Chromium-green on CI run 27226030304.
BINARYEN_VERSION="${BINARYEN_VERSION:-130}"
# BINARYEN_BUILD_FROM_SOURCE=1: compile wasm-opt ourselves instead of using the
# official Linux release tarballs, which are badly built — measured on the
# calculator fixture, identical output sha256: asyncify 4x faster on x86
# (CI run 27276830256: 3:50 -> 0:58) and 13x on aarch64; -O2 equal. The macOS
# tarball is well-built (self-build is ~12% SLOWER there), so this is only
# worth enabling on Linux CI. Needs cmake, ninja, g++, git. ~5 min on 32 cores,
# cached in build-wasm/tools after the first call.
if [[ "${BINARYEN_BUILD_FROM_SOURCE:-0}" == "1" ]]; then
SELF_DIR="${PROJECT_ROOT}/build-wasm/tools/binaryen-${BINARYEN_VERSION}-selfbuilt"
SELF_WASM_OPT="${SELF_DIR}/bin/wasm-opt"
if [ ! -x "${SELF_WASM_OPT}" ]; then
SRC_DIR="${PROJECT_ROOT}/build-wasm/tools/binaryen-src-${BINARYEN_VERSION}"
BUILD_DIR="${PROJECT_ROOT}/build-wasm/tools/binaryen-build-${BINARYEN_VERSION}"
echo "Building Binaryen v${BINARYEN_VERSION} from source (one-time, ~5 min)..." >&2
if [ ! -d "${SRC_DIR}" ]; then
git clone -q --depth 1 --branch "version_${BINARYEN_VERSION}" \
--recurse-submodules --shallow-submodules \
https://github.com/WebAssembly/binaryen.git "${SRC_DIR}" >&2
fi
cmake -S "${SRC_DIR}" -B "${BUILD_DIR}" -G Ninja \
-DCMAKE_BUILD_TYPE=Release \
-DCMAKE_CXX_FLAGS="-Wno-maybe-uninitialized" \
-DCMAKE_INTERPROCEDURAL_OPTIMIZATION=ON -DBUILD_TESTS=OFF >&2
ninja -C "${BUILD_DIR}" wasm-opt wasm-emscripten-finalize >&2
# wasm-opt links lib/libbinaryen.so via rpath $ORIGIN/../lib.
mkdir -p "${SELF_DIR}/bin" "${SELF_DIR}/lib"
cp "${BUILD_DIR}/bin/wasm-opt" "${BUILD_DIR}/bin/wasm-emscripten-finalize" "${SELF_DIR}/bin/"
cp "${BUILD_DIR}"/lib/libbinaryen.* "${SELF_DIR}/lib/" 2>/dev/null || true
echo "Self-built Binaryen installed to ${SELF_DIR}" >&2
fi
SELF_VERSION=$("${SELF_WASM_OPT}" --version 2>&1 | grep -o '[0-9]\+' | head -1)
if [ "${SELF_VERSION}" != "${BINARYEN_VERSION}" ]; then
echo "ERROR: self-built wasm-opt version mismatch (got ${SELF_VERSION}, expected ${BINARYEN_VERSION})" >&2
exit 1
fi
echo "Using self-built Binaryen v${BINARYEN_VERSION} (${SELF_WASM_OPT})" >&2
echo "${SELF_WASM_OPT}"
exit 0
fi
BINARYEN_DIR="${PROJECT_ROOT}/build-wasm/tools/binaryen-${BINARYEN_VERSION}"
WASM_OPT="${BINARYEN_DIR}/bin/wasm-opt"

View file

@ -105,7 +105,8 @@ TOTAL_FIXED=0
apply_fix() { # <grep/sed pattern> <sed replacement> <label>
local before; before=$(grep -c "$1" "$JS_FILE" || true)
if [ "$before" -gt 0 ]; then
sed -i '' "s/$1/$2/g" "$JS_FILE"
# Portable in-place edit (BSD `sed -i ''` and GNU `sed -i` differ; temp+mv works on both).
sed "s/$1/$2/g" "$JS_FILE" > "${JS_FILE}.sedtmp" && mv "${JS_FILE}.sedtmp" "$JS_FILE"
local after; after=$(grep -c "$1" "$JS_FILE" || true)
echo " Fixed $((before - after)) $3"
TOTAL_FIXED=$((TOTAL_FIXED + before - after))