浏览代码

eviction fixes

Alex Meijer 1 周之前
父节点
当前提交
0d58790cbf
共有 2 个文件被更改,包括 10 次插入4 次删除
  1. 9 4
      core/pkg/opencost/opencost_codecs.go
  2. 1 0
      core/pkg/opencost/opencost_codecs_test.go

+ 9 - 4
core/pkg/opencost/opencost_codecs.go

@@ -476,6 +476,7 @@ type FileStringTableReader struct {
 	memoMaxBytes   int64
 	evictStop      chan struct{}
 	evictDone      chan struct{}
+	evictScratch   []memoEvictionCandidate
 }
 
 // NewFileStringTableFromBuffer reads exactly tl length-prefixed (uint16) string payloads from buffer
@@ -540,6 +541,7 @@ func NewFileStringTableReaderFrom(buffer *util.Buffer, dir string) StringTableRe
 		memo:         make([]atomic.Pointer[string], len(refs)),
 		memoHits:     make([]atomic.Uint64, len(refs)),
 		memoMaxBytes: BingenFileBackedStringTableMemoMaxBytes(),
+		evictScratch: make([]memoEvictionCandidate, len(refs)),
 	}
 	if reader.memoMaxBytes > 0 {
 		reader.startMemoEvictionLoop()
@@ -689,19 +691,22 @@ func (fstr *FileStringTableReader) evictLeastUsedMemoEntries(percent float64) {
 		return
 	}
 
-	candidates := make([]memoEvictionCandidate, 0, len(fstr.memo))
+	candidates := fstr.evictScratch
+	n := 0
 	for i := range fstr.memo {
 		if fstr.memo[i].Load() == nil {
 			continue
 		}
-		candidates = append(candidates, memoEvictionCandidate{
+		candidates[n] = memoEvictionCandidate{
 			idx:  i,
 			hits: fstr.memoHits[i].Load(),
-		})
+		}
+		n++
 	}
-	if len(candidates) == 0 {
+	if n == 0 {
 		return
 	}
+	candidates = candidates[:n]
 
 	evictCount := int(float64(len(candidates)) * percent)
 	if evictCount <= 0 {

+ 1 - 0
core/pkg/opencost/opencost_codecs_test.go

@@ -880,6 +880,7 @@ func TestFileStringTableReader_EvictLeastUsedMemoEntries(t *testing.T) {
 		},
 		memo:     make([]atomic.Pointer[string], 4),
 		memoHits: make([]atomic.Uint64, 4),
+		evictScratch: make([]memoEvictionCandidate, 4),
 	}
 
 	reader.memo[0].Store(&s1)