Skip to content

Commit b49e313

Browse files
Thomas StrombergThomas Stromberg
authored andcommitted
ghost tuning
1 parent 9ef5a45 commit b49e313

File tree

1 file changed

+11
-5
lines changed

1 file changed

+11
-5
lines changed

s3fifo.go

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -84,21 +84,27 @@ func newS3FIFO[K comparable, V any](capacity int) *s3fifo[K, V] {
8484
}
8585

8686
// newShard creates a new S3-FIFO shard with the given capacity.
87+
// Queue sizes per S3-FIFO paper: Small=10%, Main=90%, Ghost=90% (matching Main).
8788
func newShard[K comparable, V any](capacity int) *shard[K, V] {
8889
smallCap := capacity / 10
8990
if smallCap < 1 {
9091
smallCap = 1
9192
}
93+
// Ghost tracks evicted keys from Main, so size matches Main (90% of capacity)
94+
ghostCap := capacity - smallCap
95+
if ghostCap < 1 {
96+
ghostCap = 1
97+
}
9298

9399
return &shard[K, V]{
94100
capacity: capacity,
95101
smallCap: smallCap,
96-
ghostCap: capacity,
97-
items: make(map[K]*entry[K, V]),
102+
ghostCap: ghostCap,
103+
items: make(map[K]*entry[K, V], capacity),
98104
small: list.New(),
99105
main: list.New(),
100106
ghost: list.New(),
101-
ghostKeys: make(map[K]*list.Element),
107+
ghostKeys: make(map[K]*list.Element, ghostCap),
102108
}
103109
}
104110

@@ -373,11 +379,11 @@ func (s *shard[K, V]) flush() int {
373379
defer s.mu.Unlock()
374380

375381
n := len(s.items)
376-
s.items = make(map[K]*entry[K, V])
382+
s.items = make(map[K]*entry[K, V], s.capacity)
377383
s.small.Init()
378384
s.main.Init()
379385
s.ghost.Init()
380-
s.ghostKeys = make(map[K]*list.Element)
386+
s.ghostKeys = make(map[K]*list.Element, s.ghostCap)
381387
return n
382388
}
383389

0 commit comments

Comments
 (0)