Skip to content

Commit

Permalink
accurate hit/miss counter (#44)
Browse files Browse the repository at this point in the history
* accurate hit/miss counter

* update miss counter on expire
  • Loading branch information
Yiling-J authored Aug 21, 2024
1 parent 05dc84d commit bcfb834
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 7 deletions.
5 changes: 3 additions & 2 deletions internal/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import (
)

const (
MAX_READ_BUFF_SIZE = 64
MIN_WRITE_BUFF_SIZE = 4
MAX_WRITE_BUFF_SIZE = 1024
)
Expand Down Expand Up @@ -225,10 +224,13 @@ func (s *Store[K, V]) getFromShard(key K, hash uint64, shard *Shard[K, V]) (V, b
expire := entry.expire.Load()
if expire != 0 && expire <= s.timerwheel.clock.NowNano() {
ok = false
s.policy.miss.Add(1)
} else {
s.policy.hit.Add(1)
value = entry.value
}
} else {
s.policy.miss.Add(1)
}
shard.mu.RUnlock(tk)

Expand Down Expand Up @@ -542,7 +544,6 @@ func (s *Store[K, V]) removeEntry(entry *Entry[K, V], reason RemoveReason) {
}

func (s *Store[K, V]) drainRead(buffer []ReadBufItem[K, V]) {
s.policy.total.Add(MAX_READ_BUFF_SIZE)
s.mlock.Lock()
for _, e := range buffer {
s.policy.Access(e)
Expand Down
18 changes: 18 additions & 0 deletions internal/store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,3 +114,21 @@ func TestDoorKeeperDynamicSize(t *testing.T) {
}
require.True(t, shard.dookeeper.Capacity > 100000)
}

func TestPolicyCounter(t *testing.T) {
store := NewStore[int, int](1000, false, nil, nil, nil, 0, 0, nil)
for i := 0; i < 1000; i++ {
store.Set(i, i, 1, 0)
}
// hit
for i := 0; i < 1600; i++ {
store.Get(100)
}
// miss
for i := 0; i < 1600; i++ {
store.Get(10000)
}

require.Equal(t, int64(1600), store.policy.hit.Value())
require.Equal(t, int64(1600), store.policy.miss.Value())
}
10 changes: 5 additions & 5 deletions internal/tlfu.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ type TinyLfu[K comparable, V any] struct {
hasher *Hasher[K]
size uint
counter uint
total *Counter
miss *Counter
hit *Counter
hr float32
threshold atomic.Int32
Expand All @@ -25,7 +25,7 @@ func NewTinyLfu[K comparable, V any](size uint, hasher *Hasher[K]) *TinyLfu[K, V
sketch: NewCountMinSketch(),
step: 1,
hasher: hasher,
total: NewCounter(),
miss: NewCounter(),
hit: NewCounter(),
}
// default threshold to -1 so all entries are admitted until cache is full
Expand All @@ -34,9 +34,9 @@ func NewTinyLfu[K comparable, V any](size uint, hasher *Hasher[K]) *TinyLfu[K, V
}

func (t *TinyLfu[K, V]) climb() {
total := t.total.Value()
miss := t.miss.Value()
hit := t.hit.Value()
current := float32(hit) / float32(total)
current := float32(hit) / float32(hit+miss)
delta := current - t.hr
var diff int8
if delta > 0.0 {
Expand Down Expand Up @@ -77,7 +77,7 @@ func (t *TinyLfu[K, V]) climb() {
t.threshold.Add(-int32(diff))
t.hr = current
t.hit.Reset()
t.total.Reset()
t.miss.Reset()
}

func (t *TinyLfu[K, V]) Set(entry *Entry[K, V]) *Entry[K, V] {
Expand Down

0 comments on commit bcfb834

Please sign in to comment.