Skip to content

perf(txindex): search optimization - #3458

Merged
melekes merged 6 commits into
cometbft:mainfrom
inon-man:perf-txindex
Jul 10, 2024
Merged

melekes merged 6 commits into
cometbft:mainfrom
inon-man:perf-txindex

Conversation

@inon-man

@inon-man inon-man commented Jul 8, 2024 •

Copy link
Copy Markdown
Contributor

This PR addresses several optimization points for TxIndex.Search:

  1. Remove expensive map access from sorting function
  2. Replace sort.Slice to sort.Sort with sorter approach
  3. Reduce heap allocations from extractEventSeqFromKey

benchstat result:

goos: darwin
goarch: amd64
pkg: github.com/cometbft/cometbft/state/txindex/kv
cpu: Intel(R) Core(TM) i9-10910 CPU @ 3.60GHz
                     │   old.txt    │              new.txt               │
                     │    sec/op    │   sec/op     vs base               │
TxSearchBigResult-20   3236.1m ± 7%   945.9m ± 9%  -70.77% (p=0.002 n=6)

                     │   old.txt    │               new.txt               │
                     │     B/op     │     B/op      vs base               │
TxSearchBigResult-20   426.1Mi ± 0%   311.6Mi ± 0%  -26.86% (p=0.002 n=6)

                     │   old.txt   │              new.txt               │
                     │  allocs/op  │  allocs/op   vs base               │
TxSearchBigResult-20   4.040M ± 0%   2.040M ± 0%  -49.51% (p=0.002 n=6)

PR checklist

  • Tests written/updated
  • Changelog entry added in .changelog (we use unclog to manage our changelog)
  • Updated relevant documentation (docs/ or spec/) and code comments
  • Title follows the Conventional Commits spec

@inon-man
inon-man requested a review from a team as a code owner July 8, 2024 12:38
@inon-man
inon-man requested a review from a team July 8, 2024 12:38
@melekes melekes added the indexer label Jul 9, 2024

@melekes melekes left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @inon-man ❤️

Comment thread state/txindex/kv/kv.go
tagKeySeparator = "/"
tagKeySeparatorRune = '/'
eventSeqSeparator = "$es$"
tagKeySeparator = "/"

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we still need this?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess it is for conditional search.

@ValarDragon

ValarDragon commented Jul 9, 2024 •

Copy link
Copy Markdown
Contributor

Really helpful, thank you! Can we backport this?

@melekes

melekes commented Jul 10, 2024

Copy link
Copy Markdown
Collaborator

Really helpful, thank you! Can we backport this?

According to our backporting policy, we will only backport bug fixes to v0.38 and v0.37. We can backport to the v0.37.x-experimental and/or v0.38.x-experimental branches if needed.

@melekes
melekes added this pull request to the merge queue Jul 10, 2024
Merged via the queue into cometbft:main with commit 95af24d Jul 10, 2024
mergify Bot pushed a commit that referenced this pull request Jul 10, 2024
<!--

Please add a reference to the issue that this PR addresses and indicate
which
files are most critical to review. If it fully addresses a particular
issue,
please include "Closes #XXX" (where "XXX" is the issue number).

If this PR is non-trivial/large/complex, please ensure that you have
either
created an issue that the team's had a chance to respond to, or had some
discussion with the team prior to submitting substantial pull requests.
The team
can be reached via GitHub Discussions or the Cosmos Network Discord
server in
the #cometbft channel. GitHub Discussions is preferred over Discord as
it
allows us to keep track of conversations topically.
https://github.com/cometbft/cometbft/discussions

If the work in this PR is not aligned with the team's current
priorities, please
be advised that it may take some time before it is merged - especially
if it has
not yet been discussed with the team.

See the project board for the team's current priorities:
https://github.com/orgs/cometbft/projects/1

-->
This PR addresses several optimization points for TxIndex.Search:

1. Remove expensive map access from sorting function
2. Replace sort.Slice to sort.Sort with sorter approach
3. Reduce heap allocations from extractEventSeqFromKey

benchstat result:
```
goos: darwin
goarch: amd64
pkg: github.com/cometbft/cometbft/state/txindex/kv
cpu: Intel(R) Core(TM) i9-10910 CPU @ 3.60GHz
                     │   old.txt    │              new.txt               │
                     │    sec/op    │   sec/op     vs base               │
TxSearchBigResult-20   3236.1m ± 7%   945.9m ± 9%  -70.77% (p=0.002 n=6)

                     │   old.txt    │               new.txt               │
                     │     B/op     │     B/op      vs base               │
TxSearchBigResult-20   426.1Mi ± 0%   311.6Mi ± 0%  -26.86% (p=0.002 n=6)

                     │   old.txt   │              new.txt               │
                     │  allocs/op  │  allocs/op   vs base               │
TxSearchBigResult-20   4.040M ± 0%   2.040M ± 0%  -49.51% (p=0.002 n=6)
```

#### PR checklist

- [x] Tests written/updated
- [x] Changelog entry added in `.changelog` (we use
[unclog](https://github.com/informalsystems/unclog) to manage our
changelog)
- [x] Updated relevant documentation (`docs/` or `spec/`) and code
comments
- [x] Title follows the [Conventional
Commits](https://www.conventionalcommits.org/en/v1.0.0/) spec

---------

Co-authored-by: Anton Kaliaev <[email protected]>
(cherry picked from commit 95af24d)
melekes pushed a commit that referenced this pull request Jul 10, 2024
This PR addresses several optimization points for TxIndex.Search:

1. Remove expensive map access from sorting function
2. Replace sort.Slice to sort.Sort with sorter approach
3. Reduce heap allocations from extractEventSeqFromKey

benchstat result:
```
goos: darwin
goarch: amd64
pkg: github.com/cometbft/cometbft/state/txindex/kv
cpu: Intel(R) Core(TM) i9-10910 CPU @ 3.60GHz
                     │   old.txt    │              new.txt               │
                     │    sec/op    │   sec/op     vs base               │
TxSearchBigResult-20   3236.1m ± 7%   945.9m ± 9%  -70.77% (p=0.002 n=6)

                     │   old.txt    │               new.txt               │
                     │     B/op     │     B/op      vs base               │
TxSearchBigResult-20   426.1Mi ± 0%   311.6Mi ± 0%  -26.86% (p=0.002 n=6)

                     │   old.txt   │              new.txt               │
                     │  allocs/op  │  allocs/op   vs base               │
TxSearchBigResult-20   4.040M ± 0%   2.040M ± 0%  -49.51% (p=0.002 n=6)
```

#### PR checklist

- [x] Tests written/updated
- [x] Changelog entry added in `.changelog` (we use
[unclog](https://github.com/informalsystems/unclog) to manage our
changelog)
- [x] Updated relevant documentation (`docs/` or `spec/`) and code
comments
- [x] Title follows the [Conventional
Commits](https://www.conventionalcommits.org/en/v1.0.0/) spec
<hr>This is an automatic backport of pull request #3458 done by
[Mergify](https://mergify.com).

Co-authored-by: inon-man <[email protected]>
@inon-man
inon-man deleted the perf-txindex branch July 10, 2024 15:07
@ValarDragon ValarDragon added backport-to-v0.38.x Tell Mergify to backport the PR to v0.38.x experimental-backport Marks PRs that should be considered for back port to experimental branches of older versions. labels Jul 24, 2024
mergify Bot pushed a commit that referenced this pull request Jul 24, 2024
<!--

Please add a reference to the issue that this PR addresses and indicate
which
files are most critical to review. If it fully addresses a particular
issue,
please include "Closes #XXX" (where "XXX" is the issue number).

If this PR is non-trivial/large/complex, please ensure that you have
either
created an issue that the team's had a chance to respond to, or had some
discussion with the team prior to submitting substantial pull requests.
The team
can be reached via GitHub Discussions or the Cosmos Network Discord
server in
the #cometbft channel. GitHub Discussions is preferred over Discord as
it
allows us to keep track of conversations topically.
https://github.com/cometbft/cometbft/discussions

If the work in this PR is not aligned with the team's current
priorities, please
be advised that it may take some time before it is merged - especially
if it has
not yet been discussed with the team.

See the project board for the team's current priorities:
https://github.com/orgs/cometbft/projects/1

-->
This PR addresses several optimization points for TxIndex.Search:

1. Remove expensive map access from sorting function
2. Replace sort.Slice to sort.Sort with sorter approach
3. Reduce heap allocations from extractEventSeqFromKey

benchstat result:
```
goos: darwin
goarch: amd64
pkg: github.com/cometbft/cometbft/state/txindex/kv
cpu: Intel(R) Core(TM) i9-10910 CPU @ 3.60GHz
                     │   old.txt    │              new.txt               │
                     │    sec/op    │   sec/op     vs base               │
TxSearchBigResult-20   3236.1m ± 7%   945.9m ± 9%  -70.77% (p=0.002 n=6)

                     │   old.txt    │               new.txt               │
                     │     B/op     │     B/op      vs base               │
TxSearchBigResult-20   426.1Mi ± 0%   311.6Mi ± 0%  -26.86% (p=0.002 n=6)

                     │   old.txt   │              new.txt               │
                     │  allocs/op  │  allocs/op   vs base               │
TxSearchBigResult-20   4.040M ± 0%   2.040M ± 0%  -49.51% (p=0.002 n=6)
```

#### PR checklist

- [x] Tests written/updated
- [x] Changelog entry added in `.changelog` (we use
[unclog](https://github.com/informalsystems/unclog) to manage our
changelog)
- [x] Updated relevant documentation (`docs/` or `spec/`) and code
comments
- [x] Title follows the [Conventional
Commits](https://www.conventionalcommits.org/en/v1.0.0/) spec

---------

Co-authored-by: Anton Kaliaev <[email protected]>
(cherry picked from commit 95af24d)

# Conflicts:
#	.changelog/v0.38.8/improvements/3458-txindex-search-optimization.md
#	state/txindex/kv/kv.go
#	state/txindex/kv/kv_test.go
sergio-mena pushed a commit that referenced this pull request Jul 25, 2024
This PR addresses several optimization points for TxIndex.Search:

1. Remove expensive map access from sorting function
2. Replace sort.Slice to sort.Sort with sorter approach
3. Reduce heap allocations from extractEventSeqFromKey

benchstat result:
```
goos: darwin
goarch: amd64
pkg: github.com/cometbft/cometbft/state/txindex/kv
cpu: Intel(R) Core(TM) i9-10910 CPU @ 3.60GHz
                     │   old.txt    │              new.txt               │
                     │    sec/op    │   sec/op     vs base               │
TxSearchBigResult-20   3236.1m ± 7%   945.9m ± 9%  -70.77% (p=0.002 n=6)

                     │   old.txt    │               new.txt               │
                     │     B/op     │     B/op      vs base               │
TxSearchBigResult-20   426.1Mi ± 0%   311.6Mi ± 0%  -26.86% (p=0.002 n=6)

                     │   old.txt   │              new.txt               │
                     │  allocs/op  │  allocs/op   vs base               │
TxSearchBigResult-20   4.040M ± 0%   2.040M ± 0%  -49.51% (p=0.002 n=6)
```

- [x] Tests written/updated
- [x] Changelog entry added in `.changelog` (we use
[unclog](https://github.com/informalsystems/unclog) to manage our
changelog)
- [x] Updated relevant documentation (`docs/` or `spec/`) and code
comments
- [x] Title follows the [Conventional
Commits](https://www.conventionalcommits.org/en/v1.0.0/) spec

---------

Co-authored-by: Anton Kaliaev <[email protected]>
sergio-mena pushed a commit that referenced this pull request Jul 25, 2024
This PR addresses several optimization points for TxIndex.Search:

1. Remove expensive map access from sorting function
2. Replace sort.Slice to sort.Sort with sorter approach
3. Reduce heap allocations from extractEventSeqFromKey

benchstat result:
```
goos: darwin
goarch: amd64
pkg: github.com/cometbft/cometbft/state/txindex/kv
cpu: Intel(R) Core(TM) i9-10910 CPU @ 3.60GHz
                     │   old.txt    │              new.txt               │
                     │    sec/op    │   sec/op     vs base               │
TxSearchBigResult-20   3236.1m ± 7%   945.9m ± 9%  -70.77% (p=0.002 n=6)

                     │   old.txt    │               new.txt               │
                     │     B/op     │     B/op      vs base               │
TxSearchBigResult-20   426.1Mi ± 0%   311.6Mi ± 0%  -26.86% (p=0.002 n=6)

                     │   old.txt   │              new.txt               │
                     │  allocs/op  │  allocs/op   vs base               │
TxSearchBigResult-20   4.040M ± 0%   2.040M ± 0%  -49.51% (p=0.002 n=6)
```

#### PR checklist

- [x] Tests written/updated
- [x] Changelog entry added in `.changelog` (we use
[unclog](https://github.com/informalsystems/unclog) to manage our
changelog)
- [x] Updated relevant documentation (`docs/` or `spec/`) and code
comments
- [x] Title follows the [Conventional
Commits](https://www.conventionalcommits.org/en/v1.0.0/) spec
<hr>This is an automatic backport of pull request #3458 done by
[Mergify](https://mergify.com).

Co-authored-by: inon-man <[email protected]>
Co-authored-by: Anton Kaliaev <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

backport-to-v0.38.x Tell Mergify to backport the PR to v0.38.x experimental-backport Marks PRs that should be considered for back port to experimental branches of older versions. indexer

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants