mirror of
https://github.com/nspcc-dev/neo-go.git
synced 2024-12-24 09:25:21 +00:00
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:
parent
53c014a0bb
commit
0ea8c8ba67
2 changed files with 10 additions and 14 deletions
|
@ -35,7 +35,7 @@ type item struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
// items is a slice of item.
|
// items is a slice of item.
|
||||||
type items []*item
|
type items []item
|
||||||
|
|
||||||
// utilityBalanceAndFees stores sender's balance and overall fees of
|
// utilityBalanceAndFees stores sender's balance and overall fees of
|
||||||
// sender's transactions which are currently in mempool
|
// sender's transactions which are currently in mempool
|
||||||
|
@ -47,7 +47,7 @@ type utilityBalanceAndFees struct {
|
||||||
// Pool stores the unconfirms transactions.
|
// Pool stores the unconfirms transactions.
|
||||||
type Pool struct {
|
type Pool struct {
|
||||||
lock sync.RWMutex
|
lock sync.RWMutex
|
||||||
verifiedMap map[util.Uint256]*item
|
verifiedMap map[util.Uint256]item
|
||||||
verifiedTxes items
|
verifiedTxes items
|
||||||
fees map[util.Uint160]utilityBalanceAndFees
|
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.
|
// difference = 0 implies p = otherP.
|
||||||
// difference > 0 implies p > otherP.
|
// difference > 0 implies p > otherP.
|
||||||
func (p *item) CompareTo(otherP *item) int {
|
func (p item) CompareTo(otherP item) int {
|
||||||
if otherP == nil {
|
|
||||||
return 1
|
|
||||||
}
|
|
||||||
|
|
||||||
pHigh := p.txn.HasAttribute(transaction.HighPriority)
|
pHigh := p.txn.HasAttribute(transaction.HighPriority)
|
||||||
otherHigh := otherP.txn.HasAttribute(transaction.HighPriority)
|
otherHigh := otherP.txn.HasAttribute(transaction.HighPriority)
|
||||||
if pHigh && !otherHigh {
|
if pHigh && !otherHigh {
|
||||||
|
@ -151,7 +147,7 @@ func checkBalance(tx *transaction.Transaction, balance utilityBalanceAndFees) er
|
||||||
|
|
||||||
// Add tries to add given transaction to the Pool.
|
// Add tries to add given transaction to the Pool.
|
||||||
func (mp *Pool) Add(t *transaction.Transaction, fee Feer) error {
|
func (mp *Pool) Add(t *transaction.Transaction, fee Feer) error {
|
||||||
var pItem = &item{
|
var pItem = item{
|
||||||
txn: t,
|
txn: t,
|
||||||
timeStamp: time.Now().UTC(),
|
timeStamp: time.Now().UTC(),
|
||||||
}
|
}
|
||||||
|
@ -271,8 +267,8 @@ func (mp *Pool) checkPolicy(tx *transaction.Transaction, policyChanged bool) boo
|
||||||
// New returns a new Pool struct.
|
// New returns a new Pool struct.
|
||||||
func New(capacity int) *Pool {
|
func New(capacity int) *Pool {
|
||||||
return &Pool{
|
return &Pool{
|
||||||
verifiedMap: make(map[util.Uint256]*item),
|
verifiedMap: make(map[util.Uint256]item),
|
||||||
verifiedTxes: make([]*item, 0, capacity),
|
verifiedTxes: make([]item, 0, capacity),
|
||||||
capacity: capacity,
|
capacity: capacity,
|
||||||
fees: make(map[util.Uint160]utilityBalanceAndFees),
|
fees: make(map[util.Uint160]utilityBalanceAndFees),
|
||||||
}
|
}
|
||||||
|
|
|
@ -262,23 +262,23 @@ func TestMempoolItemsOrder(t *testing.T) {
|
||||||
tx1.NetworkFee = new(big.Int).Div(balance, big.NewInt(8)).Int64()
|
tx1.NetworkFee = new(big.Int).Div(balance, big.NewInt(8)).Int64()
|
||||||
tx1.Signers = []transaction.Signer{{Account: sender0}}
|
tx1.Signers = []transaction.Signer{{Account: sender0}}
|
||||||
tx1.Attributes = []transaction.Attribute{{Type: transaction.HighPriority}}
|
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 := transaction.New(netmode.UnitTestNet, []byte{byte(opcode.PUSH1)}, 0)
|
||||||
tx2.NetworkFee = new(big.Int).Div(balance, big.NewInt(16)).Int64()
|
tx2.NetworkFee = new(big.Int).Div(balance, big.NewInt(16)).Int64()
|
||||||
tx2.Signers = []transaction.Signer{{Account: sender0}}
|
tx2.Signers = []transaction.Signer{{Account: sender0}}
|
||||||
tx2.Attributes = []transaction.Attribute{{Type: transaction.HighPriority}}
|
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 := transaction.New(netmode.UnitTestNet, []byte{byte(opcode.PUSH1)}, 0)
|
||||||
tx3.NetworkFee = new(big.Int).Div(balance, big.NewInt(2)).Int64()
|
tx3.NetworkFee = new(big.Int).Div(balance, big.NewInt(2)).Int64()
|
||||||
tx3.Signers = []transaction.Signer{{Account: sender0}}
|
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 := transaction.New(netmode.UnitTestNet, []byte{byte(opcode.PUSH1)}, 0)
|
||||||
tx4.NetworkFee = new(big.Int).Div(balance, big.NewInt(4)).Int64()
|
tx4.NetworkFee = new(big.Int).Div(balance, big.NewInt(4)).Int64()
|
||||||
tx4.Signers = []transaction.Signer{{Account: sender0}}
|
tx4.Signers = []transaction.Signer{{Account: sender0}}
|
||||||
item4 := &item{txn: tx4}
|
item4 := item{txn: tx4}
|
||||||
|
|
||||||
require.True(t, item1.CompareTo(item2) > 0)
|
require.True(t, item1.CompareTo(item2) > 0)
|
||||||
require.True(t, item2.CompareTo(item1) < 0)
|
require.True(t, item2.CompareTo(item1) < 0)
|
||||||
|
|
Loading…
Reference in a new issue