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.
This commit is contained in:
Roman Khimov 2020-02-05 14:34:58 +03:00
parent e01bfeeb4d
commit 325bea3fa9

View file

@ -25,9 +25,11 @@ var (
// item represents a transaction in the the Memory pool. // item represents a transaction in the the Memory pool.
type item struct { type item struct {
txn *transaction.Transaction txn *transaction.Transaction
timeStamp time.Time timeStamp time.Time
fee Feer perByteFee util.Fixed8
netFee util.Fixed8
isLowPrio bool
} }
// items is a slice of item. // items is a slice of item.
@ -59,7 +61,7 @@ func (p item) CompareTo(otherP *item) int {
return 1 return 1
} }
if p.fee.IsLowPriority(p.txn) && p.fee.IsLowPriority(otherP.txn) { if p.isLowPrio && otherP.isLowPrio {
thisIsClaimTx := p.txn.Type == transaction.ClaimType thisIsClaimTx := p.txn.Type == transaction.ClaimType
otherIsClaimTx := otherP.txn.Type == transaction.ClaimType otherIsClaimTx := otherP.txn.Type == transaction.ClaimType
@ -74,15 +76,11 @@ func (p item) CompareTo(otherP *item) int {
} }
// Fees sorted ascending. // Fees sorted ascending.
pFPB := p.fee.FeePerByte(p.txn) if ret := p.perByteFee.CompareTo(otherP.perByteFee); ret != 0 {
otherFPB := p.fee.FeePerByte(otherP.txn)
if ret := pFPB.CompareTo(otherFPB); ret != 0 {
return ret return ret
} }
pNF := p.fee.NetworkFee(p.txn) if ret := p.netFee.CompareTo(otherP.netFee); ret != 0 {
otherNF := p.fee.NetworkFee(otherP.txn)
if ret := pNF.CompareTo(otherNF); ret != 0 {
return ret return ret
} }
@ -118,12 +116,14 @@ func (mp *Pool) ContainsKey(hash util.Uint256) bool {
func (mp *Pool) Add(t *transaction.Transaction, fee Feer) error { func (mp *Pool) Add(t *transaction.Transaction, fee Feer) error {
var pool *items var pool *items
var pItem = &item{ var pItem = &item{
txn: t, txn: t,
timeStamp: time.Now().UTC(), timeStamp: time.Now().UTC(),
fee: fee, perByteFee: fee.FeePerByte(t),
netFee: fee.NetworkFee(t),
isLowPrio: fee.IsLowPriority(t),
} }
if pItem.fee.IsLowPriority(pItem.txn) { if pItem.isLowPrio {
pool = &mp.sortedLowPrioTxn pool = &mp.sortedLowPrioTxn
} else { } else {
pool = &mp.sortedHighPrioTxn pool = &mp.sortedHighPrioTxn