Skip to content

Commit

Permalink
add count to list, fix cm wrong capacity (#22)
Browse files Browse the repository at this point in the history
  • Loading branch information
Yiling-J authored Jun 9, 2023
1 parent c516ee0 commit b96bbbb
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 2 deletions.
5 changes: 4 additions & 1 deletion internal/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ const (
// The zero value for List is an empty list ready to use.
type List[K comparable, V any] struct {
root Entry[K, V] // sentinel list element, only &root, root.prev, and root.next are used
len int // current list length excluding (this) sentinel element
len int // current list length(sum of costs) excluding (this) sentinel element
count int // count of entries in list
capacity uint
bounded bool
listType uint8 // 1 tinylfu list, 2 timerwheel list
Expand Down Expand Up @@ -97,6 +98,7 @@ func (l *List[K, V]) insert(e, at *Entry[K, V]) *Entry[K, V] {
e.next(l.listType).setPrev(e, l.listType)
if l.bounded {
l.len += int(e.cost.Load())
l.count += 1
}
return evicted
}
Expand All @@ -122,6 +124,7 @@ func (l *List[K, V]) remove(e *Entry[K, V]) {
}
if l.bounded {
l.len -= int(e.cost.Load())
l.count -= 1
}
}

Expand Down
18 changes: 18 additions & 0 deletions internal/list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,24 @@ func TestList(t *testing.T) {

}

func TestListCountCost(t *testing.T) {
l := NewList[string, string](100, LIST_PROBATION)
require.Equal(t, uint(100), l.capacity)
require.Equal(t, LIST_PROBATION, l.listType)
for i := 0; i < 5; i++ {
evicted := l.PushFront(NewEntry(fmt.Sprintf("%d", i), "", 20, 0))
require.Nil(t, evicted)
}
require.Equal(t, 100, l.len)
require.Equal(t, 5, l.count)
for i := 0; i < 3; i++ {
entry := l.PopTail()
require.NotNil(t, entry)
}
require.Equal(t, 40, l.len)
require.Equal(t, 2, l.count)
}

func TestWheelList(t *testing.T) {
l := NewList[string, string](5, WHEEL_LIST)
require.Equal(t, uint(5), l.capacity)
Expand Down
2 changes: 1 addition & 1 deletion internal/tlfu.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ func (t *TinyLfu[K, V]) Set(entry *Entry[K, V]) *Entry[K, V] {
return entry
}
} else {
count := t.slru.probation.len + t.slru.protected.len
count := t.slru.probation.count + t.slru.protected.count
t.sketch.ensureCapacity(uint(count + count/100))
}
evicted := t.slru.insert(entry)
Expand Down

0 comments on commit b96bbbb

Please sign in to comment.