Skip to content

Commit

Permalink
fix demote bug
Browse files Browse the repository at this point in the history
  • Loading branch information
Yiling-J committed Oct 24, 2024
1 parent ae102f1 commit f33e813
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 15 deletions.
19 changes: 5 additions & 14 deletions internal/tlfu.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,14 +96,6 @@ func (t *TinyLfu[K, V]) decreaseWindow(amount int) int {

func (t *TinyLfu[K, V]) resizeWindow() {

if t.amount == 0 {
// when processing read buffer,
// probation entries might be promoted to protected,
// and protected might exceed it's cap
t.demoteFromProtected()
return
}

t.window.capacity += uint(t.amount)
t.slru.protected.capacity -= uint(t.amount)
t.demoteFromProtected()
Expand Down Expand Up @@ -168,19 +160,14 @@ func (t *TinyLfu[K, V]) climb() {

// decrease window, min window size is 1
if t.amount < 0 && -t.amount > int(t.window.capacity-1) {
t.amount = -int(t.window.capacity)
t.amount = -int(t.window.capacity - 1)
}
}

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

t.weightedSize += uint(entry.policyWeight)

// try finish unfinished climb first
if t.amount != 0 && t.counter&15 == 0 {
t.resizeWindow()
}

if entry.meta.prev == nil {
t.window.PushFront(entry)
}
Expand All @@ -195,6 +182,10 @@ func (t *TinyLfu[K, V]) Access(item ReadBufItem[K, V]) {
t.counter = 0
}

if t.counter&15 == 0 {
t.demoteFromProtected()
}

if entry := item.entry; entry != nil {
t.sketch.Add(item.hash)
if entry.meta.prev != nil {
Expand Down
34 changes: 33 additions & 1 deletion internal/tlfu_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ var weightTests = []testCase{
"11/10:5/3/2/1/0:4",
},
{
"update protected, demote",
"update protected, demote ",
[]testEvent{
{TestEventRemove, 14, 1},
{TestEventRemove, 13, 1},
Expand All @@ -175,6 +175,38 @@ var weightTests = []testCase{
// protected cap exceed, demote
"11/10:4/5/3/2/1/0:",
},
{
"update protected, demote not run",
[]testEvent{
{TestEventRemove, 14, 1},
{TestEventRemove, 13, 1},
{TestEventRemove, 12, 1},
{TestEventRemove, 9, 1},
{TestEventRemove, 8, 1},
{TestEventRemove, 7, 1},
{TestEventRemove, 6, 1},
{TestEventGet, 4, 1},
{TestEventUpdate, 4, 5},
{TestEventGet, 4, 1},
},
"11/10:5/3/2/1/0:4",
},
{
"update protected, demote auto run",
[]testEvent{
{TestEventRemove, 14, 1},
{TestEventRemove, 13, 1},
{TestEventRemove, 12, 1},
{TestEventRemove, 9, 1},
{TestEventRemove, 8, 1},
{TestEventRemove, 7, 1},
{TestEventRemove, 6, 1},
{TestEventGet, 4, 1},
{TestEventUpdate, 4, 5},
{TestEventGet, 3, 20},
},
"11/10:4/5/2/1/0:3",
},
{
"window too large",
[]testEvent{
Expand Down

0 comments on commit f33e813

Please sign in to comment.