Skip to content

Commit a38f3af

Browse files
authored
feat: use entry pool (#48)
1 parent 33cf53c commit a38f3af

File tree

1 file changed

+15
-5
lines changed

1 file changed

+15
-5
lines changed

pkg/grib2/cache/store.go

+15-5
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,11 @@ type Store interface {
1313

1414
// LRU Cache 实现
1515
type lruStore struct {
16-
mu sync.RWMutex
17-
capacity int
18-
cache map[int]*list.Element
19-
lru *list.List
16+
mu sync.RWMutex
17+
capacity int
18+
cache map[int]*list.Element
19+
lru *list.List
20+
entryPool sync.Pool
2021
}
2122

2223
type entry struct {
@@ -29,6 +30,11 @@ func NewLRUStore(capacity int) Store {
2930
capacity: capacity,
3031
cache: make(map[int]*list.Element, capacity),
3132
lru: list.New(),
33+
entryPool: sync.Pool{
34+
New: func() any {
35+
return &entry{}
36+
},
37+
},
3238
}
3339
}
3440

@@ -60,11 +66,15 @@ func (l *lruStore) Set(ctx context.Context, key int, value float32) {
6066
if oldest != nil {
6167
delete(l.cache, oldest.Value.(*entry).key)
6268
l.lru.Remove(oldest)
69+
l.entryPool.Put(oldest.Value)
6370
}
6471
}
6572

6673
// 添加新条目
67-
elem := l.lru.PushFront(&entry{key: key, value: value})
74+
newEntry := l.entryPool.Get().(*entry)
75+
newEntry.key = key
76+
newEntry.value = value
77+
elem := l.lru.PushFront(newEntry)
6878
l.cache[key] = elem
6979
}
7080

0 commit comments

Comments
 (0)