From 45876d082878f1bf40981c236a941b311c125d10 Mon Sep 17 00:00:00 2001 From: Ethan Buchman Date: Tue, 18 Apr 2017 20:23:58 -0400 Subject: [PATCH] NewBatch takes size, batch.Add doesn't use append --- state/execution.go | 2 +- state/txindex/indexer.go | 10 ++++++---- state/txindex/kv/kv_test.go | 9 +++++---- 3 files changed, 12 insertions(+), 9 deletions(-) diff --git a/state/execution.go b/state/execution.go index 2e980faa5..25d5dcd3a 100644 --- a/state/execution.go +++ b/state/execution.go @@ -246,7 +246,7 @@ func (s *State) ApplyBlock(eventCache types.Fireable, proxyAppConn proxy.AppConn return fmt.Errorf("Commit failed for application: %v", err) } - batch := txindex.NewBatch() + batch := txindex.NewBatch(block.NumTxs) for _, r := range txResults { batch.Add(*r) } diff --git a/state/txindex/indexer.go b/state/txindex/indexer.go index f492dac2c..1c311830a 100644 --- a/state/txindex/indexer.go +++ b/state/txindex/indexer.go @@ -33,13 +33,15 @@ type Batch struct { } // NewBatch creates a new Batch. -func NewBatch() *Batch { - return &Batch{} +func NewBatch(n int) *Batch { + return &Batch{ + Ops: make([]types.TxResult, n), + } } -// Index adds or updates entry for the given hash. +// Index adds or updates entry for the given result.Index. func (b *Batch) Add(result types.TxResult) error { - b.Ops = append(b.Ops, result) + b.Ops[result.Index] = result return nil } diff --git a/state/txindex/kv/kv_test.go b/state/txindex/kv/kv_test.go index 0bd45a824..cd881dfc1 100644 --- a/state/txindex/kv/kv_test.go +++ b/state/txindex/kv/kv_test.go @@ -17,10 +17,10 @@ func TestTxIndex(t *testing.T) { indexer := &TxIndex{store: db.NewMemDB()} tx := types.Tx("HELLO WORLD") - txResult := &types.TxResult{1, 1, tx, abci.ResponseDeliverTx{Data: []byte{0}, Code: abci.CodeType_OK, Log: ""}} + txResult := &types.TxResult{1, 0, tx, abci.ResponseDeliverTx{Data: []byte{0}, Code: abci.CodeType_OK, Log: ""}} hash := tx.Hash() - batch := txindex.NewBatch() + batch := txindex.NewBatch(1) batch.Add(*txResult) err := indexer.AddBatch(batch) require.Nil(t, err) @@ -32,7 +32,7 @@ func TestTxIndex(t *testing.T) { func benchmarkTxIndex(txsCount int, b *testing.B) { tx := types.Tx("HELLO WORLD") - txResult := &types.TxResult{1, 1, tx, abci.ResponseDeliverTx{Data: []byte{0}, Code: abci.CodeType_OK, Log: ""}} + txResult := &types.TxResult{1, 0, tx, abci.ResponseDeliverTx{Data: []byte{0}, Code: abci.CodeType_OK, Log: ""}} dir, err := ioutil.TempDir("", "tx_indexer_db") if err != nil { @@ -43,8 +43,9 @@ func benchmarkTxIndex(txsCount int, b *testing.B) { store := db.NewDB("tx_indexer", "leveldb", dir) indexer := &TxIndex{store: store} - batch := txindex.NewBatch() + batch := txindex.NewBatch(txsCount) for i := 0; i < txsCount; i++ { + txResult.Index += 1 batch.Add(*txResult) }