Skip to content

Commit

Permalink
add sketch resize test
Browse files Browse the repository at this point in the history
  • Loading branch information
Yiling-J committed Oct 24, 2024
1 parent 7bcd43f commit 8dd0066
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
8 changes: 7 additions & 1 deletion internal/tlfu.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,8 +172,14 @@ func (t *TinyLfu[K, V]) Set(entry *Entry[K, V]) {
if entry.meta.prev == nil {
t.window.PushFront(entry)
}

t.demoteFromProtected()
t.EvictEntries()
if t.weightedSize > t.capacity {
t.EvictEntries()
} else {
count := t.slru.probation.count + t.slru.protected.count + t.window.count
t.sketch.EnsureCapacity(uint(count))
}
}

func (t *TinyLfu[K, V]) Access(item ReadBufItem[K, V]) {
Expand Down
17 changes: 17 additions & 0 deletions internal/tlfu_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -549,3 +549,20 @@ func TestTlfu_AdaptiveAmountRemain(t *testing.T) {
require.Equal(t, "998-998>149-110:109-103>101-80:79-0", result)

}

func TestTlfu_SketchResize(t *testing.T) {
hasher := NewHasher[int](nil)
tlfu := NewTinyLfu[int, int](10000, hasher)

for i := 0; i < 10000; i++ {
tlfu.Set(&Entry[int, int]{key: i, value: i, policyWeight: 1})
require.True(t, len(tlfu.sketch.Table) >= i, fmt.Sprintf("sketch size %d < %d", len(tlfu.sketch.Table), i))
}

size := len(tlfu.sketch.Table)
require.Equal(t, 16384, size)

for i := 10000; i < 20000; i++ {
require.Equal(t, size, len(tlfu.sketch.Table))
}
}

0 comments on commit 8dd0066

Please sign in to comment.