SIEVE Cache (Go)
Topics: cache eviction, doubly-linked list, SIEVE
Problem
Implement the SIEVE eviction algorithm (Zhang et al., NSDI 2024) — simpler than LRU, with a
better hit rate, and now used in production caches. It's a single FIFO-ordered doubly-linked list
where each entry has a visited bit, plus a hand that scans for a victim.
func New[K comparable, V any](capacity int) *Sieve[K, V]
func (c *Sieve[K, V]) Get(key K) (V, bool)
func (c *Sieve[K, V]) Put(key K, value V)
func (c *Sieve[K, V]) Len() int
- New entries are inserted at the head (newest) with their visited bit clear.
Get (a hit) sets the entry's visited bit — its "second chance" — and does not move it.
- On a full
Put, evict: start at the hand (or the tail/oldest on the first eviction) and
scan toward the head. Clear the visited bit of any entry you pass that has it set; evict the
first entry whose bit is already clear. The hand then points at the next candidate (toward the
head) and keeps that position for the next eviction.
- Updating an existing key refreshes its value and marks it visited.
put 1,2,3 (cap 3); get(1); put(4) → evicts 2, not 1 (1 had its bit set)
The node, insertAtHead, and unlink plumbing is provided; you implement Get, Put, and
evict.
Run
go test -v -race ./challenges/sieve-cache/go/
Sign in to submit your solution.