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:
| implementation | get hit | get miss | put old | put new | B/insert | remove |
|---|---|---|---|---|---|---|
| sb-hash | 4.35 | 7.04 | 4.10 | 11.36 | 0.00 | 13.89 |
| sb-sync | 2.30 | 3.97 | 2.15 | 5.00 | 0.00 | 5.00 |
| cht-sync | 1.39 | 2.04 | 1.38 | 2.31 | 31.95 | 1.58 |
| cht-sharded | 1.42 | 1.34 | 1.35 | 2.19 | 31.95 | 1.36 |
| cht-simd | 1.42 | 0.61 | 1.32 | 0.57 | 0.00 | 1.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:
| threads | sb-sync | cht-sync | cht-sharded | cht-simd |
|---|---|---|---|---|
| 1 | 2.17 | 1.45 | 1.40 | 1.34 |
| 2 | 1.02 | 0.67 | 1.82 | 1.80 |
| 4 | 1.14 | 0.71 | 3.29 | 2.73 |
Disjoint-key inserts, 200,000 per thread:
| threads | sb-sync | cht-sync | cht-sharded | cht-simd |
|---|---|---|---|---|
| 1 | 4.55 | 2.38 | 1.52 | 0.49 |
| 2 | 2.13 | 1.30 | 2.38 | 0.80 |
| 4 | 1.77 | 1.29 | 2.94 | 1.27 |
90% reads, 10% writes, 500,000 operations per thread:
| threads | sb-sync | cht-sync | cht-sharded | cht-simd |
|---|---|---|---|---|
| 1 | 2.08 | 1.32 | 1.19 | 1.25 |
| 2 | 0.94 | 0.62 | 1.95 | 1.67 |
| 4 | 0.98 | 0.64 | 2.51 | 3.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:
| shards | read | write |
|---|---|---|
| 1 | 1.93 | 2.30 |
| 4 | 2.32 | 2.33 |
| 16 | 2.70 | 2.74 |
| 64 | 4.00 | 2.02 |
| 256 | 4.10 | 0.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
:synchronizedtable still takes a lock on everygethash, so it does not scale. cl-cht with enough shards does. - Several threads that read and write: cl-cht's
:shardedis 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-lockand 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.