From f366ae3c875a4f4f61f37f4b39383558ac5a58cc Mon Sep 17 00:00:00 2001 From: Sam Kleinman Date: Tue, 4 May 2021 10:45:32 -0400 Subject: [PATCH] mempool: benchmark improvements (#6418) --- mempool/bench_test.go | 7 +++++-- mempool/clist_mempool.go | 19 +++++++++++++------ 2 files changed, 18 insertions(+), 8 deletions(-) diff --git a/mempool/bench_test.go b/mempool/bench_test.go index 779110b62..946d8016c 100644 --- a/mempool/bench_test.go +++ b/mempool/bench_test.go @@ -13,13 +13,14 @@ func BenchmarkReap(b *testing.B) { cc := proxy.NewLocalClientCreator(app) mempool, cleanup := newMempoolWithApp(cc) defer cleanup() + mempool.config.Size = 100000 size := 10000 for i := 0; i < size; i++ { tx := make([]byte, 8) binary.BigEndian.PutUint64(tx, uint64(i)) if err := mempool.CheckTx(tx, nil, TxInfo{}); err != nil { - b.Error(err) + b.Fatal(err) } } b.ResetTimer() @@ -34,11 +35,13 @@ func BenchmarkCheckTx(b *testing.B) { mempool, cleanup := newMempoolWithApp(cc) defer cleanup() + mempool.config.Size = 1000000 + for i := 0; i < b.N; i++ { tx := make([]byte, 8) binary.BigEndian.PutUint64(tx, uint64(i)) if err := mempool.CheckTx(tx, nil, TxInfo{}); err != nil { - b.Error(err) + b.Fatal(err) } } } diff --git a/mempool/clist_mempool.go b/mempool/clist_mempool.go index 7d486ad69..363b29fa3 100644 --- a/mempool/clist_mempool.go +++ b/mempool/clist_mempool.go @@ -489,7 +489,10 @@ func (mem *CListMempool) ReapMaxBytesMaxGas(maxBytes, maxGas int64) types.Txs { mem.updateMtx.RLock() defer mem.updateMtx.RUnlock() - var totalGas int64 + var ( + totalGas int64 + runningSize int64 + ) // TODO: we will get a performance boost if we have a good estimate of avg // size per tx, and set the initial capacity based off of that. @@ -498,22 +501,26 @@ func (mem *CListMempool) ReapMaxBytesMaxGas(maxBytes, maxGas int64) types.Txs { for e := mem.txs.Front(); e != nil; e = e.Next() { memTx := e.Value.(*mempoolTx) - dataSize := types.ComputeProtoSizeForTxs(append(txs, memTx.tx)) + txs = append(txs, memTx.tx) + + dataSize := types.ComputeProtoSizeForTxs([]types.Tx{memTx.tx}) // Check total size requirement - if maxBytes > -1 && dataSize > maxBytes { - return txs + if maxBytes > -1 && runningSize+dataSize > maxBytes { + return txs[:len(txs)-1] } + + runningSize += dataSize + // Check total gas requirement. // If maxGas is negative, skip this check. // Since newTotalGas < masGas, which // must be non-negative, it follows that this won't overflow. newTotalGas := totalGas + memTx.gasWanted if maxGas > -1 && newTotalGas > maxGas { - return txs + return txs[:len(txs)-1] } totalGas = newTotalGas - txs = append(txs, memTx.tx) } return txs }