diff options
Diffstat (limited to 'data/transactions/verify/verifiedTxnCache.go')
-rw-r--r-- | data/transactions/verify/verifiedTxnCache.go | 19 |
1 files changed, 10 insertions, 9 deletions
diff --git a/data/transactions/verify/verifiedTxnCache.go b/data/transactions/verify/verifiedTxnCache.go index 44a230010..0c9bbe2c0 100644 --- a/data/transactions/verify/verifiedTxnCache.go +++ b/data/transactions/verify/verifiedTxnCache.go @@ -26,7 +26,6 @@ import ( "github.com/algorand/go-algorand/protocol" ) -const entriesPerBucket = 8179 // the default bucket size; a prime number could promote a lower hash collisions in case the hash function isn't perfect. const maxPinnedEntries = 500000 // VerifiedTxnCacheError helps to identify the errors of a cache error and diffrenciate these from a general verification errors. @@ -72,6 +71,8 @@ type VerifiedTransactionCache interface { // verifiedTransactionCache provides an implementation of the VerifiedTransactionCache interface type verifiedTransactionCache struct { + // Number of entries in each map (bucket). + entriesPerBucket int // bucketsLock is the lock for syncornizing the access to the cache bucketsLock deadlock.Mutex // buckets is the circular cache buckets buffer @@ -84,14 +85,14 @@ type verifiedTransactionCache struct { // MakeVerifiedTransactionCache creates an instance of verifiedTransactionCache and returns it. func MakeVerifiedTransactionCache(cacheSize int) VerifiedTransactionCache { - bucketsCount := 1 + (cacheSize / entriesPerBucket) impl := &verifiedTransactionCache{ - buckets: make([]map[transactions.Txid]*GroupContext, bucketsCount), - pinned: make(map[transactions.Txid]*GroupContext, cacheSize), - base: 0, + entriesPerBucket: (cacheSize + 1) / 2, + buckets: make([]map[transactions.Txid]*GroupContext, 3), + pinned: make(map[transactions.Txid]*GroupContext, cacheSize), + base: 0, } - for i := 0; i < bucketsCount; i++ { - impl.buckets[i] = make(map[transactions.Txid]*GroupContext, entriesPerBucket) + for i := 0; i < len(impl.buckets); i++ { + impl.buckets[i] = make(map[transactions.Txid]*GroupContext, impl.entriesPerBucket) } return impl } @@ -245,10 +246,10 @@ func (v *verifiedTransactionCache) Pin(txgroup []transactions.SignedTxn) (err er // add is the internal implementation of Add/AddPayset which adds a transaction group to the buffer. func (v *verifiedTransactionCache) add(txgroup []transactions.SignedTxn, groupCtx *GroupContext) { - if len(v.buckets[v.base])+len(txgroup) > entriesPerBucket { + if len(v.buckets[v.base])+len(txgroup) > v.entriesPerBucket { // move to the next bucket while deleting the content of the next bucket. v.base = (v.base + 1) % len(v.buckets) - v.buckets[v.base] = make(map[transactions.Txid]*GroupContext, entriesPerBucket) + v.buckets[v.base] = make(map[transactions.Txid]*GroupContext, v.entriesPerBucket) } currentBucket := v.buckets[v.base] for _, txn := range txgroup { |