Debian Codegraph @

pkg/sievecache/doc.go

raw symbols SCIP
1 /*
2 Package sievecache provides thread-safe, high-performance implementations of the SIEVE cache replacement algorithm in Go.
3
4 # Overview
5
6 SIEVE (Simple, space-efficient, In-memory, EViction mEchanism) is a cache eviction
7 algorithm that maintains a single bit per entry to track whether an item has been
8 "visited" since it was last considered for eviction. This approach requires less
9 state than LRU but achieves excellent performance, especially on skewed workloads.
10
11 The package offers three implementations to address different concurrency needs:
12
13 - SieveCache: Non-thread-safe implementation for single-threaded use
14 - SyncSieveCache: Thread-safe wrapper with mutex locking
15 - ShardedSieveCache: High-concurrency implementation with data sharding
16
17 # Cache Implementation Details
18
19 The cache is implemented as a combination of:
20
21 1. A map for O(1) key lookups
22 2. A slice-based ordered collection for managing entries
23 3. A "visited" flag on each entry to track recent access
24 4. A "hand" pointer that indicates the next eviction candidate
25
26 When the cache is full and a new item is inserted, the eviction algorithm:
27
28 1. Starts from the "hand" position (eviction candidate)
29 2. Finds the first non-visited entry, evicting it
30 3. Marks all visited entries as non-visited while searching
31 4. Updates the hand to point to the position before the evicted entry
32
33 Performance Characteristics
34
35 - All basic operations (Get, Insert, Remove) are O(1) in the common case
36 - Memory overhead is minimal (1 bit per entry plus standard Go overhead)
37 - Thread-safe implementations provide atomic multi-operation capabilities
38 - Sharded implementation reduces lock contention for high-concurrency scenarios
39
40 Choosing the Right Implementation
41
42 - Use SieveCache for single-threaded applications
43 - Use SyncSieveCache for multi-threaded applications with moderate concurrency
44 - Use ShardedSieveCache for applications with high concurrency where operations
45 are distributed across many different keys
46
47 The package also provides a RecommendedCapacity method to dynamically adjust cache
48 size based on access patterns, which can help optimize memory usage over time.
49 */
50 package sievecache