Adds 6 hash-set builtins (make-hash-set, hash-set?, hash-set-add!,
hash-set-contains?, hash-set-size, hash-set->list). Same sentinel
scheme as hash-table but tag word = -2 (hash-table is -1, vector
is >= 0). One cons cell per entry (vs two for hash-table) since
a set stores keys only — that's where the speedup over the Scheme-
level vector-based ht-* lib comes from.
Benchmark (tests/bench-hashset.sh, via make bench-hashset),
N=5000, i5-8350U asm tier:
portable native speedup
insert ~130 ms ~7 ms ~20x
hit-lookup ~125 ms ~8 ms ~15x
miss-lookup ~240 ms ~12 ms ~20x
Portable is the ht-* lib from proof-netspace-server-lib.lsp
(vectors + cons chains + modulo, pure Scheme). Native replaces
the Scheme-level bucket walk with an asm loop that dereferences
pairs directly — no env lookups, no frame building per iteration.
All 137 asm + 189 functional (Python + C) tests still green.
136 lines
4 KiB
Text
136 lines
4 KiB
Text
;;; bench-hashset.lsp — native hash-set vs portable vector-based ht-*
|
|
;;;
|
|
;;; ASM-ONLY: exercises the native hash-set-* primitives introduced
|
|
;;; alongside the portable-lib equivalent. C and Python tiers do not
|
|
;;; carry hash-set builtins (they have hash-table), so this file is
|
|
;;; driven from tests/bench-hashset.sh against asm/uncommonlisp only.
|
|
;;;
|
|
;;; Phases: insert-N, hit-lookup-N, miss-lookup-N.
|
|
;;; Output: ms per phase + speedup (portable/native).
|
|
|
|
(define N 5000)
|
|
|
|
;;; ─── Portable hash-set (vectors + cons chains, pure Scheme) ───
|
|
;;; Identical algorithm to proof-netspace-server-lib.lsp.
|
|
|
|
(define *pht-buckets* 127)
|
|
|
|
(define (pht-make)
|
|
(let ((v (make-vector (+ *pht-buckets* 1) '())))
|
|
(vector-set! v 0 *pht-buckets*)
|
|
v))
|
|
|
|
(define (pht-bucket t k)
|
|
(+ 1 (modulo (if (< k 0) (- 0 k) k) (vector-ref t 0))))
|
|
|
|
(define (pht-in? bucket k)
|
|
(cond ((null? bucket) #f)
|
|
((= (car bucket) k) #t)
|
|
(else (pht-in? (cdr bucket) k))))
|
|
|
|
(define (pht-has? t k)
|
|
(pht-in? (vector-ref t (pht-bucket t k)) k))
|
|
|
|
(define (pht-add! t k)
|
|
(let ((idx (pht-bucket t k)))
|
|
(let ((bucket (vector-ref t idx)))
|
|
(if (pht-in? bucket k) #f
|
|
(begin (vector-set! t idx (cons k bucket)) #t)))))
|
|
|
|
;;; ─── Timing helpers ───
|
|
|
|
(define (ms-since t0) (- (current-time-ms) t0))
|
|
|
|
(define (report label ms)
|
|
(display label) (display ": ") (display ms) (display " ms") (newline))
|
|
|
|
;;; ─── Phase runners ───
|
|
|
|
(define (portable-insert! t n)
|
|
(define (loop i)
|
|
(if (>= i n) 'done
|
|
(begin (pht-add! t i) (loop (+ i 1)))))
|
|
(loop 0))
|
|
|
|
(define (portable-hit-lookups t n)
|
|
(define (loop i hits)
|
|
(if (>= i n) hits
|
|
(loop (+ i 1) (if (pht-has? t i) (+ hits 1) hits))))
|
|
(loop 0 0))
|
|
|
|
(define (portable-miss-lookups t n)
|
|
(define (loop i)
|
|
(if (>= i n) 'done
|
|
(begin (pht-has? t (+ i n)) (loop (+ i 1)))))
|
|
(loop 0))
|
|
|
|
(define (native-insert! s n)
|
|
(define (loop i)
|
|
(if (>= i n) 'done
|
|
(begin (hash-set-add! s i) (loop (+ i 1)))))
|
|
(loop 0))
|
|
|
|
(define (native-hit-lookups s n)
|
|
(define (loop i hits)
|
|
(if (>= i n) hits
|
|
(loop (+ i 1) (if (hash-set-contains? s i) (+ hits 1) hits))))
|
|
(loop 0 0))
|
|
|
|
(define (native-miss-lookups s n)
|
|
(define (loop i)
|
|
(if (>= i n) 'done
|
|
(begin (hash-set-contains? s (+ i n)) (loop (+ i 1)))))
|
|
(loop 0))
|
|
|
|
;;; ─── Driver ───
|
|
|
|
(display "=== hash-set benchmark N=") (display N) (display " ===") (newline)
|
|
|
|
(display "-- portable (vectors + cons chains, Scheme) --") (newline)
|
|
(define pt (pht-make))
|
|
(define t0 (current-time-ms))
|
|
(portable-insert! pt N)
|
|
(define p-ins (ms-since t0))
|
|
(report " insert" p-ins)
|
|
|
|
(set! t0 (current-time-ms))
|
|
(define p-hits (portable-hit-lookups pt N))
|
|
(define p-hit-ms (ms-since t0))
|
|
(report " hit-lookup" p-hit-ms)
|
|
(display " (hits=") (display p-hits) (display ")") (newline)
|
|
|
|
(set! t0 (current-time-ms))
|
|
(portable-miss-lookups pt N)
|
|
(define p-miss (ms-since t0))
|
|
(report " miss-lookup" p-miss)
|
|
|
|
(display "-- native (hash-set-*, bump-allocated in asm) --") (newline)
|
|
(define ns (make-hash-set))
|
|
(set! t0 (current-time-ms))
|
|
(native-insert! ns N)
|
|
(define n-ins (ms-since t0))
|
|
(report " insert" n-ins)
|
|
|
|
(set! t0 (current-time-ms))
|
|
(define n-hits (native-hit-lookups ns N))
|
|
(define n-hit-ms (ms-since t0))
|
|
(report " hit-lookup" n-hit-ms)
|
|
(display " (hits=") (display n-hits) (display ")") (newline)
|
|
|
|
(set! t0 (current-time-ms))
|
|
(native-miss-lookups ns N)
|
|
(define n-miss (ms-since t0))
|
|
(report " miss-lookup" n-miss)
|
|
|
|
(display "-- speedup (portable / native) --") (newline)
|
|
(define (speedup-x p n)
|
|
(if (= n 0) 'infinite (quotient (* 1000 p) n)))
|
|
(display " insert: ") (display (speedup-x p-ins n-ins))
|
|
(display " (milliX, so ")(display (quotient (speedup-x p-ins n-ins) 1000))
|
|
(display "x)") (newline)
|
|
(display " hit-lookup: ") (display (speedup-x p-hit-ms n-hit-ms))
|
|
(display " (milliX, so ")(display (quotient (speedup-x p-hit-ms n-hit-ms) 1000))
|
|
(display "x)") (newline)
|
|
(display " miss-lookup: ") (display (speedup-x p-miss n-miss))
|
|
(display " (milliX, so ")(display (quotient (speedup-x p-miss n-miss) 1000))
|
|
(display "x)") (newline)
|