prysm icon indicating copy to clipboard operation
prysm copied to clipboard

Remove enclosing locks from lru caches

Open potuz opened this issue 11 months ago • 0 comments

We have many caches that have the following general form,

// BalanceCache is a struct with 1 LRU cache for looking up balance by epoch.
type BalanceCache struct {
	cache *lru.Cache
	lock  sync.RWMutex
}

Then we call functions wrapping the lru Add as follows:

// AddTotalEffectiveBalance adds a new total effective balance entry for current balance for state `st` into the cache.
func (c *BalanceCache) AddTotalEffectiveBalance(st state.ReadOnlyBeaconState, balance uint64) error {
	key, err := balanceCacheKey(st)
	if err != nil {
		return err
	}

	c.lock.Lock()
	defer c.lock.Unlock()

	_ = c.cache.Add(key, balance)
	return nil
}

This is wrong as the enclosing lru already has its own lock and Add locks on calls. The whole structure BalanceCache is useless and should be just a type alias to the lru cache.

potuz avatar Mar 11 '24 19:03 potuz