Skip to content

Commit

Permalink
fastcache.go: re-create the bucket.m map inside cleanLocked() in orde…
Browse files Browse the repository at this point in the history
…r to reduce map fragmentation

Hopefully, this may help reducing memory usage and GC pressure for long-running processes, which use fastcache.

See VictoriaMetrics/VictoriaMetrics#5379
  • Loading branch information
valyala committed Nov 24, 2023
1 parent 80e8ba2 commit 959e53c
Showing 1 changed file with 17 additions and 3 deletions.
20 changes: 17 additions & 3 deletions fastcache.go
Original file line number Diff line number Diff line change
Expand Up @@ -272,13 +272,27 @@ func (b *bucket) cleanLocked() {
bGen := b.gen & ((1 << genSizeBits) - 1)
bIdx := b.idx
bm := b.m
for k, v := range bm {
newItems := 0
for _, v := range bm {
gen := v >> bucketSizeBits
idx := v & ((1 << bucketSizeBits) - 1)
if (gen+1 == bGen || gen == maxGen && bGen == 1) && idx >= bIdx || gen == bGen && idx < bIdx {
continue
newItems++
}
delete(bm, k)
}
if newItems < len(bm) {
// Re-create b.m with valid items, which weren't expired yet instead of deleting expired items from b.m.
// This should reduce memory fragmentation and the number Go objects behind b.m.
// See https://github.com/VictoriaMetrics/VictoriaMetrics/issues/5379
bmNew := make(map[uint64]uint64, newItems)
for k, v := range bm {
gen := v >> bucketSizeBits
idx := v & ((1 << bucketSizeBits) - 1)
if (gen+1 == bGen || gen == maxGen && bGen == 1) && idx >= bIdx || gen == bGen && idx < bIdx {
bmNew[k] = v
}
}
b.m = bmNew
}
}

Expand Down

0 comments on commit 959e53c

Please sign in to comment.