Skip to content

refactor(storage): Optimized v1LegacyLayout key creation. - #4490

Merged
alesforz merged 15 commits into
mainfrom
alesforz/state-v1legacylayout
Nov 20, 2024
Merged

alesforz merged 15 commits into
mainfrom
alesforz/state-v1legacylayout

Conversation

@alesforz

@alesforz alesforz commented Nov 14, 2024 •

Copy link
Copy Markdown
Collaborator

This PR refactors the v1LegacyLayout key creation to improve its speed and reduce its memory usage.
Follow up to #4484.


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

@alesforz alesforz self-assigned this Nov 14, 2024
@alesforz

alesforz commented Nov 14, 2024 •

Copy link
Copy Markdown
Collaborator Author

Some benchmarks below. They all use height=94_673_744, which was the height of a chain taken from the cosmos directory at the time of benchmarking.

state pkg

Benchmarks for state.v1LegacyLayout.

CalcABCIResponsesKey(), CalcConsensusParamsKey(), and CalcValidatorsKey() compute keys of the form <some_prefix>:<height> so benchmarks results are almost identical.

BenchmarkKeys/StringConcat-11    50976726    23.16 ns/op    8 B/op    1 allocs/op
BenchmarkKeys/Prealloc-11       137513770    8.699 ns/op    0 B/op    0 allocs/op

StringConcat is what's in main now. Preallocate is the refactoring in this PR.

store pkg

Benchmarks for store.v1LegacyLayout.

CalcBlockHashKey()

Used hash=e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855.

BenchmarkKeyHex/Sprintf-11          14144371    84.84 ns/op    104 B/op    2 allocs/op
BenchmarkKeyHex/Preallocate-11      35376228    33.37 ns/op     80 B/op    1 allocs/op
BenchmarkKeyHex/NoHex-11            92982408    12.72 ns/op     48 B/op    1 allocs/op

Sprintf is what's in main now. Preallocate is the refactoring in this PR. NoHex is if we don't hex the hash []byte.

CalcBlockPartKey()

Used height=94_673_744 and partIndex=3.

BenchmarkMultiPartStr/Sprintf-11        16753863    71.16 ns/op    24 B/op    2 allocs/op
BenchmarkMultiPartStr/Preallocate-11    47561875    24.18 ns/op    24 B/op    1 allocs/op

Sprintf is what's in main now. Preallocate is the refactoring in this PR.

Other Methods

CalcBlockCommitKey(), CalcBlockMetaKey(), CalcExtCommitKey(), and CalcSeenCommitKey() compute keys of the form <some_prefix>:<height> so benchmarks results are almost identical.

BenchmarkKey/Sprintf-11              24316731    49.34 ns/op    24 B/op    2 allocs/op
BenchmarkKey/Preallocate-11         162306126     7.391 ns/op    0 B/op    0 allocs/op

Sprintf is what's in main now. Preallocate is the refactoring in this PR.

light/store/db pkg

Benchmarks for db.v1LegacyLayout.

BenchmarkKey/Sprintf-11              12818776    94.40 ns/op    48 B/op    2 allocs/op
BenchmarkKey/Preallocate-11          51989970    22.46 ns/op    32 B/op    1 allocs/op

Sprintf is what's in main now. Preallocate is the refactoring in this PR.

@alesforz
alesforz marked this pull request as ready for review November 14, 2024 17:44
@alesforz
alesforz requested a review from a team as a code owner November 14, 2024 17:44
@alesforz
alesforz requested a review from a team November 14, 2024 17:44
@alesforz alesforz added storage P:tech-debt Priority: Technical debt that needs to be paid off to enable us to move faster, reliably chore labels Nov 14, 2024
@alesforz
alesforz marked this pull request as draft November 15, 2024 12:03
@alesforz alesforz changed the title chore(state): Optimized v1LegacyLayout key creation. refactor(state): Optimized v1LegacyLayout key creation. Nov 15, 2024
@alesforz alesforz changed the title refactor(state): Optimized v1LegacyLayout key creation. refactor(storage): Optimized v1LegacyLayout key creation. Nov 15, 2024
Alessandro added 2 commits November 15, 2024 16:36
…usage.

Methods refactored: `CalcBlockCommitKey()`, `CalcBlockMetaKey()`,
`CalcExtCommitKey()`, and `CalcSeenCommitKey()`
@alesforz
alesforz marked this pull request as ready for review November 15, 2024 15:45

@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.

👍

// LBKey implements LightStoreKeyLayout.
func (v1LegacyLayout) LBKey(height int64, prefix string) []byte {
return []byte(fmt.Sprintf("lb/%s/%020d", prefix, height))
const (

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.

would be even better to extract this so it doesn't get created on every fn call

@alesforz alesforz Nov 18, 2024 •

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

you mean creating global, unexported constants for the prefixes? e.g.,

const (
 _prefixLBKey = "lb/"
 _prefixAbciResponseKey = "abciResponsesKey:"
 _prefixBlockCommitKey = "C:"
// and so on...
)

And have the functions use them instead of creating a local constant?

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.

yes

@alesforz alesforz Nov 19, 2024 •

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

I ran some benchmarks; results are identical using a global const. The length of the string doesn't affect the performance; I think the go compiler does some optimizations on const even if they are declared inside a function.

Benchmark with long string keys:

BenchmarkLongKeys/StringConcat-11    50991807    23.33 ns/op     8 B/op    1 allocs/op
BenchmarkLongKeys/Prealloc-11        137356957    8.736 ns/op    0 B/op    0 allocs/op
BenchmarkLongKeys/GlobalConst-11     137596965    8.730 ns/op    0 B/op    0 allocs/op

Benchmark with short string keys:

BenchmarkShortKey/Sprintf-11         24446098   48.82 ns/op    24 B/op    2 allocs/op
BenchmarkShortKey/Prealloc-11       161066708    7.446 ns/op    0 B/op    0 allocs/op
BenchmarkShortKey/GlobalConst-11    161569386    7.456 ns/op    0 B/op    0 allocs/op

Given these results, I propose to leave the const declaration inside the functions to not pollute the global namespace of the pkg 😄

@alesforz
alesforz added this pull request to the merge queue Nov 20, 2024
Merged via the queue into main with commit b0d484f Nov 20, 2024
@alesforz
alesforz deleted the alesforz/state-v1legacylayout branch November 20, 2024 09:14
@jmalicevic jmalicevic added this to the 2024-Q4 milestone Dec 3, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

chore P:tech-debt Priority: Technical debt that needs to be paid off to enable us to move faster, reliably storage

Projects

No open projects
Status: Done

Development

Successfully merging this pull request may close these issues.

3 participants