Skip to content

Commit 4f14d90

Browse files
committed
[disk] avoid looking for items in the LRU index twice
1 parent c6cbe2f commit 4f14d90

File tree

3 files changed

+13
-7
lines changed

3 files changed

+13
-7
lines changed

cache/disk/disk.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -510,7 +510,7 @@ func (c *diskCache) availableOrTryProxy(kind cache.EntryKind, hash string, size
510510
_ = f.Close()
511511

512512
c.mu.Lock()
513-
c.lru.Remove(key)
513+
c.lru.RemoveElement(listElem)
514514
c.mu.Unlock()
515515
} else {
516516
return rc, item.size, false, nil

cache/disk/lru.go

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -241,14 +241,20 @@ func (c *SizedLRU) Get(key string) (lruItem, *list.Element) {
241241
return lruItem{}, nil
242242
}
243243

244-
// Remove removes a (key, value) from the cache
245-
func (c *SizedLRU) Remove(key string) {
246-
if ele, hit := c.cache[key]; hit {
247-
c.removeElement(ele)
244+
// Remove removes a (key, value) from the cache.
245+
func (c *SizedLRU) RemoveKey(key string) {
246+
if elem, hit := c.cache[key]; hit {
247+
c.removeElement(elem)
248248
c.gaugeCacheLogicalBytes.Set(float64(c.uncompressedSize))
249249
}
250250
}
251251

252+
// Remove a *list.Element from the cache.
253+
func (c *SizedLRU) RemoveElement(elem *list.Element) {
254+
c.removeElement(elem)
255+
c.gaugeCacheLogicalBytes.Set(float64(c.uncompressedSize))
256+
}
257+
252258
// Len returns the number of items in the cache
253259
func (c *SizedLRU) Len() int {
254260
return len(c.cache)

cache/disk/lru_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ func TestBasics(t *testing.T) {
5858
checkSizeAndNumItems(t, &lru, BlockSize, 1)
5959

6060
// Remove the item
61-
lru.Remove(aKey)
61+
lru.RemoveKey(aKey)
6262
checkSizeAndNumItems(t, &lru, 0, 0)
6363
}
6464

@@ -178,7 +178,7 @@ func TestReserveAtEvictionQueueLimit(t *testing.T) {
178178
testutils.AssertEquals(t, BlockSize*2, lru.totalDiskSizePeak)
179179

180180
// Move large item into eviction queue.
181-
lru.Remove(blockSize2Key)
181+
lru.RemoveKey(blockSize2Key)
182182
testutils.AssertEquals(t, BlockSize*2, lru.queuedEvictionsSize.Load())
183183

184184
// Accept reservation since not exceeding maxSizeHardLimit.

0 commit comments

Comments
 (0)