Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid remove of just added item from LRU cache #2057

Open
wants to merge 1 commit into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 3 additions & 4 deletions domain/consensus/utils/lrucache/lrucache.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{})
}
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand All @@ -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
Expand Down