From fed9bbe218afb5536f8660e6c0b6be2a70ae983c Mon Sep 17 00:00:00 2001 From: Justin Harms Date: Wed, 22 Jan 2025 19:35:17 -0600 Subject: [PATCH] fix: go back to the old averaging --- steamcache/avgcachestate/avgcachestate.go | 31 +++++++++-------------- 1 file changed, 12 insertions(+), 19 deletions(-) diff --git a/steamcache/avgcachestate/avgcachestate.go b/steamcache/avgcachestate/avgcachestate.go index 8d16237..ce51f72 100644 --- a/steamcache/avgcachestate/avgcachestate.go +++ b/steamcache/avgcachestate/avgcachestate.go @@ -7,11 +7,9 @@ import ( // AvgCacheState is a cache state that averages the last N cache states. type AvgCacheState struct { - size int - hits int - misses int - avgs []cachestate.CacheState - mu sync.Mutex + size int + avgs []cachestate.CacheState + mu sync.Mutex } // New creates a new average cache state with the given size. @@ -44,21 +42,8 @@ func (a *AvgCacheState) Add(cs cachestate.CacheState) { a.avgs = append(a.avgs, cs) if len(a.avgs) > a.size { - switch a.avgs[0] { - case cachestate.CacheStateHit: - a.hits-- - case cachestate.CacheStateMiss: - a.misses-- - } a.avgs = a.avgs[1:] } - - switch cs { - case cachestate.CacheStateHit: - a.hits++ - case cachestate.CacheStateMiss: - a.misses++ - } } // Avg returns the average cache state. @@ -66,5 +51,13 @@ func (a *AvgCacheState) Avg() float64 { a.mu.Lock() defer a.mu.Unlock() - return float64(a.hits) / (float64(a.misses) + 0.0000001) + var hits int + + for _, cs := range a.avgs { + if cs == cachestate.CacheStateHit { + hits++ + } + } + + return float64(hits) / float64(len(a.avgs)) }