Skip to content

Commit

Permalink
Merge pull request #32 from vimeo/atomic_bytecount
Browse files Browse the repository at this point in the history
marginal improvement: atomic byte-counts
  • Loading branch information
dfinkel authored Aug 4, 2022
2 parents de18a11 + 4e0934e commit e298fc0
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 11 deletions.
8 changes: 3 additions & 5 deletions galaxycache.go
Original file line number Diff line number Diff line change
Expand Up @@ -704,7 +704,7 @@ func (c *cache) stats() CacheStats {
c.mu.Lock()
defer c.mu.Unlock()
return CacheStats{
Bytes: c.nbytes,
Bytes: c.nbytes.Get(),
Items: c.itemsLocked(),
Gets: c.nget,
Hits: c.nhit,
Expand Down Expand Up @@ -732,7 +732,7 @@ func (c *cache) add(key string, value valWithStat) {
c.mu.Lock()
defer c.mu.Unlock()
c.lru.Add(key, value)
c.nbytes += int64(len(key)) + value.size()
c.nbytes.Add(int64(len(key)) + value.size())
}

func (c *cache) removeOldest() {
Expand All @@ -745,9 +745,7 @@ func (c *cache) removeOldest() {
}

func (c *cache) bytes() int64 {
c.mu.Lock()
defer c.mu.Unlock()
return c.nbytes
return c.nbytes.Get()
}

func (c *cache) items() int64 {
Expand Down
4 changes: 2 additions & 2 deletions galaxycache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -388,8 +388,8 @@ func TestNoDedup(t *testing.T) {
testKStats := keyStats{dQPS: windowedAvgQPS{}}
testvws := g.newValWithStat([]byte(testval), &testKStats)
wantBytes := int64(len(testkey)) + testvws.size()
if g.mainCache.nbytes != wantBytes {
t.Errorf("cache has %d bytes, want %d", g.mainCache.nbytes, wantBytes)
if g.mainCache.nbytes.Get() != wantBytes {
t.Errorf("cache has %d bytes, want %d", g.mainCache.nbytes.Get(), wantBytes)
}
}

Expand Down
4 changes: 2 additions & 2 deletions typed_caches.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func (c *candidateCache) get(key string) (*keyStats, bool) {
type cache struct {
mu sync.Mutex
lru *lru.TypedCache[string, valWithStat]
nbytes int64 // of all keys and values
nbytes AtomicInt // of all keys and values
nhit, nget int64
nevict int64 // number of evictions
ctype CacheType
Expand All @@ -69,7 +69,7 @@ func newCache(kind CacheType) cache {
func (c *cache) setLRUOnEvicted(f func(key string, kStats *keyStats)) {
c.lru.OnEvicted = func(key string, value valWithStat) {
val := value
c.nbytes -= int64(len(key)) + val.size()
c.nbytes.Add(-(int64(len(key)) + val.size()))
c.nevict++
if f != nil {
f(key, val.stats)
Expand Down
4 changes: 2 additions & 2 deletions untyped_caches.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func (c *candidateCache) get(key string) (*keyStats, bool) {
type cache struct {
mu sync.Mutex
lru *lru.Cache
nbytes int64 // of all keys and values
nbytes AtomicInt // of all keys and values
nhit, nget int64
nevict int64 // number of evictions
ctype CacheType
Expand All @@ -73,7 +73,7 @@ func newCache(kind CacheType) cache {
func (c *cache) setLRUOnEvicted(f func(key string, kStats *keyStats)) {
c.lru.OnEvicted = func(key lru.Key, value interface{}) {
val := value.(valWithStat)
c.nbytes -= int64(len(key.(string))) + val.size()
c.nbytes.Add(-(int64(len(key.(string))) + val.size()))
c.nevict++
if f != nil {
f(key.(string), val.stats)
Expand Down

0 comments on commit e298fc0

Please sign in to comment.