From 35183b6dbae642d42971357d002c71ba5532ef5e Mon Sep 17 00:00:00 2001 From: Roman Khimov Date: Wed, 5 Feb 2020 17:46:19 +0300 Subject: [PATCH] mempool: reverse the order of sorted slice Chopping off the last element of the slice if way easier than doing it with the first one. --- pkg/core/mempool/mem_pool.go | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/pkg/core/mempool/mem_pool.go b/pkg/core/mempool/mem_pool.go index 326ce495f..24950168f 100644 --- a/pkg/core/mempool/mem_pool.go +++ b/pkg/core/mempool/mem_pool.go @@ -144,7 +144,7 @@ func (mp *Pool) Add(t *transaction.Transaction, fee Feer) error { mp.verifiedMap[t.Hash()] = pItem mp.verifiedTxes = append(mp.verifiedTxes, pItem) - sort.Sort(mp.verifiedTxes) + sort.Sort(sort.Reverse(mp.verifiedTxes)) mp.lock.Unlock() if mp.Count() > mp.capacity { @@ -201,13 +201,12 @@ func (mp *Pool) RemoveOverCapacity() { // minItem belongs to the mp.sortedLowPrioTxn slice. // The corresponding unsorted pool is is mp.unsortedTxn. delete(mp.verifiedMap, minItem.txn.Hash()) - mp.verifiedTxes = append(mp.verifiedTxes[:0], mp.verifiedTxes[1:]...) + mp.verifiedTxes = mp.verifiedTxes[:len(mp.verifiedTxes)-1] } else { // minItem belongs to the mp.unverifiedSortedLowPrioTxn slice. // The corresponding unsorted pool is is mp.unverifiedTxn. delete(mp.unverifiedMap, minItem.txn.Hash()) - mp.unverifiedTxes = append(mp.unverifiedTxes[:0], mp.unverifiedTxes[1:]...) - + mp.unverifiedTxes = mp.unverifiedTxes[:len(mp.unverifiedTxes)-1] } updateMempoolMetrics(len(mp.verifiedTxes), len(mp.unverifiedTxes)) mp.lock.Unlock() @@ -265,7 +264,7 @@ func min(sortedPool items) *item { if len(sortedPool) == 0 { return nil } - return sortedPool[0] + return sortedPool[len(sortedPool)-1] } // GetVerifiedTransactions returns a slice of Input from all the transactions in the memory pool