From 0ea8c8ba67578a7c10c6380dbcb77196033f3229 Mon Sep 17 00:00:00 2001 From: Roman Khimov Date: Sat, 29 Aug 2020 22:20:56 +0300 Subject: [PATCH] mempool: drop a level of indirection `item` is so small that it makes no sense bothering memory allocator with every instance of it. --- pkg/core/mempool/mem_pool.go | 16 ++++++---------- pkg/core/mempool/mem_pool_test.go | 8 ++++---- 2 files changed, 10 insertions(+), 14 deletions(-) diff --git a/pkg/core/mempool/mem_pool.go b/pkg/core/mempool/mem_pool.go index a643123ee..883890c8f 100644 --- a/pkg/core/mempool/mem_pool.go +++ b/pkg/core/mempool/mem_pool.go @@ -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), } diff --git a/pkg/core/mempool/mem_pool_test.go b/pkg/core/mempool/mem_pool_test.go index 1415eab5f..d297efbb4 100644 --- a/pkg/core/mempool/mem_pool_test.go +++ b/pkg/core/mempool/mem_pool_test.go @@ -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)