mempool: drop a level of indirection

`item` is so small that it makes no sense bothering memory allocator with
every instance of it.
This commit is contained in:
Roman Khimov 2020-08-29 22:20:56 +03:00
parent 53c014a0bb
commit 0ea8c8ba67
2 changed files with 10 additions and 14 deletions

View file

@ -35,7 +35,7 @@ type item struct {
}
// items is a slice of item.
type items []*item
type items []item
// utilityBalanceAndFees stores sender's balance and overall fees of
// sender's transactions which are currently in mempool
@ -47,7 +47,7 @@ type utilityBalanceAndFees struct {
// Pool stores the unconfirms transactions.
type Pool struct {
lock sync.RWMutex
verifiedMap map[util.Uint256]*item
verifiedMap map[util.Uint256]item
verifiedTxes items
fees map[util.Uint160]utilityBalanceAndFees
@ -63,11 +63,7 @@ func (p items) Less(i, j int) bool { return p[i].CompareTo(p[j]) < 0 }
// difference < 0 implies p < otherP.
// difference = 0 implies p = otherP.
// difference > 0 implies p > otherP.
func (p *item) CompareTo(otherP *item) int {
if otherP == nil {
return 1
}
func (p item) CompareTo(otherP item) int {
pHigh := p.txn.HasAttribute(transaction.HighPriority)
otherHigh := otherP.txn.HasAttribute(transaction.HighPriority)
if pHigh && !otherHigh {
@ -151,7 +147,7 @@ func checkBalance(tx *transaction.Transaction, balance utilityBalanceAndFees) er
// Add tries to add given transaction to the Pool.
func (mp *Pool) Add(t *transaction.Transaction, fee Feer) error {
var pItem = &item{
var pItem = item{
txn: t,
timeStamp: time.Now().UTC(),
}
@ -271,8 +267,8 @@ func (mp *Pool) checkPolicy(tx *transaction.Transaction, policyChanged bool) boo
// New returns a new Pool struct.
func New(capacity int) *Pool {
return &Pool{
verifiedMap: make(map[util.Uint256]*item),
verifiedTxes: make([]*item, 0, capacity),
verifiedMap: make(map[util.Uint256]item),
verifiedTxes: make([]item, 0, capacity),
capacity: capacity,
fees: make(map[util.Uint160]utilityBalanceAndFees),
}

View file

@ -262,23 +262,23 @@ func TestMempoolItemsOrder(t *testing.T) {
tx1.NetworkFee = new(big.Int).Div(balance, big.NewInt(8)).Int64()
tx1.Signers = []transaction.Signer{{Account: sender0}}
tx1.Attributes = []transaction.Attribute{{Type: transaction.HighPriority}}
item1 := &item{txn: tx1}
item1 := item{txn: tx1}
tx2 := transaction.New(netmode.UnitTestNet, []byte{byte(opcode.PUSH1)}, 0)
tx2.NetworkFee = new(big.Int).Div(balance, big.NewInt(16)).Int64()
tx2.Signers = []transaction.Signer{{Account: sender0}}
tx2.Attributes = []transaction.Attribute{{Type: transaction.HighPriority}}
item2 := &item{txn: tx2}
item2 := item{txn: tx2}
tx3 := transaction.New(netmode.UnitTestNet, []byte{byte(opcode.PUSH1)}, 0)
tx3.NetworkFee = new(big.Int).Div(balance, big.NewInt(2)).Int64()
tx3.Signers = []transaction.Signer{{Account: sender0}}
item3 := &item{txn: tx3}
item3 := item{txn: tx3}
tx4 := transaction.New(netmode.UnitTestNet, []byte{byte(opcode.PUSH1)}, 0)
tx4.NetworkFee = new(big.Int).Div(balance, big.NewInt(4)).Int64()
tx4.Signers = []transaction.Signer{{Account: sender0}}
item4 := &item{txn: tx4}
item4 := item{txn: tx4}
require.True(t, item1.CompareTo(item2) > 0)
require.True(t, item2.CompareTo(item1) < 0)