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

@ -27,7 +27,9 @@ var (
type item struct {
txn *transaction.Transaction
timeStamp time.Time
fee Feer
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
}
@ -120,10 +118,12 @@ func (mp *Pool) Add(t *transaction.Transaction, fee Feer) error {
var pItem = &item{
txn: t,
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
} else {
pool = &mp.sortedHighPrioTxn