backport: performance improvements for the event query API (#7319) (#9334)

* Performance improvements for the event query API (#7319)

Rework the implementation of event query parsing and execution to
improve performance and reduce memory usage.

Previous memory and CPU profiles of the pubsub service showed query
processing as a significant hotspot. While we don't have evidence that
this is visibly hurting users, fixing it is fairly easy and self-contained.

Updates #6439.

Typical benchmark results comparing the original implementation (PEG) with the reworked implementation (Custom):
```
TEST                        TIME/OP  BYTES/OP  ALLOCS/OP  SPEEDUP   MEM SAVING
BenchmarkParsePEG-12       51716 ns  526832    27
BenchmarkParseCustom-12     2167 ns    4616    17         23.8x     99.1%
BenchmarkMatchPEG-12        3086 ns    1097    22
BenchmarkMatchCustom-12    294.2 ns      64     3         10.5x     94.1%
```
This commit is contained in:
mmsqe
2022-09-13 16:42:14 +08:00
committed by GitHub
parent 93ead3d0e5
commit e80dd00894
32 changed files with 2316 additions and 758 deletions

View File

@@ -36,7 +36,7 @@ func TestEventBusPublishEventTx(t *testing.T) {
// PublishEventTx adds 3 composite keys, so the query below should work
query := fmt.Sprintf("tm.event='Tx' AND tx.height=1 AND tx.hash='%X' AND testType.baz=1", tx.Hash())
txsSub, err := eventBus.Subscribe(context.Background(), "test", tmquery.MustParse(query))
txsSub, err := eventBus.Subscribe(context.Background(), "test", tmquery.MustCompile(query))
require.NoError(t, err)
done := make(chan struct{})
@@ -89,7 +89,7 @@ func TestEventBusPublishEventNewBlock(t *testing.T) {
// PublishEventNewBlock adds the tm.event compositeKey, so the query below should work
query := "tm.event='NewBlock' AND testType.baz=1 AND testType.foz=2"
blocksSub, err := eventBus.Subscribe(context.Background(), "test", tmquery.MustParse(query))
blocksSub, err := eventBus.Subscribe(context.Background(), "test", tmquery.MustCompile(query))
require.NoError(t, err)
done := make(chan struct{})
@@ -184,7 +184,7 @@ func TestEventBusPublishEventTxDuplicateKeys(t *testing.T) {
}
for i, tc := range testCases {
sub, err := eventBus.Subscribe(context.Background(), fmt.Sprintf("client-%d", i), tmquery.MustParse(tc.query))
sub, err := eventBus.Subscribe(context.Background(), fmt.Sprintf("client-%d", i), tmquery.MustCompile(tc.query))
require.NoError(t, err)
done := make(chan struct{})
@@ -248,7 +248,7 @@ func TestEventBusPublishEventNewBlockHeader(t *testing.T) {
// PublishEventNewBlockHeader adds the tm.event compositeKey, so the query below should work
query := "tm.event='NewBlockHeader' AND testType.baz=1 AND testType.foz=2"
headersSub, err := eventBus.Subscribe(context.Background(), "test", tmquery.MustParse(query))
headersSub, err := eventBus.Subscribe(context.Background(), "test", tmquery.MustCompile(query))
require.NoError(t, err)
done := make(chan struct{})
@@ -289,7 +289,7 @@ func TestEventBusPublishEventNewEvidence(t *testing.T) {
require.NoError(t, err)
query := "tm.event='NewEvidence'"
evSub, err := eventBus.Subscribe(context.Background(), "test", tmquery.MustParse(query))
evSub, err := eventBus.Subscribe(context.Background(), "test", tmquery.MustCompile(query))
require.NoError(t, err)
done := make(chan struct{})
@@ -326,7 +326,7 @@ func TestEventBusPublish(t *testing.T) {
const numEventsExpected = 14
sub, err := eventBus.Subscribe(context.Background(), "test", tmquery.Empty{}, numEventsExpected)
sub, err := eventBus.Subscribe(context.Background(), "test", tmquery.All, numEventsExpected)
require.NoError(t, err)
done := make(chan struct{})

View File

@@ -162,11 +162,11 @@ var (
)
func EventQueryTxFor(tx Tx) tmpubsub.Query {
return tmquery.MustParse(fmt.Sprintf("%s='%s' AND %s='%X'", EventTypeKey, EventTx, TxHashKey, tx.Hash()))
return tmquery.MustCompile(fmt.Sprintf("%s='%s' AND %s='%X'", EventTypeKey, EventTx, TxHashKey, tx.Hash()))
}
func QueryForEvent(eventType string) tmpubsub.Query {
return tmquery.MustParse(fmt.Sprintf("%s='%s'", EventTypeKey, eventType))
return tmquery.MustCompile(fmt.Sprintf("%s='%s'", EventTypeKey, eventType))
}
// BlockEventPublisher publishes all block related events

View File

@@ -10,18 +10,18 @@ import (
func TestQueryTxFor(t *testing.T) {
tx := Tx("foo")
assert.Equal(t,
fmt.Sprintf("tm.event='Tx' AND tx.hash='%X'", tx.Hash()),
fmt.Sprintf("tm.event = 'Tx' AND tx.hash = '%X'", tx.Hash()),
EventQueryTxFor(tx).String(),
)
}
func TestQueryForEvent(t *testing.T) {
assert.Equal(t,
"tm.event='NewBlock'",
"tm.event = 'NewBlock'",
QueryForEvent(EventNewBlock).String(),
)
assert.Equal(t,
"tm.event='NewEvidence'",
"tm.event = 'NewEvidence'",
QueryForEvent(EventNewEvidence).String(),
)
}