mempool: store only pointer in the verifiedMap

It's only used for presence checks, there is no need for metadata here.
This commit is contained in:
Roman Khimov 2020-08-31 23:57:10 +03:00
parent a2d9b89964
commit fe1f1d19be

View file

@ -46,7 +46,7 @@ type utilityBalanceAndFees struct {
// Pool stores the unconfirms transactions. // Pool stores the unconfirms transactions.
type Pool struct { type Pool struct {
lock sync.RWMutex lock sync.RWMutex
verifiedMap map[util.Uint256]item verifiedMap map[util.Uint256]*transaction.Transaction
verifiedTxes items verifiedTxes items
fees map[util.Uint160]utilityBalanceAndFees fees map[util.Uint160]utilityBalanceAndFees
@ -156,7 +156,7 @@ func (mp *Pool) Add(t *transaction.Transaction, fee Feer) error {
return err return err
} }
mp.verifiedMap[t.Hash()] = pItem mp.verifiedMap[t.Hash()] = t
// Insert into sorted array (from max to min, that could also be done // Insert into sorted array (from max to min, that could also be done
// using sort.Sort(sort.Reverse()), but it incurs more overhead. Notice // using sort.Sort(sort.Reverse()), but it incurs more overhead. Notice
// also that we're searching for position that is strictly more // also that we're searching for position that is strictly more
@ -197,7 +197,7 @@ func (mp *Pool) Add(t *transaction.Transaction, fee Feer) error {
// nothing if it doesn't). // nothing if it doesn't).
func (mp *Pool) Remove(hash util.Uint256) { func (mp *Pool) Remove(hash util.Uint256) {
mp.lock.Lock() mp.lock.Lock()
if it, ok := mp.verifiedMap[hash]; ok { if tx, ok := mp.verifiedMap[hash]; ok {
var num int var num int
delete(mp.verifiedMap, hash) delete(mp.verifiedMap, hash)
for num = range mp.verifiedTxes { for num = range mp.verifiedTxes {
@ -210,9 +210,9 @@ func (mp *Pool) Remove(hash util.Uint256) {
} else if num == len(mp.verifiedTxes)-1 { } else if num == len(mp.verifiedTxes)-1 {
mp.verifiedTxes = mp.verifiedTxes[:num] mp.verifiedTxes = mp.verifiedTxes[:num]
} }
senderFee := mp.fees[it.txn.Sender()] senderFee := mp.fees[tx.Sender()]
senderFee.feeSum.Sub(senderFee.feeSum, big.NewInt(it.txn.SystemFee+it.txn.NetworkFee)) senderFee.feeSum.Sub(senderFee.feeSum, big.NewInt(tx.SystemFee+tx.NetworkFee))
mp.fees[it.txn.Sender()] = senderFee mp.fees[tx.Sender()] = senderFee
} }
updateMempoolMetrics(len(mp.verifiedTxes)) updateMempoolMetrics(len(mp.verifiedTxes))
mp.lock.Unlock() mp.lock.Unlock()
@ -261,7 +261,7 @@ func (mp *Pool) checkPolicy(tx *transaction.Transaction, policyChanged bool) boo
// New returns a new Pool struct. // New returns a new Pool struct.
func New(capacity int) *Pool { func New(capacity int) *Pool {
return &Pool{ return &Pool{
verifiedMap: make(map[util.Uint256]item), verifiedMap: make(map[util.Uint256]*transaction.Transaction),
verifiedTxes: make([]item, 0, capacity), verifiedTxes: make([]item, 0, capacity),
capacity: capacity, capacity: capacity,
fees: make(map[util.Uint160]utilityBalanceAndFees), fees: make(map[util.Uint160]utilityBalanceAndFees),
@ -272,8 +272,8 @@ func New(capacity int) *Pool {
func (mp *Pool) TryGetValue(hash util.Uint256) (*transaction.Transaction, bool) { func (mp *Pool) TryGetValue(hash util.Uint256) (*transaction.Transaction, bool) {
mp.lock.RLock() mp.lock.RLock()
defer mp.lock.RUnlock() defer mp.lock.RUnlock()
if pItem, ok := mp.verifiedMap[hash]; ok { if tx, ok := mp.verifiedMap[hash]; ok {
return pItem.txn, ok return tx, ok
} }
return nil, false return nil, false