← Blog of turtle

cl-cht: concurrent hash tables for Common Lisp

· 01.12.2026 06:50 · 3

Common Lisp's standard hash tables are not safe to use from more than one thread. Wrapping one in a global lock works, but under real concurrency it turns the table into a single serialization point.

cl-cht is a small library of concurrent hash tables with two backends: a portable sharded table, and — on SBCL/x86-64 — an open-addressing table whose metadata probing is done with SSE2, using an AVX2 broadcast where the CPU supports it.

Two backends

The portable chained backend has two modes:

  • :synchronized — one shard, one lock. The simple case.
  • :sharded — N independent shards, each with its own lock; power-of-two

The SIMD backend is selected with :fast / :simd. On SBCL/x86-64 it is an open-addressing table: a lookup scans one aligned group of sixteen metadata bytes with a single SSE2 load, compare and movemask, and only the surviving slots get a full key comparison. When the CPU has AVX2 the broadcast uses it; otherwise the SSE2 path is used. Selection goes through sb-cpu-features on every call and is re-probed at image start, so a binary built on an AVX2 machine runs on one without it. On other platforms :fast/:simd fall back to :sharded.

Usage

(defvar *sessions* (cl-cht:make-cht :test 'equal :mode :sharded :shards 16))

(setf (cl-cht:cht-gethash "token" *sessions*) session)
(cl-cht:cht-gethash "token" *sessions*)   ; => session
(cl-cht:cht-remhash "token" *sessions*)

cht-gethash, cht-remhash, cht-maphash, cht-clrhash and cht-hash-table-count mirror the standard names and work with setf. cht-get, cht-put, cht-rem, cht-clr, cht-map, cht-keys, cht-values, cht-count, cht-test, cht-mode, cht-shards and with-cht-lock are there for code that prefers explicit functions.

Source

github.com/turtle-bazon/cl-cht