From 325bea3fa9d8641711f936bd8b926557dd8932ad Mon Sep 17 00:00:00 2001 From: Roman Khimov Date: Wed, 5 Feb 2020 14:34:58 +0300 Subject: [PATCH] mempool: cache Feer invocation results in the item They shouldn't depend on the chain state and for the same transaction they should always produce the same result. Thus, it makes no sense recalculating them over and over again. --- pkg/core/mempool/mem_pool.go | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/pkg/core/mempool/mem_pool.go b/pkg/core/mempool/mem_pool.go index 13e6689e1..3be8e47f9 100644 --- a/pkg/core/mempool/mem_pool.go +++ b/pkg/core/mempool/mem_pool.go @@ -25,9 +25,11 @@ var ( // item represents a transaction in the the Memory pool. type item struct { - txn *transaction.Transaction - timeStamp time.Time - fee Feer + txn *transaction.Transaction + timeStamp time.Time + perByteFee util.Fixed8 + netFee util.Fixed8 + isLowPrio bool } // items is a slice of item. @@ -59,7 +61,7 @@ func (p item) CompareTo(otherP *item) int { return 1 } - if p.fee.IsLowPriority(p.txn) && p.fee.IsLowPriority(otherP.txn) { + if p.isLowPrio && otherP.isLowPrio { thisIsClaimTx := p.txn.Type == transaction.ClaimType otherIsClaimTx := otherP.txn.Type == transaction.ClaimType @@ -74,15 +76,11 @@ func (p item) CompareTo(otherP *item) int { } // Fees sorted ascending. - pFPB := p.fee.FeePerByte(p.txn) - otherFPB := p.fee.FeePerByte(otherP.txn) - if ret := pFPB.CompareTo(otherFPB); ret != 0 { + if ret := p.perByteFee.CompareTo(otherP.perByteFee); ret != 0 { return ret } - pNF := p.fee.NetworkFee(p.txn) - otherNF := p.fee.NetworkFee(otherP.txn) - if ret := pNF.CompareTo(otherNF); ret != 0 { + if ret := p.netFee.CompareTo(otherP.netFee); ret != 0 { return ret } @@ -118,12 +116,14 @@ func (mp *Pool) ContainsKey(hash util.Uint256) bool { func (mp *Pool) Add(t *transaction.Transaction, fee Feer) error { var pool *items var pItem = &item{ - txn: t, - timeStamp: time.Now().UTC(), - fee: fee, + txn: t, + timeStamp: time.Now().UTC(), + perByteFee: fee.FeePerByte(t), + netFee: fee.NetworkFee(t), + isLowPrio: fee.IsLowPriority(t), } - if pItem.fee.IsLowPriority(pItem.txn) { + if pItem.isLowPrio { pool = &mp.sortedLowPrioTxn } else { pool = &mp.sortedHighPrioTxn