lp-hash-table

2026-11-29

Pretty fast insertion-ordered linear-probing hash tables and sets

Upstream URL

github.com/kchanqvq/lp-hash-table

Author

Qiantan Hong <qthong@stanford.edu>

License

Public Domain
README
Pretty Fast™ Linear-Probing Hash Tables and SetsThis library aims to provide hash tables with nearly highest throughput if:
  • You want to preserve insertion order; or your keys/values are Lisp objects, inthat case you almost always want to preserve insertion order, becauseinsertion order ≈ allocation order ≈ address order and GC scanning the tablewill thank you
  • Your hash function does not suck
  • Your table is really large, much larger than Last Level Cache (LLC)

This library is still pretty fast if your table is small, but some SIMD Swiss table could be faster. For my workload, my SIMD F14vector prototype ranges from as fast or 9% slower compared to lp-hash-table, while being much more complex.

This software is in Public Domain.

1How fast

Linux SBCL 2.6.3 with 24GB heap on Xeon E5-2640 v4 (25MB LLC). 4-element fixnumlist keys (because that's why we write Lisp), lp-hash-table uses a custom hashfunction, is generally 1.4x-1.8x faster than stock equal hash table:

./bench-list-table.png

2Usage

define-hash-table / define-hash-set defines a table type for a given hash-fn and test-fn, interning the operations (make-NAME, NAME-get/put/rem/clear/count, map-NAME, macro NAME-ensure) into the current package. If hash-fn and test-fn is declared inline, they will be inlined. If operations such as NAME-get is declared inline before using define-hash-table / define-hash-set, they will be inlined.

(ql:quickload :lp-hash-table)
(use-package :lp-hash-table)              ; nickname :LPHT

(define-hash-table str-table (lambda (s) (sxhash (the string s))) equal)

(defvar *h* (make-str-table))             ; &key size load-factor kv-size
(str-table-put "foo" 42 *h*)              ; => 42        insert/update, returns the value
(str-table-get "foo" *h*)                 ; => 42, T     (values value present-p)
(str-table-get "bar" *h* :none)           ; => :none, NIL   default on miss
(str-table-count *h*)                     ; => 1
(map-str-table (lambda (k v) (print (list k v))) *h*)  ; iterates in insertion order
(str-table-rem "foo" *h*)                 ; => T

(declare (inline fxhash))
(defun fxhash (x)
  (declare (type fixnum x))
  (logand (* x 2654435769) most-positive-fixnum))

(declaim (inline fixnum-set-get fixnum-set-put)) ; These can be inlined when used
(define-hash-set fixnum-set fxhash eq)     ; FXHASH is inlined

(defvar *s* (make-fixnum-set))
(fixnum-set-put 7 *s*)                     ; => 7        ;returns the stored key
(fixnum-set-get 7 *s*)                     ; => 7, T
(fixnum-set-get 9 *s*)                     ; => NIL, NIL
(map-fixnum-set #'print *s*)

3How it works

The hash table stores a PROBE vector of type (simple-array (unsigned-byte 32)) that interleaves (32-bit-hash, index + 1). 0 in the index slot marks an empty entry. PROBE has power-of-two size and implements linear-probing with backward-shift on deletion (no tombstones). index points to another KV vector, which interleaves key and values for define-hash-table and stores only keys for define-hash-set. Entries in KV vector mirrors insertion order, and new element are appended to HWM (high water mark). Deleted entries are marked by +delete+. When KV is full, *-COMPACT is called to remove all deleted entries. The index indirection has two benefits:

  • map-* preserve insertion order
  • PROBE, a nightmare for GC if we store boxed keys/values there, do not need tobe scanned; KV is scanned and is GC-friendly

Note: the above assume *probe-bits* is 32. On platform where fixnum is shorter this will be decreased automatically (like 32-bit platforms). You can also control this via the :probe-bits option.

4Knobs

There isn't one single fastest hash table, so the library provides a number of knobs:

  • define-hash-set to save space if you don't need to store values
  • pass :store-hash t to speed up compaction if your hash function is expensiveand your workload is delete-heavy; otherwise :store-hash nil to save space.
  • pass :optimize (speed (safety 0)) to become maybe faster or/and heap-corrupted
  • :probe-bits defaults to *probe-bits*, which is a reasonable guess(32-bit if possible, or shorter if fixnum is short).
  • The constructor make-* defined by the define-hash-table or define-hash-setmacro takes option:
    • :size initial size
    • :load-factor default to 0.7
    • :kv-size control frequency of compaction, KV vector has sizeprobe-size*load-factor*kv-size, default to 1.5.

Ping me if you wish for specialized key/value type or whatever else. The library currently serves my use case (hash-consing) well, but I'm happy to help with yours.

5Advanced functionality

Macro (NAME-ensure KEY TABLE &optional DEFAULT) is like ALEXANDRIA:ENSURE-GETHASH but it also has performance benefit (it only probes once, instead of twice if you use NAME-get and NAME-put separately.
(str-table-ensure "bar" *h* 0)            ; => 0, NIL    on miss, store default
(str-table-ensure "bar" *h* (error "no")) ; => 0, T      on hit, default not evaluated

For sets, (NAME-ensure KEY SET &optional DEFAULT) adds DEFAULT to SET if and only if KEY does not exist in SET. User should guarantee DEFAULT and KEY are equal under test-fn, otherwise all hell breaks loose. DEFAULT defaults to KEY for sets.

Dependencies (3)

  • alexandria
  • fiveam
  • trivial-benchmark

Dependents (0)

    • GitHub
    • Quicklisp