lib/workingsetcache: reuse prev cache after its reset

This should reduce memory churn rate
This commit is contained in:
Aliaksandr Valialkin 2022-04-05 20:37:40 +03:00
parent cae61c85d4
commit 319e910897
No known key found for this signature in database
GPG key ID: A72BEC6CD3D0DED1

View file

@ -144,17 +144,12 @@ func (c *Cache) expirationWatcher(expireDuration time.Duration) {
c.mu.Unlock() c.mu.Unlock()
return return
} }
// Expire prev cache and create fresh curr cache with the same capacity. // Reset prev cache and swap it with the curr cache.
// Do not reuse prev cache, since it can occupy too big amounts of memory.
prev := c.prev.Load().(*fastcache.Cache) prev := c.prev.Load().(*fastcache.Cache)
prev.Reset()
curr := c.curr.Load().(*fastcache.Cache) curr := c.curr.Load().(*fastcache.Cache)
c.prev.Store(curr) c.prev.Store(curr)
// Use c.maxBytes/2 instead of cs.MaxBytesSize for creating new cache, prev.Reset()
// since cs.MaxBytesSize may not match c.maxBytes/2, so the created cache c.curr.Store(prev)
// couldn't be loaded from file with c.maxBytes/2 limit after saving with cs.MaxBytesSize size.
curr = fastcache.New(c.maxBytes / 2)
c.curr.Store(curr)
c.mu.Unlock() c.mu.Unlock()
} }
} }
@ -197,9 +192,9 @@ func (c *Cache) cacheSizeWatcher() {
c.mu.Lock() c.mu.Lock()
c.setMode(switching) c.setMode(switching)
prev := c.prev.Load().(*fastcache.Cache) prev := c.prev.Load().(*fastcache.Cache)
prev.Reset()
curr := c.curr.Load().(*fastcache.Cache) curr := c.curr.Load().(*fastcache.Cache)
c.prev.Store(curr) c.prev.Store(curr)
prev.Reset()
// use c.maxBytes instead of maxBytesSize*2 for creating new cache, since otherwise the created cache // use c.maxBytes instead of maxBytesSize*2 for creating new cache, since otherwise the created cache
// couldn't be loaded from file with c.maxBytes limit after saving with maxBytesSize*2 limit. // couldn't be loaded from file with c.maxBytes limit after saving with maxBytesSize*2 limit.
c.curr.Store(fastcache.New(c.maxBytes)) c.curr.Store(fastcache.New(c.maxBytes))
@ -222,8 +217,8 @@ func (c *Cache) cacheSizeWatcher() {
c.mu.Lock() c.mu.Lock()
c.setMode(whole) c.setMode(whole)
prev = c.prev.Load().(*fastcache.Cache) prev = c.prev.Load().(*fastcache.Cache)
prev.Reset()
c.prev.Store(fastcache.New(1024)) c.prev.Store(fastcache.New(1024))
prev.Reset()
c.mu.Unlock() c.mu.Unlock()
} }