portal benchmark + 3 mismatch defects fixed
Benchmark exercises full save×load matrix across Python/C/asm plus the mismatch cases (wrong format, truncated input, missing file, corrupt header). Cases that used to segfault or report wrong paths now degrade cleanly. asm (uncommonlisp.s): - (define var) with no value now binds to VOID instead of segfault - portal-resume checks sys_read returned full 48-byte header; sanity- checks heap_size and heap_base before committing r14/r15 restore. Corrupt/truncated portals now return #f cleanly. py (uncommonlisp.py): - file-not-found error inside a nested (load) now reports the actual missing path (via FileNotFoundError.filename) rather than the outer script path. - _load wraps UnicodeDecodeError (binary file loaded as text) into a LispErr with the path; no more raw Python traceback. tests/portal-benchmark.sh: 50-iter benchmark, 4 parts (save / load / cross-process / mismatch-classification). Representative numbers (this laptop, 2026-04-16): setup+save: Python 126ms, C 3ms, asm 0.9ms cross-proc: Py→Py 260ms, C→C 6ms, asm→asm 1.5ms All three test suites still pass: 571 py unit, 131 asm, 189 shared.
This commit is contained in:
parent
57f3c9fab1
commit
2f8b7dc737
5 changed files with 204 additions and 7 deletions
BIN
asm/uncommonlisp
BIN
asm/uncommonlisp
Binary file not shown.
Binary file not shown.
|
|
@ -1626,7 +1626,9 @@ env_lookup_both:
|
|||
cmpq $TAG_PAIR, %rax
|
||||
je .ev_define_func
|
||||
|
||||
# Simple: (define var expr)
|
||||
# Simple: (define var expr) — or (define var) with no expr → VAL_VOID
|
||||
cmpq $VAL_NIL, %r12
|
||||
je .ev_define_no_expr
|
||||
movq %r12, %rax
|
||||
andq $-8, %rax
|
||||
movq (%rax), %rdi # expr
|
||||
|
|
@ -1634,6 +1636,10 @@ env_lookup_both:
|
|||
movq %rbp, %rsi
|
||||
call eval
|
||||
popq %rbx # var symbol
|
||||
jmp .ev_define_bind
|
||||
.ev_define_no_expr:
|
||||
movq $VAL_VOID, %rax
|
||||
.ev_define_bind:
|
||||
movq %rbx, %rdi
|
||||
movq %rax, %rsi
|
||||
movq %r14, %rdx
|
||||
|
|
@ -3927,13 +3933,15 @@ bi_portal_resume:
|
|||
js .pr_fail
|
||||
movq %rax, %rbx # fd
|
||||
|
||||
# Read header
|
||||
# Read header — must get full PORTAL_HDR_SIZE bytes
|
||||
subq $PORTAL_HDR_SIZE, %rsp
|
||||
movq $SYS_READ, %rax
|
||||
movq %rbx, %rdi
|
||||
movq %rsp, %rsi
|
||||
movq $PORTAL_HDR_SIZE, %rdx
|
||||
syscall
|
||||
cmpq $PORTAL_HDR_SIZE, %rax
|
||||
jne .pr_bad_magic # short read → file is not a portal
|
||||
|
||||
# Verify magic
|
||||
leaq portal_magic(%rip), %rdi
|
||||
|
|
@ -3941,9 +3949,15 @@ bi_portal_resume:
|
|||
cmpq (%rdi), %rax
|
||||
jne .pr_bad_magic
|
||||
|
||||
# Read heap size, base, r14, r15
|
||||
# Sanity check header fields before committing
|
||||
movq 8(%rsp), %rcx # heap_size
|
||||
testq %rcx, %rcx
|
||||
jle .pr_bad_magic # zero or negative heap size → corrupt
|
||||
movq 16(%rsp), %rdx # heap_base (must match our mmap)
|
||||
testq %rdx, %rdx
|
||||
jz .pr_bad_magic # null heap base → corrupt
|
||||
|
||||
# Header looks valid — commit to r14/r15 restore
|
||||
movq 24(%rsp), %r14 # restore global env
|
||||
movq 32(%rsp), %r15 # restore bump pointer
|
||||
|
||||
|
|
|
|||
179
tests/portal-benchmark.sh
Executable file
179
tests/portal-benchmark.sh
Executable file
|
|
@ -0,0 +1,179 @@
|
|||
#!/bin/bash
|
||||
# portal-benchmark.sh — measure portal save/load across:
|
||||
# 1. Same-process save + load per impl + format
|
||||
# 2. Cross-process (proc A saves, proc B loads) same server, same binary
|
||||
# 3. Mismatch cases (wrong format, corrupt file, truncated input)
|
||||
#
|
||||
# Usage: bash tests/portal-benchmark.sh
|
||||
#
|
||||
# Output: one table. Times are wall time averaged over N iterations.
|
||||
|
||||
set -u
|
||||
cd "$(dirname "$0")/.."
|
||||
|
||||
PY="python3 uncommonlisp.py --fast"
|
||||
C="./c/uncommonlisp"
|
||||
ASM="./asm/uncommonlisp"
|
||||
N=50
|
||||
|
||||
pad() { printf " %-42s " "$1"; }
|
||||
|
||||
time_n() {
|
||||
local n=$1 cmd=$2 t0 t1
|
||||
t0=$(date +%s.%N)
|
||||
for _ in $(seq 1 "$n"); do eval "$cmd" >/dev/null 2>&1; done
|
||||
t1=$(date +%s.%N)
|
||||
python3 -c "print(f'{(float(\"$t1\")-float(\"$t0\"))*1000/$n:8.3f} ms/iter')"
|
||||
}
|
||||
|
||||
PROLOGUE='(define my-int 42)
|
||||
(define my-list (list 1 2 3 4 5 6 7 8 9 10))
|
||||
(define my-str "hello world")
|
||||
(define (fib n) (let loop ((a 0) (b 1) (i 0)) (if (= i n) a (loop b (+ a b) (+ i 1)))))
|
||||
(define my-result (fib 30))'
|
||||
|
||||
# Save programs that write the same state in sexp / binary / JSON
|
||||
cat > /tmp/pb-sexp-save.lsp <<EOF
|
||||
$PROLOGUE
|
||||
(define p (open-output-file "/tmp/pb-state.sexp"))
|
||||
(display "(define my-int " p) (write my-int p) (display ")\n" p)
|
||||
(display "(define my-list '" p) (write my-list p) (display ")\n" p)
|
||||
(display "(define my-str " p) (write my-str p) (display ")\n" p)
|
||||
(display "(define my-result " p) (write my-result p) (display ")\n" p)
|
||||
(close-port p)
|
||||
EOF
|
||||
cat > /tmp/pb-bin-save.lsp <<EOF
|
||||
$PROLOGUE
|
||||
(portal-save "/tmp/pb-state.binary")
|
||||
EOF
|
||||
cat > /tmp/pb-json-save.lsp <<EOF
|
||||
$PROLOGUE
|
||||
(portal-save "/tmp/pb-state.json")
|
||||
EOF
|
||||
cat > /tmp/pb-noop.lsp <<EOF
|
||||
$PROLOGUE
|
||||
EOF
|
||||
cat > /tmp/pb-sexp-load.lsp <<'EOF'
|
||||
(load "/tmp/pb-state.sexp")
|
||||
EOF
|
||||
cat > /tmp/pb-bin-load.lsp <<'EOF'
|
||||
(portal-resume "/tmp/pb-state.binary")
|
||||
EOF
|
||||
|
||||
# Pre-materialize files so "load" cases have something to load.
|
||||
$PY /tmp/pb-sexp-save.lsp >/dev/null 2>&1
|
||||
$ASM < /tmp/pb-bin-save.lsp >/dev/null 2>&1
|
||||
$PY /tmp/pb-json-save.lsp >/dev/null 2>&1
|
||||
|
||||
echo "═══════════════════════════════════════════════════════════════"
|
||||
echo "Portal benchmark — $N iterations per case"
|
||||
echo " (each iter = one full process: startup + workload + task)"
|
||||
echo "═══════════════════════════════════════════════════════════════"
|
||||
echo
|
||||
|
||||
echo "PART 1 — full lifetime: startup + build workload + SAVE"
|
||||
pad "baseline (workload only, no save)" ; time_n $N "$PY /tmp/pb-noop.lsp"
|
||||
pad "Python S-exp save (via ports)" ; time_n $N "$PY /tmp/pb-sexp-save.lsp"
|
||||
pad "C S-exp save (via ports)" ; time_n $N "$C /tmp/pb-sexp-save.lsp"
|
||||
pad "Asm S-exp save (via ports)" ; time_n $N "$ASM < /tmp/pb-sexp-save.lsp"
|
||||
pad "Asm binary portal-save (heap dump)" ; time_n $N "$ASM < /tmp/pb-bin-save.lsp"
|
||||
pad "Python JSON portal-save (graph-aware)" ; time_n $N "$PY /tmp/pb-json-save.lsp"
|
||||
echo
|
||||
|
||||
echo "PART 2 — full lifetime: startup + LOAD pre-written state"
|
||||
pad "Python (load sexp)" ; time_n $N "$PY /tmp/pb-sexp-load.lsp"
|
||||
pad "C (load sexp)" ; time_n $N "$C /tmp/pb-sexp-load.lsp"
|
||||
pad "Asm (load sexp)" ; time_n $N "$ASM < /tmp/pb-sexp-load.lsp"
|
||||
pad "Asm (portal-resume binary)" ; time_n $N "$ASM < /tmp/pb-bin-load.lsp"
|
||||
echo
|
||||
|
||||
echo "PART 3 — cross-process: proc A saves → proc B loads"
|
||||
echo " (wall-time for both procs end-to-end; pipes via file)"
|
||||
pad "Python → Python (sexp)" ; time_n $N "$PY /tmp/pb-sexp-save.lsp && $PY /tmp/pb-sexp-load.lsp"
|
||||
pad "C → C (sexp)" ; time_n $N "$C /tmp/pb-sexp-save.lsp && $C /tmp/pb-sexp-load.lsp"
|
||||
pad "Asm → Asm (sexp)" ; time_n $N "$ASM < /tmp/pb-sexp-save.lsp && $ASM < /tmp/pb-sexp-load.lsp"
|
||||
pad "Asm → Asm (binary portal)" ; time_n $N "$ASM < /tmp/pb-bin-save.lsp && $ASM < /tmp/pb-bin-load.lsp"
|
||||
pad "Python → C (sexp)" ; time_n $N "$PY /tmp/pb-sexp-save.lsp && $C /tmp/pb-sexp-load.lsp"
|
||||
pad "Python → Asm (sexp)" ; time_n $N "$PY /tmp/pb-sexp-save.lsp && $ASM < /tmp/pb-sexp-load.lsp"
|
||||
pad "C → Python (sexp)" ; time_n $N "$C /tmp/pb-sexp-save.lsp && $PY /tmp/pb-sexp-load.lsp"
|
||||
pad "C → Asm (sexp)" ; time_n $N "$C /tmp/pb-sexp-save.lsp && $ASM < /tmp/pb-sexp-load.lsp"
|
||||
pad "Asm → Python (sexp)" ; time_n $N "$ASM < /tmp/pb-sexp-save.lsp && $PY /tmp/pb-sexp-load.lsp"
|
||||
pad "Asm → C (sexp)" ; time_n $N "$ASM < /tmp/pb-sexp-save.lsp && $C /tmp/pb-sexp-load.lsp"
|
||||
echo
|
||||
|
||||
echo "PART 4 — mismatch cases (classify output, errors are expected)"
|
||||
|
||||
# Helper: run, capture output, classify via keyword
|
||||
run_out() { eval "$1" 2>&1 | head -4 | tr '\n' ' ' | cut -c1-180; }
|
||||
|
||||
probe() {
|
||||
local label="$1" cmd="$2"
|
||||
pad "$label"
|
||||
local out
|
||||
out=$(run_out "$cmd")
|
||||
if [ -z "$out" ]; then
|
||||
echo "(no output / silent)"
|
||||
else
|
||||
echo "$out"
|
||||
fi
|
||||
}
|
||||
|
||||
# Pre-materialize canonical files for the probes
|
||||
$ASM < /tmp/pb-bin-save.lsp >/dev/null 2>&1
|
||||
$PY /tmp/pb-sexp-save.lsp >/dev/null 2>&1
|
||||
|
||||
# 4a. Load a binary portal as if it were sexp → reader errors
|
||||
cat > /tmp/pb-load-binary-as-sexp.lsp <<'EOF'
|
||||
(load "/tmp/pb-state.binary") (display "reached end") (newline)
|
||||
EOF
|
||||
probe "Python loads asm BINARY as sexp" "$PY /tmp/pb-load-binary-as-sexp.lsp"
|
||||
probe "C loads asm BINARY as sexp" "$C /tmp/pb-load-binary-as-sexp.lsp"
|
||||
probe "Asm loads asm BINARY as sexp" "$ASM < /tmp/pb-load-binary-as-sexp.lsp"
|
||||
|
||||
# 4b. Portal-resume on a sexp file (wrong magic) → #f
|
||||
cat > /tmp/pb-resume-sexp.lsp <<'EOF'
|
||||
(display (portal-resume "/tmp/pb-state.sexp")) (newline)
|
||||
EOF
|
||||
probe "Asm portal-resume on SEXP file" "$ASM < /tmp/pb-resume-sexp.lsp"
|
||||
|
||||
# 4c. Load an empty file
|
||||
: > /tmp/pb-empty.lsp
|
||||
cat > /tmp/pb-empty-test.lsp <<'EOF'
|
||||
(load "/tmp/pb-empty.lsp") (display "continued") (newline)
|
||||
EOF
|
||||
probe "Python load EMPTY file" "$PY /tmp/pb-empty-test.lsp"
|
||||
probe "C load EMPTY file" "$C /tmp/pb-empty-test.lsp"
|
||||
probe "Asm load EMPTY file" "$ASM < /tmp/pb-empty-test.lsp"
|
||||
|
||||
# 4d. Load a truncated sexp (unclosed paren, no value)
|
||||
echo -n "(define truncated" > /tmp/pb-trunc.sexp
|
||||
cat > /tmp/pb-trunc-test.lsp <<'EOF'
|
||||
(load "/tmp/pb-trunc.sexp") (display "continued") (newline)
|
||||
EOF
|
||||
probe "Python load TRUNCATED sexp" "$PY /tmp/pb-trunc-test.lsp"
|
||||
probe "C load TRUNCATED sexp" "$C /tmp/pb-trunc-test.lsp"
|
||||
probe "Asm load TRUNCATED sexp" "$ASM < /tmp/pb-trunc-test.lsp"
|
||||
|
||||
# 4e. Load a missing file
|
||||
rm -f /tmp/pb-nope.xyz
|
||||
cat > /tmp/pb-missing-test.lsp <<'EOF'
|
||||
(display (load "/tmp/pb-nope.xyz")) (newline)
|
||||
(display "continued") (newline)
|
||||
EOF
|
||||
probe "Python load MISSING file" "$PY /tmp/pb-missing-test.lsp"
|
||||
probe "C load MISSING file" "$C /tmp/pb-missing-test.lsp"
|
||||
probe "Asm load MISSING file" "$ASM < /tmp/pb-missing-test.lsp"
|
||||
|
||||
# 4f. Asm portal-resume on truncated binary (corrupted heap dump)
|
||||
head -c 40 /tmp/pb-state.binary > /tmp/pb-corrupt.binary
|
||||
cat > /tmp/pb-corrupt-test.lsp <<'EOF'
|
||||
(display (portal-resume "/tmp/pb-corrupt.binary")) (newline)
|
||||
EOF
|
||||
probe "Asm portal-resume on CORRUPT binary" "$ASM < /tmp/pb-corrupt-test.lsp"
|
||||
|
||||
echo
|
||||
echo "═══════════════════════════════════════════════════════════════"
|
||||
echo "Done."
|
||||
|
||||
rm -f /tmp/pb-*.lsp /tmp/pb-*.sexp /tmp/pb-*.binary /tmp/pb-*.json \
|
||||
/tmp/pb-state.* /tmp/pb-corrupt.binary /tmp/pb-nope.xyz 2>/dev/null
|
||||
|
|
@ -1084,8 +1084,11 @@ def _call(proc, args, env):
|
|||
|
||||
|
||||
def _load(path, env):
|
||||
with open(path) as f:
|
||||
src = f.read()
|
||||
try:
|
||||
with open(path) as f:
|
||||
src = f.read()
|
||||
except UnicodeDecodeError:
|
||||
raise LispErr(f'{path}: not a text file (binary data encountered)')
|
||||
for expr in read_all(src, track_lines=True): leval(expr, env)
|
||||
|
||||
###############################################################################
|
||||
|
|
@ -3663,8 +3666,9 @@ Features: R7RS core, bytecode compiler, full continuations, macros,
|
|||
_load(path, g)
|
||||
except LispErr as e:
|
||||
print(f'error: {e}', file=sys.stderr); sys.exit(1)
|
||||
except FileNotFoundError:
|
||||
print(f'file not found: {path}', file=sys.stderr); sys.exit(1)
|
||||
except FileNotFoundError as e:
|
||||
missing = e.filename if e.filename else path
|
||||
print(f'file not found: {missing}', file=sys.stderr); sys.exit(1)
|
||||
return
|
||||
|
||||
# REPL mode
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue