Skip to content

Commit

Permalink
vochain/indexer: use prepared statements for queries
Browse files Browse the repository at this point in the history
Allows sqlite to reuse some of the work when parsing each query.

              │     old     │                new                │
              │   sec/op    │   sec/op     vs base              │
    Indexer-8   54.39m ± 0%   53.73m ± 0%  -1.21% (p=0.001 n=7)

              │     old      │                new                 │
              │     B/op     │     B/op      vs base              │
    Indexer-8   874.1Ki ± 0%   842.1Ki ± 0%  -3.66% (p=0.001 n=7)

              │     old     │                new                │
              │  allocs/op  │  allocs/op   vs base              │
    Indexer-8   23.92k ± 0%   22.73k ± 0%  -4.98% (p=0.001 n=7)
  • Loading branch information
mvdan authored and p4u committed Aug 11, 2023
1 parent a2c5675 commit 2ad39d6
Show file tree
Hide file tree
Showing 8 changed files with 391 additions and 38 deletions.
4 changes: 2 additions & 2 deletions vochain/indexer/db/blocks.sql.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

351 changes: 349 additions & 2 deletions vochain/indexer/db/db.go

Large diffs are not rendered by default.

28 changes: 14 additions & 14 deletions vochain/indexer/db/processes.sql.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions vochain/indexer/db/token_transfers.sql.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 6 additions & 6 deletions vochain/indexer/db/transactions.sql.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 5 additions & 5 deletions vochain/indexer/db/votes.sql.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 11 additions & 6 deletions vochain/indexer/indexer.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,8 @@ type Indexer struct {
// blockTx is an in-progress SQL transaction which is committed or rolled
// back along with the current block.
blockTx *sql.Tx
// blockQueries wraps blockTx.
// blockQueries wraps blockTx. Note that it is kept between multiple transactions
// so that we can reuse the same prepared statements.
blockQueries *indexerdb.Queries
// blockUpdateProcs is the list of process IDs that require sync with the state database.
// The key is a types.ProcessID as a string, so that it can be used as a map key.
Expand Down Expand Up @@ -150,7 +151,14 @@ func NewIndexer(dataDir string, app *vochain.BaseApplication, countLiveResults b
return nil, fmt.Errorf("goose up: %w", err)
}

idx.readOnlyQuery = indexerdb.New(idx.readOnlyDB)
idx.readOnlyQuery, err = indexerdb.Prepare(context.TODO(), idx.readOnlyDB)
if err != nil {
return nil, err
}
idx.blockQueries, err = indexerdb.Prepare(context.TODO(), idx.readWriteDB)
if err != nil {
panic(err)
}

// Subscribe to events
idx.App.State.AddEventListener(idx)
Expand Down Expand Up @@ -179,7 +187,7 @@ func (idx *Indexer) blockTxQueries() *indexerdb.Queries {
panic(err) // shouldn't happen, use an error return if it ever does
}
idx.blockTx = tx
idx.blockQueries = indexerdb.New(idx.blockTx)
idx.blockQueries = idx.blockQueries.WithTx(tx)
}
return idx.blockQueries
}
Expand Down Expand Up @@ -292,7 +300,6 @@ func (idx *Indexer) AfterSyncBootstrap(inTest bool) {
log.Errorw(err, "could not commit tx")
}
idx.blockTx = nil
idx.blockQueries = nil

log.Infof("live results recovery computation finished, took %s", time.Since(startTime))
}
Expand Down Expand Up @@ -402,7 +409,6 @@ func (idx *Indexer) Commit(height uint32) error {
log.Errorw(err, "could not commit tx")
}
idx.blockTx = nil
idx.blockQueries = nil

if newVotes+overwritedVotes > 0 {
log.Infow("add live votes to results",
Expand All @@ -423,7 +429,6 @@ func (idx *Indexer) Rollback() {
log.Errorw(err, "could not rollback tx")
}
idx.blockTx = nil
idx.blockQueries = nil
}
maps.Clear(idx.blockUpdateProcs)
}
Expand Down
1 change: 1 addition & 0 deletions vochain/indexer/sqlc.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ sql:
go:
package: "indexerdb"
out: "db"
emit_prepared_queries: true
overrides:
# Defaults to int32 for integers, which is a bit small.
- db_type: "int"
Expand Down

0 comments on commit 2ad39d6

Please sign in to comment.