diff --git a/domain/consensus/utils/lrucache/lrucache.go b/domain/consensus/utils/lrucache/lrucache.go index bf54abcf0a..29ce760594 100644 --- a/domain/consensus/utils/lrucache/lrucache.go +++ b/domain/consensus/utils/lrucache/lrucache.go @@ -15,7 +15,7 @@ type LRUCache struct { func New(capacity int, preallocate bool) *LRUCache { var cache map[externalapi.DomainHash]interface{} if preallocate { - cache = make(map[externalapi.DomainHash]interface{}, capacity+1) + cache = make(map[externalapi.DomainHash]interface{}, capacity) } else { cache = make(map[externalapi.DomainHash]interface{}) } @@ -27,11 +27,10 @@ func New(capacity int, preallocate bool) *LRUCache { // Add adds an entry to the LRUCache func (c *LRUCache) Add(key *externalapi.DomainHash, value interface{}) { - c.cache[*key] = value - - if len(c.cache) > c.capacity { + if len(c.cache) >= c.capacity { c.evictRandom() } + c.cache[*key] = value } // Get returns the entry for the given key, or (nil, false) otherwise diff --git a/domain/consensus/utils/lrucacheghostdagdata/lrucacheghostdagdata.go b/domain/consensus/utils/lrucacheghostdagdata/lrucacheghostdagdata.go index 44eb770bbe..54cab3070f 100644 --- a/domain/consensus/utils/lrucacheghostdagdata/lrucacheghostdagdata.go +++ b/domain/consensus/utils/lrucacheghostdagdata/lrucacheghostdagdata.go @@ -25,7 +25,7 @@ type LRUCache struct { func New(capacity int, preallocate bool) *LRUCache { var cache map[lruKey]*externalapi.BlockGHOSTDAGData if preallocate { - cache = make(map[lruKey]*externalapi.BlockGHOSTDAGData, capacity+1) + cache = make(map[lruKey]*externalapi.BlockGHOSTDAGData, capacity) } else { cache = make(map[lruKey]*externalapi.BlockGHOSTDAGData) } @@ -37,12 +37,12 @@ func New(capacity int, preallocate bool) *LRUCache { // Add adds an entry to the LRUCache func (c *LRUCache) Add(blockHash *externalapi.DomainHash, isTrustedData bool, value *externalapi.BlockGHOSTDAGData) { - key := newKey(blockHash, isTrustedData) - c.cache[key] = value - - if len(c.cache) > c.capacity { + if len(c.cache) >= c.capacity { c.evictRandom() } + + key := newKey(blockHash, isTrustedData) + c.cache[key] = value } // Get returns the entry for the given key, or (nil, false) otherwise diff --git a/domain/consensus/utils/lrucachehashandwindowsizetoblockghostdagdatahashpairs/lrucachehashandwindowsizetoblockghostdagdatahashpairs.go b/domain/consensus/utils/lrucachehashandwindowsizetoblockghostdagdatahashpairs/lrucachehashandwindowsizetoblockghostdagdatahashpairs.go index ef2459c738..cb6019e59d 100644 --- a/domain/consensus/utils/lrucachehashandwindowsizetoblockghostdagdatahashpairs/lrucachehashandwindowsizetoblockghostdagdatahashpairs.go +++ b/domain/consensus/utils/lrucachehashandwindowsizetoblockghostdagdatahashpairs/lrucachehashandwindowsizetoblockghostdagdatahashpairs.go @@ -25,7 +25,7 @@ type LRUCache struct { func New(capacity int, preallocate bool) *LRUCache { var cache map[lruKey][]*externalapi.BlockGHOSTDAGDataHashPair if preallocate { - cache = make(map[lruKey][]*externalapi.BlockGHOSTDAGDataHashPair, capacity+1) + cache = make(map[lruKey][]*externalapi.BlockGHOSTDAGDataHashPair, capacity) } else { cache = make(map[lruKey][]*externalapi.BlockGHOSTDAGDataHashPair) } @@ -37,12 +37,12 @@ func New(capacity int, preallocate bool) *LRUCache { // Add adds an entry to the LRUCache func (c *LRUCache) Add(blockHash *externalapi.DomainHash, windowSize int, value []*externalapi.BlockGHOSTDAGDataHashPair) { - key := newKey(blockHash, windowSize) - c.cache[key] = value - - if len(c.cache) > c.capacity { + if len(c.cache) >= c.capacity { c.evictRandom() } + + key := newKey(blockHash, windowSize) + c.cache[key] = value } // Get returns the entry for the given key, or (nil, false) otherwise diff --git a/domain/consensus/utils/lrucachehashpairtoblockghostdagdatahashpair/lrucachehashpairtoblockghostdagdatahashpair.go b/domain/consensus/utils/lrucachehashpairtoblockghostdagdatahashpair/lrucachehashpairtoblockghostdagdatahashpair.go index 674a4c6d13..ea035722ec 100644 --- a/domain/consensus/utils/lrucachehashpairtoblockghostdagdatahashpair/lrucachehashpairtoblockghostdagdatahashpair.go +++ b/domain/consensus/utils/lrucachehashpairtoblockghostdagdatahashpair/lrucachehashpairtoblockghostdagdatahashpair.go @@ -25,7 +25,7 @@ type LRUCache struct { func New(capacity int, preallocate bool) *LRUCache { var cache map[lruKey]*externalapi.BlockGHOSTDAGDataHashPair if preallocate { - cache = make(map[lruKey]*externalapi.BlockGHOSTDAGDataHashPair, capacity+1) + cache = make(map[lruKey]*externalapi.BlockGHOSTDAGDataHashPair, capacity) } else { cache = make(map[lruKey]*externalapi.BlockGHOSTDAGDataHashPair) } @@ -37,12 +37,12 @@ func New(capacity int, preallocate bool) *LRUCache { // Add adds an entry to the LRUCache func (c *LRUCache) Add(blockHash *externalapi.DomainHash, index uint64, value *externalapi.BlockGHOSTDAGDataHashPair) { - key := newKey(blockHash, index) - c.cache[key] = value - - if len(c.cache) > c.capacity { + if len(c.cache) >= c.capacity { c.evictRandom() } + + key := newKey(blockHash, index) + c.cache[key] = value } // Get returns the entry for the given key, or (nil, false) otherwise diff --git a/domain/consensus/utils/lrucacheuint64tohash/lrucacheuint64tohash.go b/domain/consensus/utils/lrucacheuint64tohash/lrucacheuint64tohash.go index 7df4b1e0f1..6b60c38c5b 100644 --- a/domain/consensus/utils/lrucacheuint64tohash/lrucacheuint64tohash.go +++ b/domain/consensus/utils/lrucacheuint64tohash/lrucacheuint64tohash.go @@ -13,7 +13,7 @@ type LRUCache struct { func New(capacity int, preallocate bool) *LRUCache { var cache map[uint64]*externalapi.DomainHash if preallocate { - cache = make(map[uint64]*externalapi.DomainHash, capacity+1) + cache = make(map[uint64]*externalapi.DomainHash, capacity) } else { cache = make(map[uint64]*externalapi.DomainHash) } @@ -25,11 +25,11 @@ func New(capacity int, preallocate bool) *LRUCache { // Add adds an entry to the LRUCache func (c *LRUCache) Add(key uint64, value *externalapi.DomainHash) { - c.cache[key] = value - - if len(c.cache) > c.capacity { + if len(c.cache) >= c.capacity { c.evictRandom() } + + c.cache[key] = value } // Get returns the entry for the given key, or (nil, false) otherwise