← Blog of turtle

cl-cht vs SBCL

· 01.12.2026 12:57 · 20

Someone on Reddit asked how cl-cht compares with SBCL's built-in (make-hash-table :synchronized t). Fair question: SBCL ships a thread-safe table, so why use another one? I put a benchmark in the repository and ran it. Short version: SBCL's synchronized table is faster on one thread; cl-cht's sharded table wins on reads as soon as threads overlap, and on writes by four threads. The shard count and the :size argument matter more than the headline ratio.

What was measured

SBCL 2.6.7 on a four-vCPU Xeon Skylake KVM guest, Linux, AVX2 available. 100,000 entries, fixnum keys, :test 'eql. The access order is a precomputed LCG sequence shared by every implementation. Each number is wall-clock, median of three runs, with a full GC before each timed region. Operations go through a function slot for every table, so none of them gets an inlining advantage. The plain SBCL table is included as a single-thread reference only; it is not safe for concurrent writers, and the scaling tables leave it out.

One thread

Mops/s, and bytes consed per insert:

implementationget hitget missput oldput newB/insertremove
sb-hash4.357.044.1011.360.0013.89
sb-sync2.303.972.155.000.005.00
cht-sync1.392.041.382.3131.951.58
cht-sharded1.421.341.352.1931.951.36
cht-simd1.420.611.320.570.001.54

Synchronization costs SBCL about 47% of its lookup throughput and 56% of its insert throughput. cl-cht's synchronized mode is another step slower: it is a portable chained table, every operation takes a bordeaux-threads mutex, and each insert conses a key/value pair into a bucket alist — two conses, 32 bytes on a 64-bit SBCL. SBCL writes into preallocated slots and consed nothing during the insert run.

:sharded is not faster than :synchronized on one thread. It does the same work plus a shard lookup; the point of sharding is what happens next.

The SIMD backend is not a win at this size. It is an open-addressing table with an SSE2 group probe, but its misses scan more groups than a hit and make-cht sizes it generously. It is included because it is part of cl-cht, not because 100k fixnum keys is its target workload.

Scaling

Aggregate Mops/s over all threads.

Reads, 1,000,000 lookups per thread:

threadssb-synccht-synccht-shardedcht-simd
12.171.451.401.34
21.020.671.821.80
41.140.713.292.73

Disjoint-key inserts, 200,000 per thread:

threadssb-synccht-synccht-shardedcht-simd
14.552.381.520.49
22.131.302.380.80
41.771.292.941.27

90% reads, 10% writes, 500,000 operations per thread:

threadssb-synccht-synccht-shardedcht-simd
12.081.321.191.25
20.940.621.951.67
40.980.642.513.11

The synchronized SBCL table serialises every operation, reads included. Its aggregate read throughput does not merely stay flat as threads are added — it falls, from 2.17 to 1.14 Mops/s, because more threads queue on the same lock. At four threads cl-cht's sharded table is about 2.9x faster for reads, 1.7x for disjoint inserts, and 2.6x for the 90/10 mix.

It is not linear. With 16 shards and four threads, two threads land on the same shard often enough to matter: the collision rate is 1/16 per pair, and a collision means one of them waits for the shard's mutex. A separate lock-only experiment with four locks spread far apart in the heap reaches about 3.7x the one-thread rate, so the mutex itself is not the ceiling; the shard count is.

Shard count

Sharded backend at four threads, Mops/s:

shardsreadwrite
11.932.30
42.322.33
162.702.74
644.002.02
2564.100.80

More shards reduce lock contention and help reads; they also multiply the number of bucket vectors, locks and per-shard rehashes, which hurts writes. 16 shards, the default, is a reasonable balance for a mixed workload. A read-mostly table on four cores does better with 64 or 256.

Sizing matters as much as sharding

The first run of the write benchmark had cht-sharded barely scaling at all. The table was created with :size equal to the number of entries inserted. The chained backend rehashes at a 0.75 load factor, so that hint leaves no room for the last quarter and pays for a resize in the middle of the run. Asking for twice the entry count took four-thread insert throughput from 1.54 to 3.85 Mops/s in an isolated check.

SBCL's :size already accounts for its load factor; cl-cht's :size is a bucket hint that does not. In the benchmark, the chained tables are sized at 2× the entries and the SIMD table, which reserves far more than the entry count internally, is left alone. If you use cl-cht and know how many entries you expect, ask for twice that many.

So which one?

  • One thread, or one writer with rare readers: SBCL's ordinary hash table. It is the fastest thing in the list and allocates nothing per insert.
  • Several threads that only read: SBCL's :synchronized table still takes a lock on every gethash, so it does not scale. cl-cht with enough shards does.
  • Several threads that read and write: cl-cht's :sharded is the one that keeps going. Budget 32 bytes per entry for the chained buckets and size for twice the entries.
  • A hot key that every thread updates: no sharded table helps; all of those operations meet at one shard. Use with-cht-lock and keep the critical section small, or move the counter out of the table.

The numbers here are one host, fixnum keys, one SBCL. String keys make hashing more expensive on both sides, and SBCL caches string hashes in the string object, so the ratio can move. SBCL mutex performance is implementation and kernel dependent. Run benchmark/run.sh on the machine you actually care about; the script prints the same tables, and benchmark/README.md records the method.

hg clone https://github.com/turtle-bazon/cl-cht
cd cl-cht
./benchmark/run.sh

cl-cht is not a drop-in "faster hash table". It is a table that stays useful when more than one thread is inside it, and the benchmark is there to show where that starts to pay.