From 6b88ca06f5e4afac4c3b7d55ce01e267ac3c1d62 Mon Sep 17 00:00:00 2001 From: maypok86 Date: Mon, 4 Mar 2024 12:37:13 +0300 Subject: [PATCH] [Chore] Refactor stats --- internal/stats/stats.go | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/internal/stats/stats.go b/internal/stats/stats.go index e3824fa..8bb8211 100644 --- a/internal/stats/stats.go +++ b/internal/stats/stats.go @@ -16,7 +16,6 @@ package stats import ( "sync/atomic" - "unsafe" "github.com/maypok86/otter/internal/xruntime" ) @@ -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. @@ -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. @@ -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. @@ -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. @@ -127,7 +126,7 @@ func (s *Stats) EvictedCost() int64 { return 0 } - return s.evictedCost.Load() + return atomic.LoadInt64(&s.evictedCost) } func (s *Stats) Clear() { @@ -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) }