Skip to content

Commit

Permalink
[Chore] Refactor stats
Browse files Browse the repository at this point in the history
  • Loading branch information
maypok86 committed Mar 4, 2024
1 parent 424d52b commit 6b88ca0
Showing 1 changed file with 9 additions and 10 deletions.
19 changes: 9 additions & 10 deletions internal/stats/stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ package stats

import (
"sync/atomic"
"unsafe"

"github.com/maypok86/otter/internal/xruntime"
)
Expand All @@ -26,9 +25,9 @@ type Stats struct {
hits *counter
misses *counter
rejectedSets *counter
evictedCountersPadding [xruntime.CacheLineSize - 2*unsafe.Sizeof(atomic.Int64{})]byte
evictedCount atomic.Int64
evictedCost atomic.Int64
evictedCountersPadding [xruntime.CacheLineSize - 16]byte
evictedCount int64
evictedCost int64
}

// New creates a new Stats collector.
Expand Down Expand Up @@ -100,7 +99,7 @@ func (s *Stats) IncEvictedCount() {
return
}

s.evictedCount.Add(1)
atomic.AddInt64(&s.evictedCount, 1)
}

// EvictedCount returns the number of evicted entries.
Expand All @@ -109,7 +108,7 @@ func (s *Stats) EvictedCount() int64 {
return 0
}

return s.evictedCount.Load()
return atomic.LoadInt64(&s.evictedCount)
}

// AddEvictedCost adds cost to the evictedCost counter.
Expand All @@ -118,7 +117,7 @@ func (s *Stats) AddEvictedCost(cost uint32) {
return
}

s.evictedCost.Add(int64(cost))
atomic.AddInt64(&s.evictedCost, int64(cost))
}

// EvictedCost returns the sum of costs of evicted entries.
Expand All @@ -127,7 +126,7 @@ func (s *Stats) EvictedCost() int64 {
return 0
}

return s.evictedCost.Load()
return atomic.LoadInt64(&s.evictedCost)
}

func (s *Stats) Clear() {
Expand All @@ -138,6 +137,6 @@ func (s *Stats) Clear() {
s.hits.reset()
s.misses.reset()
s.rejectedSets.reset()
s.evictedCount.Store(0)
s.evictedCost.Store(0)
atomic.StoreInt64(&s.evictedCount, 0)
atomic.StoreInt64(&s.evictedCost, 0)
}

0 comments on commit 6b88ca0

Please sign in to comment.