From 19aab99b593ba07dea4804e2a9bcd4e1e78b328a Mon Sep 17 00:00:00 2001 From: wetcod <37023735+wetcod@users.noreply.github.com> Date: Fri, 4 Jun 2021 19:02:26 +0900 Subject: [PATCH] perf: optimize checking the txs size (#264) * perf: optimize checking the txs size * ci: add GOPRIVATE to workflows * test: add a unit test * fix: fix lint errors --- .github/workflows/coverage.yml | 2 ++ .github/workflows/docker.yml | 2 ++ .github/workflows/lint.yaml | 2 ++ .github/workflows/tests.yml | 2 ++ mempool/clist_mempool.go | 7 ++++--- types/tx_test.go | 30 ++++++++++++++++++++++++++++++ 6 files changed, 42 insertions(+), 3 deletions(-) diff --git a/.github/workflows/coverage.yml b/.github/workflows/coverage.yml index 654c04182..e5014932b 100644 --- a/.github/workflows/coverage.yml +++ b/.github/workflows/coverage.yml @@ -1,4 +1,6 @@ name: Test Coverage +env: + GOPRIVATE: "github.com/line/*" on: pull_request: push: diff --git a/.github/workflows/docker.yml b/.github/workflows/docker.yml index 256fbdfe8..e7d4fac0c 100644 --- a/.github/workflows/docker.yml +++ b/.github/workflows/docker.yml @@ -1,6 +1,8 @@ name: Build & Push # Build & Push rebuilds the ostracon docker image on every push to master and creation of tags # and pushes the image to https://docker-registry.linecorp.com/link-network/v2/lbm +env: + GOPRIVATE: "github.com/line/*" on: pull_request: push: diff --git a/.github/workflows/lint.yaml b/.github/workflows/lint.yaml index eb154a1c1..c36b996a7 100644 --- a/.github/workflows/lint.yaml +++ b/.github/workflows/lint.yaml @@ -2,6 +2,8 @@ name: Lint # Lint runs golangci-lint over the entire Tendermint repository # This workflow is run on every pull request and push to master # The `golangci` job will pass without running if no *.{go, mod, sum} files have been modified. +env: + GOPRIVATE: "github.com/line/*" on: pull_request: push: diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index d9656bb95..edb9ad2cf 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -2,6 +2,8 @@ name: Tests # Tests runs different tests (test_abci_apps, test_abci_cli, test_apps) # This workflow runs on every push to master or release branch and every pull requests # All jobs will pass without running if no *{.go, .mod, .sum} files have been modified +env: + GOPRIVATE: "github.com/line/*" on: pull_request: push: diff --git a/mempool/clist_mempool.go b/mempool/clist_mempool.go index 62dd61c93..555b75990 100644 --- a/mempool/clist_mempool.go +++ b/mempool/clist_mempool.go @@ -17,6 +17,7 @@ import ( tmos "github.com/line/ostracon/libs/os" tmsync "github.com/line/ostracon/libs/sync" "github.com/line/ostracon/p2p" + tmproto "github.com/line/ostracon/proto/ostracon/types" "github.com/line/ostracon/proxy" "github.com/line/ostracon/types" ) @@ -581,13 +582,13 @@ func (mem *CListMempool) ReapMaxBytesMaxGas(maxBytes, maxGas int64) types.Txs { // size per tx, and set the initial capacity based off of that. // txs := make([]types.Tx, 0, tmmath.MinInt(mem.txs.Len(), max/mem.avgTxSize)) txs := make([]types.Tx, 0, mem.txs.Len()) + protoTxs := tmproto.Data{} for e := mem.txs.Front(); e != nil; e = e.Next() { memTx := e.Value.(*mempoolTx) - dataSize := types.ComputeProtoSizeForTxs(append(txs, memTx.tx)) - + protoTxs.Txs = append(protoTxs.Txs, memTx.tx) // Check total size requirement - if maxBytes > -1 && dataSize > maxBytes { + if maxBytes > -1 && int64(protoTxs.Size()) > maxBytes { return txs } // Check total gas requirement. diff --git a/types/tx_test.go b/types/tx_test.go index 08e00c7a7..c8e239834 100644 --- a/types/tx_test.go +++ b/types/tx_test.go @@ -149,3 +149,33 @@ func assertBadProof(t *testing.T, root []byte, bad []byte, good TxProof) { } } } + +func TestComputeProtoSizeForTxs(t *testing.T) { + tests := []struct { + count int + }{ + {1}, + {2}, + {30}, + {450}, + {1352}, + {2543}, + {4000}, + } + + for _, tt := range tests { + allTxs := make(Txs, tt.count) + for i := 0; i < tt.count; i++ { + size := tmrand.Intn(500) + allTxs[i] = tmrand.Bytes(size) + } + + txs := make([]Tx, 0, len(allTxs)) + protoTxs := tmproto.Data{} + for _, tx := range allTxs { + protoTxs.Txs = append(protoTxs.Txs, tx) + txs = append(txs, tx) + require.Equal(t, int64(protoTxs.Size()), ComputeProtoSizeForTxs(txs)) + } + } +}