#!/bin/bash
# bench-hashset.sh — run the asm native hash-set vs portable benchmark
# and echo the speedup. ASM-only: C/Python have no hash-set builtin.
set +e
cd "$(dirname "$0")/.."

ulimit -v 524288
trap 'pkill -9 -u "$USER" -f "asm/lumbda" 2>/dev/null || true' EXIT

# Build if needed
if [ ! -x asm/lumbda ] || [ asm/lumbda.s -nt asm/lumbda ]; then
    make -s asm-build
fi

echo "── asm no-GC ──"
timeout 60 ./asm/lumbda < examples/bench-hashset.lsp
echo ""
echo "── asm GC (GC_NAIVE build) ──"
# Under GC_NAIVE the heap is 1 MB per chunk; build-list(K=500) runs
# into the bump threshold and triggers implicit GC. That's the
# intended comparison — same primitives, different allocator.
timeout 60 ./asm/lumbda-gc < examples/bench-hashset.lsp

# Verify cleanup. pgrep with -x matches the exact command basename
# so it doesn't false-positive on the parent shell.
if pgrep -u "$USER" -x lumbda > /dev/null || \
   pgrep -u "$USER" -x lumbda-gc > /dev/null; then
    echo "STRAGGLER asm binary detected" >&2
    exit 1
fi
