Skip to content

Commit

Permalink
perf: optimize checking the txs size (Finschia#264)
Browse files Browse the repository at this point in the history
* perf: optimize checking the txs size

* ci: add GOPRIVATE to workflows

* test: add a unit test

* fix: fix lint errors
  • Loading branch information
egonspace committed Jul 8, 2021
1 parent ceec05b commit 162c63c
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 4 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/coverage.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
name: Test Coverage
env:
GOPRIVATE: "github.com/line/*"
on:
pull_request:
push:
Expand Down
4 changes: 3 additions & 1 deletion .github/workflows/docker.yml
Original file line number Diff line number Diff line change
@@ -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://hub.docker.com/r/interchainio/simapp/tags
# and pushes the image to https://docker-registry.linecorp.com/link-network/v2/lbm
env:
GOPRIVATE: "github.com/line/*"
on:
pull_request:
push:
Expand Down
2 changes: 2 additions & 0 deletions .github/workflows/lint.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ name: Lint
# Lint runs golangci-lint over the entire Ostracon 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:
Expand Down
2 changes: 2 additions & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
7 changes: 4 additions & 3 deletions mempool/clist_mempool.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -587,13 +588,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.
Expand Down
30 changes: 30 additions & 0 deletions types/tx_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
}
}

0 comments on commit 162c63c

Please sign in to comment.