Просмотр исходного кода

Reduce LRU Complexity based on real-world usage.

Matt Bolt 1 месяц назад
Родитель
Сommit
2b75616efb
2 измененных файлов с 23 добавлено и 29 удалено
  1. 9 15
      core/pkg/util/stringutil/lrubank.go
  2. 14 14
      core/pkg/util/stringutil/lrubank_test.go

+ 9 - 15
core/pkg/util/stringutil/lrubank.go

@@ -10,20 +10,14 @@ type lruEntry struct {
 	value string
 	used  time.Time
 }
-
-type heapEntry struct {
-	*lruEntry
-	key string
-}
-
-type maxHeap []*heapEntry
+type maxHeap []*lruEntry
 
 func (h maxHeap) Len() int           { return len(h) }
 func (h maxHeap) Less(i, j int) bool { return h[i].used.After(h[j].used) } // newer = "larger"
 func (h maxHeap) Swap(i, j int)      { h[i], h[j] = h[j], h[i] }
 
 func (h *maxHeap) Push(x any) {
-	*h = append(*h, x.(*heapEntry))
+	*h = append(*h, x.(*lruEntry))
 }
 
 func (h *maxHeap) Pop() any {
@@ -34,9 +28,9 @@ func (h *maxHeap) Pop() any {
 	return x
 }
 
-func nOldest(arr []*heapEntry, n int) []*heapEntry {
+func nOldest(arr []*lruEntry, n int) []*lruEntry {
 	if n <= 0 {
-		return []*heapEntry{}
+		return []*lruEntry{}
 	}
 
 	if n >= len(arr) {
@@ -54,7 +48,7 @@ func nOldest(arr []*heapEntry, n int) []*heapEntry {
 		}
 	}
 
-	return []*heapEntry(h)
+	return []*lruEntry(h)
 }
 
 type lruStringBank struct {
@@ -85,14 +79,14 @@ func NewLruStringBank(capacity int, evictionInterval time.Duration) StringBank {
 			}
 
 			// we collect a list of all lru entries so we can max heap the first n elements
-			arr := make([]*heapEntry, 0, len(bank.m))
-			for k, v := range bank.m {
-				arr = append(arr, &heapEntry{key: k, lruEntry: v})
+			arr := make([]*lruEntry, 0, len(bank.m))
+			for _, v := range bank.m {
+				arr = append(arr, v)
 			}
 
 			oldest := nOldest(arr, len(bank.m)-capacity)
 			for _, old := range oldest {
-				delete(bank.m, old.key)
+				delete(bank.m, old.value)
 			}
 			bank.lock.Unlock()
 		}

+ 14 - 14
core/pkg/util/stringutil/lrubank_test.go

@@ -233,12 +233,12 @@ func TestClear_PreviousKeysGone(t *testing.T) {
 
 func TestNOldest_ReturnsCorrectCount(t *testing.T) {
 	now := time.Now()
-	entries := []*heapEntry{
-		{key: "a", lruEntry: &lruEntry{value: "a", used: now.Add(-4 * time.Second)}},
-		{key: "b", lruEntry: &lruEntry{value: "b", used: now.Add(-3 * time.Second)}},
-		{key: "c", lruEntry: &lruEntry{value: "c", used: now.Add(-2 * time.Second)}},
-		{key: "d", lruEntry: &lruEntry{value: "d", used: now.Add(-1 * time.Second)}},
-		{key: "e", lruEntry: &lruEntry{value: "e", used: now}},
+	entries := []*lruEntry{
+		{value: "a", used: now.Add(-4 * time.Second)},
+		{value: "b", used: now.Add(-3 * time.Second)},
+		{value: "c", used: now.Add(-2 * time.Second)},
+		{value: "d", used: now.Add(-1 * time.Second)},
+		{value: "e", used: now},
 	}
 
 	oldest := nOldest(entries, 2)
@@ -259,9 +259,9 @@ func TestNOldest_ReturnsCorrectCount(t *testing.T) {
 
 func TestNOldest_NGreaterThanLen(t *testing.T) {
 	now := time.Now()
-	entries := []*heapEntry{
-		{key: "x", lruEntry: &lruEntry{value: "x", used: now}},
-		{key: "y", lruEntry: &lruEntry{value: "y", used: now.Add(-time.Second)}},
+	entries := []*lruEntry{
+		{value: "x", used: now},
+		{value: "y", used: now.Add(-time.Second)},
 	}
 
 	result := nOldest(entries, 10)
@@ -272,9 +272,9 @@ func TestNOldest_NGreaterThanLen(t *testing.T) {
 
 func TestNOldest_NEqualsLen(t *testing.T) {
 	now := time.Now()
-	entries := []*heapEntry{
-		{key: "x", lruEntry: &lruEntry{value: "x", used: now}},
-		{key: "y", lruEntry: &lruEntry{value: "y", used: now.Add(-time.Second)}},
+	entries := []*lruEntry{
+		{value: "x", used: now},
+		{value: "y", used: now.Add(-time.Second)},
 	}
 
 	result := nOldest(entries, 2)
@@ -285,8 +285,8 @@ func TestNOldest_NEqualsLen(t *testing.T) {
 
 func TestNOldest_NIsZero(t *testing.T) {
 	now := time.Now()
-	entries := []*heapEntry{
-		{key: "x", lruEntry: &lruEntry{value: "x", used: now}},
+	entries := []*lruEntry{
+		{value: "x", used: now},
 	}
 
 	result := nOldest(entries, 0)