From f33e813210308ac2042fed1893bc24b9e08356d2 Mon Sep 17 00:00:00 2001 From: Yiling-J Date: Thu, 24 Oct 2024 17:32:52 +0800 Subject: [PATCH] fix demote bug --- internal/tlfu.go | 19 +++++-------------- internal/tlfu_test.go | 34 +++++++++++++++++++++++++++++++++- 2 files changed, 38 insertions(+), 15 deletions(-) diff --git a/internal/tlfu.go b/internal/tlfu.go index e1f3933..597ce67 100644 --- a/internal/tlfu.go +++ b/internal/tlfu.go @@ -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() @@ -168,7 +160,7 @@ 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) } } @@ -176,11 +168,6 @@ 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) } @@ -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 { diff --git a/internal/tlfu_test.go b/internal/tlfu_test.go index 8c2bc3c..6aaedaa 100644 --- a/internal/tlfu_test.go +++ b/internal/tlfu_test.go @@ -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}, @@ -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{