Debian Codegraph @

/

0 files · 0 symbols
README (full)

go-sieve - SIEVE cache eviction for Go

Go Reference Go Report Card Release

A Go implementation of the SIEVE cache eviction algorithm (NSDI'24, Zhang et al.), engineered from the ground up for highly concurrent, read-heavy workloads. Generic over key and value types.

The read path (Get()) is fully lock-free — a single atomic load on the key→index map plus a single atomic bit update on a shared visited bitfield. No mutex, no pointer chasing, no per-entry allocations, zero GC traffic on hits. Under concurrent read load this is 13–55x faster than hashicorp/golang-lru per trace on real-world cache traces (warm cache, 10 cores), and the gap grows with core count: SIEVE's aggregate throughput improves as cores are added while mutex-bound LRU/ARC throughput degrades below its own single-core line. Under a cold-cache mixed read/write replay, SIEVE is 2–6x faster. The write path uses a single short-held mutex and a pre-allocated node pool, so Add()/Probe()/ProbeFunc() also avoid per-operation heap allocation in steady state.

If you need a cache that many goroutines hit simultaneously on the read path — an HTTP response cache, a DNS resolver cache, an authz decision cache, a hot-path object lookup — this implementation is built for that shape. On a single goroutine the two are comparable (SIEVE replays most traces faster sequentially, LRU wins a few); under any concurrency, Sieve wins decisively.

SIEVE uses a FIFO queue with a roving "hand" pointer: cache hits set a visited bit (lazy promotion), and eviction scans from the hand clearing visited bits until it finds an unvisited node (quick demotion). It matches or exceeds LRU/ARC hit ratios with far less bookkeeping — validated here on ~300M requests from the MSR Cambridge and Meta Storage trace repositories.

Key Design Elements

Array-backed indexed list. Marc Brooker observed that SIEVE's mid-list removal prevents a simple circular buffer. Rather than Tobin Baker's "swap tail into hole" workaround, this implementation uses a doubly-linked list with int32 indices into a pre-allocated backing array. This preserves SIEVE's exact eviction semantics while eliminating all interior pointers — the GC sees a flat []node with no pointers to trace (for non-pointer K, V types).

xsync.MapOf for concurrent access. The key→index map uses puzpuzpuz/xsync.MapOf which stores int32 values inline in cache-line-padded buckets — no traced pointers per entry. Get() is fully lock-free; only Add()/Probe()/ProbeFunc() (on miss) and Delete() take the global mutex.

Columnar slot state (slotState). Each node's lock and visited counter are hoisted out of the node struct into a separate contiguous []uint64 — one word per slot, laid out as a column alongside the []node array. This columnar split has three effects:

  1. the eviction hand scans IsVisited by walking a dense []uint64 sequentially — 8 slots per cache line, hardware-prefetch friendly — without pulling in key/val data it doesn't need;
  2. Get()'s LockAndMark writes only to the slotState word for that slot, so it never dirties the cache line holding the node's key/val — no false sharing with concurrent readers of adjacent nodes
  3. []uint64 contains no pointers, so the GC never traces it, unlike node fields that may hold pointer-typed K/V.

Within each word, bit 63 is a spinlock and the low bits are a saturating visited counter (1 bit at k=1, ⌈log₂(k+1)⌉ bits for higher k).

Pre-allocated node pool. All nodes are allocated once at cache creation in a contiguous array. A bump allocator + intrusive freelist (reusing node.next) provides O(1) alloc/free with zero heap allocations during steady-state operation.

TOCTOU-safe concurrent writes. Add() and Probe() use a double-check pattern: fast-path Load() outside the lock, re-check under mu.Lock() before inserting. This prevents duplicate nodes from concurrent writers racing on the same key. ProbeFunc() calls its constructor between the fast-path miss and the lock, so the constructor never runs under the mutex; concurrent misses on the same key may each construct a value, but only one is stored and the other callers receive it with CacheHit.

Benchmark Results

Benchmarked against hashicorp/golang-lru v2.0.7 (LRU and ARC) on an Apple M4 (10 cores: 4 performance + 6 efficiency):

  • macOS (Darwin 25.5.0, arm64)
  • go1.26.3
  • GOMAXPROCS=10

Benchmarks live in bench/ as a separate module to avoid polluting go.mod. Every table below is generated by bench/cmd/mktables from the raw results files — including derived claims, which are computed rather than hand-counted.

Commands used (no name filter — every benchmark in every package runs):

cd bench && make bench    # synthetic comparison, count=3
cd bench && make trace    # trace replay + miss ratio + GC, count=1
cd bench && make sweep SWEEP_PROCS="1 2 4 8 10"   # core-scaling sweep

Full raw results: bench-results.md.

Reading the parallel ns/op numbers

The single-digit ns/op numbers in the parallel tables below are real but need context: they are aggregate throughput.

Go's b.RunParallel distributes b.N total operations across GOMAXPROCS goroutines and reports ns/op = wall_clock / b.N. When 10 goroutines complete Get()s at 5.8 ns/op aggregate, the system produces one completed Get every ~5.8 ns; the per-core latency is ~58 ns (5.8 × 10 cores), consistent with two L1/L2-hot atomic operations plus the surrounding map lookup.

LRU/ARC report ~140–390 ns/op under the same conditions — not because a single Get is that much slower, but because every Get takes a mutex. All goroutines serialize through one lock, so adding cores makes aggregate throughput worse (see the core-scaling table below: LRU goes from 31 ns/op at 1 core to 182 ns/op at 10 — lock handoff costs grow with contention). On a single goroutine, SIEVE and LRU are in the same league — the gap is a concurrency-scaling story.

After warmup, the working data structures (map buckets, node array, visited bitfield) are resident in CPU cache — L1/L2 for small traces, L3 for larger ones. Real workloads with lower temporal locality will see higher per-core latency, but the relative advantage over mutex-bound LRU/ARC holds whenever there is any parallelism at all.

Parallel Micro-Benchmarks (count=3, medians)

BenchmarkSieveLRUARC
Get_Parallel5.76 ns/op, 0 B144.2 ns/op, 0 B144.9 ns/op, 0 B
Add_Parallel89.8 ns/op, 8 B212.0 ns/op, 40 B273.3 ns/op, 74 B
Probe_Parallel91.2 ns/op, 8 B——
Delete_Parallel81.2 ns/op, 0 B69.5 ns/op, 0 B88.3 ns/op, 0 B
Mixed_Parallel (60/30/10)71.0 ns/op, 2 B162.2 ns/op, 12 B211.8 ns/op, 23 B
Zipf_Get_Parallel (s=1.01)24.7 ns/op127.7 ns/op133.1 ns/op
Zipf_Get_Parallel (s=1.50)94.2 ns/op100.4 ns/op112.6 ns/op
Memory @ 1M fill78.4 MB112.4 MB112.3 MB
GCImpact (1M entries)4.37 ms/iter, 18.1 µs pause9.46 ms/iter, 22.4 µs9.93 ms/iter, 23.5 µs

Probe_Parallel is SIEVE-only — LRU's PeekOrAdd and ContainsOrAdd skip recency promotion and are not semantic equivalents. Delete_Parallel is the one micro where SIEVE loses: LRU's single-lock linked-list unlink edges out SIEVE's slot-state clear (69.5 vs 81.2 ns/op). At extreme Zipf skew (s=1.50) SIEVE's advantage narrows to ~6%: most requests hit a handful of keys, so readers contend on the same per-slot word.

Core scaling (Get_Parallel, median ns/op)

GOMAXPROCSSieveLRUARC
114.931.433.8
28.974.377.0
45.5121.1146.0
88.0178.7182.0
107.5182.1193.4

SIEVE's aggregate throughput improves with cores (the 8–10 core numbers include the M4's efficiency cores); LRU/ARC throughput degrades with every added core because all readers fight over one mutex — at 10 cores LRU is ~6x slower per completed Get than at 1 core.

Trace Replay Results

This implementation is benchmarked against real-world cache traces from the libCacheSim trace repository — 13 MSR Cambridge enterprise block I/O traces + 5 Meta Storage (Tectonic) block traces totalling ~300M requests. Each trace was replayed with a cache sized at 10% of unique keys, comparing SIEVE (k=1, k=2, k=3) against hashicorp/golang-lru (LRU and ARC). Parallel benchmarks walk the trace as a ring with per-goroutine staggered start offsets (see bench/README.md).

Parallel Get throughput (warm cache, 10 goroutines, ns/op, zero allocs)

SIEVE's lock-free Get() is 13–55x faster than the better of LRU/ARC on every trace under concurrent read load:

TraceSIEVE k=1SIEVE k=3LRUARC
msr_2007/msr_web_22.242.05105.7150.4
msr_2007/msr_proj_42.843.87157.4220.8
msr_2007/msr_prxy_03.283.15106.3133.3
msr_2007/msr_prn_13.513.37135.9185.8
meta_storage/block_traces_24.014.31129.4211.4
meta_storage/block_traces_14.034.34132.5197.9
msr_2007/msr_usr_24.624.79209.4268.5
meta_storage/block_traces_44.884.87159.2250.5
meta_storage/block_traces_34.954.56147.2224.8
meta_storage/block_traces_55.004.92152.2222.5
msr_2007/msr_src1_05.655.60157.4384.9
msr_2007/msr_usr_16.397.73252.6308.0
msr_2007/msr_src1_17.047.20165.9276.9
msr_2007/msr_proj_17.317.21255.1307.3
msr_2007/msr_proj_08.158.55110.4136.9
msr_2007/msr_hm_09.129.38117.6141.8
msr_2007/msr_proj_210.097.96260.1299.6
msr_2007/msr_prn_012.2012.34173.2154.2

The k=3 saturating counter adds little to no overhead on the read path. This benchmark pre-warms the cache with a full trace replay, then hammers Get() only — the ideal scenario for a read-heavy cache in steady state.

Parallel Replay throughput (cold cache, mixed read+write, 10 goroutines, ns/op)

This is Probe() for SIEVE and Get+Add for LRU/ARC, hammered in parallel with no warmup. It measures the steady-state workload a real cache faces: reads and writes interleaved, evictions happening live. The previous table's numbers reflect the best-case read ceiling; this table reflects what throughput you actually get when your cache is doing work.

TraceSIEVE k=1SIEVE k=3LRUARC
msr_2007/msr_prxy_037.436.6229.3251.1
msr_2007/msr_hm_058.761.0228.9297.9
msr_2007/msr_prn_062.267.6251.6322.3
msr_2007/msr_proj_063.058.6236.2312.2
msr_2007/msr_usr_173.680.4376.8391.2
msr_2007/msr_prn_190.491.5351.7390.4
msr_2007/msr_proj_4110.0113.5425.1563.9
msr_2007/msr_proj_1118.6121.3447.0505.6
meta_storage/block_traces_1134.2160.0277.2351.6
meta_storage/block_traces_2137.9179.4300.5374.9
msr_2007/msr_web_2155.5145.4478.4513.7
meta_storage/block_traces_4156.2179.2371.8384.8
meta_storage/block_traces_5157.4178.1336.2377.9
meta_storage/block_traces_3158.4178.6312.7380.4
msr_2007/msr_src1_0219.3200.8628.9588.3
msr_2007/msr_usr_2222.3215.6627.7579.9
msr_2007/msr_proj_2252.3265.0528.3527.5
msr_2007/msr_src1_1274.7274.2652.3633.4

Under concurrent cold-cache replay, SIEVE is 2–6x faster than the better of LRU/ARC on every trace. The margin is smaller than the warm table because every miss serializes through the same write mutex in all three caches — SIEVE's edge here comes entirely from its lock-free hit path. The best case is msr_prxy_0 (95% hits, 6.1x); the worst cases are the high-miss-ratio traces (msr_src1_1, 98% misses, 2.3x).

(An earlier revision of this table claimed 18–62x. That number was an artifact of all goroutines walking the trace from offset 0 in lockstep, which inflated hit ratios; the staggered-offset harness fixes it.)

Miss ratio (every trace)

Cache sized at 10% of unique keys. Bold = best in row.

TraceSIEVE k=1SIEVE k=2SIEVE k=3LRUARC
meta_storage/block_traces_10.46320.46510.46720.46020.4667
meta_storage/block_traces_20.47190.47430.47540.46760.4755
meta_storage/block_traces_30.49080.49280.49480.48850.4947
meta_storage/block_traces_40.48410.48700.48880.48120.4887
meta_storage/block_traces_50.49590.49840.49980.49270.5003
msr_2007/msr_hm_00.29910.30250.30250.31880.2923
msr_2007/msr_prn_00.21560.21940.22080.23100.2145
msr_2007/msr_prn_10.39080.38370.37960.43410.4148
msr_2007/msr_proj_00.25370.26600.27450.23750.2242
msr_2007/msr_proj_10.67940.67940.67940.72150.6788
msr_2007/msr_proj_20.82310.82310.82310.85480.8125
msr_2007/msr_proj_40.84630.84630.84630.81400.7173
msr_2007/msr_prxy_00.05120.05720.05940.04760.0468
msr_2007/msr_src1_00.78450.78450.78450.91320.7811
msr_2007/msr_src1_10.79390.79340.79340.81290.8231
msr_2007/msr_usr_10.35580.35580.35580.40070.3513
msr_2007/msr_usr_20.72160.72160.72160.75330.7199
msr_2007/msr_web_20.97860.97860.97860.99290.9785

Overall best (bold, computed by mktables): ARC has the lowest miss ratio in 11 of 18 rows, LRU in 5 (all meta_storage), SIEVE k-variants in 2 — msr_prn_1 (k=3 outright) and msr_src1_1 (k=2 and k=3 tied at 0.7934). SIEVE k=1 is never the overall best.

Head-to-head, SIEVE k=1 vs LRU (the typical deployment choice): SIEVE k=1 has lower miss ratio on 10 of 18 traces, LRU on 8. When SIEVE is better the margins are large (msr_src1_0: 12.9 points, msr_usr_1: 4.5, msr_prn_1: 4.3). When LRU is better the margins are narrow (all 5 meta_storage: 2–3 points each).

SIEVE's case rests on throughput. Miss ratios are competitive (SIEVE is never dramatically worse than LRU), and SIEVE is 2–6x faster on a cold mixed workload and 13–55x faster on warm reads, with the gap widening as cores are added. SIEVE k=3 produces the single-best entry in the entire table on msr_prn_1 (0.3796 vs LRU 0.4341, ARC 0.4148).

Memory during replay

On the 13.2M-request meta_storage/block_traces_1 trace (601K-entry cache), TestGCPressure reports:

VariantTotalAlloc
SIEVE k=1135 MB
SIEVE k=3135 MB
LRU418 MB
ARC997 MB

SIEVE allocates 3.1x less than LRU and 7.4x less than ARC during replay — the array-backed node pool and inline-int32 xsync.MapOf are structural wins.

Full per-trace tables (sequential replay ns/op, B/op, miss ratio for every trace) are in bench-results.md. Methodology and trace-loading details are in bench/README.md.

Usage

import "github.com/opencoff/go-sieve"

// Create a cache mapping string keys to int values, capacity 1000.
c, err := sieve.New[string, int](1000)
if err != nil {
    log.Fatal(err) // ErrInvalidCapacity or ErrInvalidVisitClamp
}

// Or, for constant arguments, use Must to get a one-liner:
c := sieve.Must(sieve.New[string, int](1000))

c.Add("foo", 42)

if val, ok := c.Get("foo"); ok {
    fmt.Println(val) // 42
}

// Probe inserts only if absent; returns the cached value if present.
val, _, r := c.Probe("foo", 99)
// val == 42, r.Hit() == true

// ProbeFunc is Probe with lazy construction: mk runs only on a miss.
val, _, r = c.ProbeFunc("bar", func(k string) int { return len(k) })
// val == 3, r.Hit() == false

c.Delete("foo")
c.Purge() // reset entire cache

SIEVE-k

WithVisitClamp(k) creates a SIEVE-k cache where each entry uses a saturating counter instead of a single visited bit. An item accessed k+1 times survives k eviction passes before being evicted. k=1 is equivalent to classic SIEVE (the default). Use k=2 or k=3 for workloads with repeated access patterns where extra eviction resistance is beneficial.

// Classic SIEVE (k=1, the default)
c, err := sieve.New[string, int](1000)

// SIEVE-k=3: items survive up to 3 eviction passes
c, err := sieve.New[string, int](1000, sieve.WithVisitClamp(3))

New returns an error for capacity <= 0 (ErrInvalidCapacity) and for WithVisitClamp(k) with k > sieve.MaxVisitClamp (7 — ErrInvalidVisitClamp). Clamp values below 1 are silently rounded up to 1.

Eviction Handling

Add(), Probe() and ProbeFunc() return the evicted entry (if any) along with a CacheResult bitmask. This allows callers to handle evictions synchronously without channels, goroutines, or lifecycle management.

c := sieve.Must(sieve.New[string, int](1000))

ev, r := c.Add("foo", 42)
if r.Evicted() {
    cleanupDisk(ev.Key, ev.Val)
}

// CacheResult bitmask:
//   CacheHit   — key was already present (value updated, no eviction)
//   CacheEvict — an entry was evicted to make room (mutually exclusive with CacheHit)

Purge() and Delete() do not report evictions.

Iteration

All() returns a Go 1.23+ iter.Seq2[K, V] over the cache contents. Iteration is weakly consistent and safe to call concurrently with Get/Add/Probe/ProbeFunc/Delete (including from inside the loop body):

for k, v := range c.All() {
    fmt.Println(k, v)
}
  • Order is unspecified — it does not follow SIEVE FIFO order.
  • Entries inserted during the walk may or may not be observed; entries evicted or deleted during the walk are skipped.
  • Walking the cache does not mark entries as visited, so iteration does not protect entries from eviction (unlike Get).
  • No cache lock is held across the loop body, so re-entrant Get/Add/Delete calls from inside range are safe.

API

Function / MethodDescription
New[K, V](capacity, ...Option) (*Sieve[K,V], error)Create a cache with fixed capacity. Returns ErrInvalidCapacity or ErrInvalidVisitClamp on bad input.
Must[K, V](*Sieve[K,V], error) *Sieve[K,V]Helper that panics on error; useful with constant arguments.
Get(key) (V, bool)Look up a key (lock-free, zero-alloc).
Add(key, val) (Evicted[K,V], CacheResult)Insert or update; returns evicted entry and result bitmask.
Probe(key, val) (V, Evicted[K,V], CacheResult)Insert-if-absent; returns cached/inserted value, evicted entry, and result bitmask.
ProbeFunc(key, mk func(K) V) (V, Evicted[K,V], CacheResult)Like Probe, but mk(key) is called only on a miss, with no locks held. Concurrent misses may each call mk; one result is stored.
Delete(key) boolRemove a key.
Purge()Clear the entire cache.
Len() intCurrent number of entries (lock-free atomic load).
Cap() intMaximum capacity.
All() iter.Seq2[K, V]Weakly-consistent range iterator over current entries; does not mark visited.

Options

OptionDescription
WithVisitClamp(k)Use k-level saturating counters (default k=1 = classic SIEVE). k is capped at MaxVisitClamp (7); k < 1 is silently rounded to 1.

Key Constraints

The zero value of K is reserved — do not use it as a key ("" for string keys, 0 for integer keys, the zero struct for struct keys).

This is by design: when an entry is evicted or deleted, its slot's key is zeroed in place, and the lock-free read path relies on a zeroed key never matching a live lookup. If you store the zero key, a Get racing with that key's eviction can observe the zeroed slot and return a spurious hit with a zero value. In single-threaded use the zero key appears to work, but the contract is: keys must be non-zero.

If your natural key space includes the zero value, wrap it (e.g. offset integer keys by 1, or prefix string keys).

GC Note

When K or V is a pointer type (including string, which contains an internal pointer in Go), the node array will still contain GC-traced pointers. The GC pressure reduction is most dramatic for scalar key/value types (int, [16]byte, fixed-size structs).

License

BSD-2-Clause. See the source files for the full license text.

dir bench/
dir debian/
dir exp/
file .gitignore
file .golangci.yml
file LICENSE
file Makefile
file README.md
file assert_test.go
file bench-results.md
file go.mod
file go.sum
file invariants_test.go
file linters.mod
file linters.sum
file options.go
file pause.go
file sieve.go
file sieve_adversarial_test.go
file sieve_bench_custom_test.go
file sieve_contract_test.go
file sieve_evict_test.go
file sieve_iter_test.go
file sieve_k_test.go
file sieve_race_test.go
file sieve_test.go
file slotstate.go
file slotstate_test.go
file validate_test.go
file zipf_bench_test.go