Skip to content

Commit 04222e5

Browse files
authored
Reduce memory usage by not storing ghost entries (#125)
1 parent 323d74a commit 04222e5

File tree

2 files changed

+11
-14
lines changed

2 files changed

+11
-14
lines changed

2q.go

+3-4
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ type TwoQueueCache[K comparable, V any] struct {
3535

3636
recent simplelru.LRUCache[K, V]
3737
frequent simplelru.LRUCache[K, V]
38-
recentEvict simplelru.LRUCache[K, V]
38+
recentEvict simplelru.LRUCache[K, struct{}]
3939
lock sync.RWMutex
4040
}
4141

@@ -71,7 +71,7 @@ func New2QParams[K comparable, V any](size int, recentRatio, ghostRatio float64)
7171
if err != nil {
7272
return nil, err
7373
}
74-
recentEvict, err := simplelru.NewLRU[K, V](evictSize, nil)
74+
recentEvict, err := simplelru.NewLRU[K, struct{}](evictSize, nil)
7575
if err != nil {
7676
return nil, err
7777
}
@@ -156,8 +156,7 @@ func (c *TwoQueueCache[K, V]) ensureSpace(recentEvict bool) {
156156
// the target, evict from there
157157
if recentLen > 0 && (recentLen > c.recentSize || (recentLen == c.recentSize && !recentEvict)) {
158158
k, _, _ := c.recent.RemoveOldest()
159-
var empty V
160-
c.recentEvict.Add(k, empty)
159+
c.recentEvict.Add(k, struct{}{})
161160
return
162161
}
163162

arc.go

+8-10
Original file line numberDiff line numberDiff line change
@@ -21,23 +21,23 @@ type ARCCache[K comparable, V any] struct {
2121
size int // Size is the total capacity of the cache
2222
p int // P is the dynamic preference towards T1 or T2
2323

24-
t1 simplelru.LRUCache[K, V] // T1 is the LRU for recently accessed items
25-
b1 simplelru.LRUCache[K, V] // B1 is the LRU for evictions from t1
24+
t1 simplelru.LRUCache[K, V] // T1 is the LRU for recently accessed items
25+
b1 simplelru.LRUCache[K, struct{}] // B1 is the LRU for evictions from t1
2626

27-
t2 simplelru.LRUCache[K, V] // T2 is the LRU for frequently accessed items
28-
b2 simplelru.LRUCache[K, V] // B2 is the LRU for evictions from t2
27+
t2 simplelru.LRUCache[K, V] // T2 is the LRU for frequently accessed items
28+
b2 simplelru.LRUCache[K, struct{}] // B2 is the LRU for evictions from t2
2929

3030
lock sync.RWMutex
3131
}
3232

3333
// NewARC creates an ARC of the given size
3434
func NewARC[K comparable, V any](size int) (*ARCCache[K, V], error) {
3535
// Create the sub LRUs
36-
b1, err := simplelru.NewLRU[K, V](size, nil)
36+
b1, err := simplelru.NewLRU[K, struct{}](size, nil)
3737
if err != nil {
3838
return nil, err
3939
}
40-
b2, err := simplelru.NewLRU[K, V](size, nil)
40+
b2, err := simplelru.NewLRU[K, struct{}](size, nil)
4141
if err != nil {
4242
return nil, err
4343
}
@@ -185,14 +185,12 @@ func (c *ARCCache[K, V]) replace(b2ContainsKey bool) {
185185
if t1Len > 0 && (t1Len > c.p || (t1Len == c.p && b2ContainsKey)) {
186186
k, _, ok := c.t1.RemoveOldest()
187187
if ok {
188-
var empty V
189-
c.b1.Add(k, empty)
188+
c.b1.Add(k, struct{}{})
190189
}
191190
} else {
192191
k, _, ok := c.t2.RemoveOldest()
193192
if ok {
194-
var empty V
195-
c.b2.Add(k, empty)
193+
c.b2.Add(k, struct{}{})
196194
}
197195
}
198196
}

0 commit comments

Comments
 (0)