Repository navigation
refactor(storage): Optimized v1LegacyLayout key creation. - #4490
Conversation
|
Some benchmarks below. They all use
|
v1LegacyLayout key creation.v1LegacyLayout key creation.
v1LegacyLayout key creation.v1LegacyLayout key creation.
…usage. Methods refactored: `CalcBlockCommitKey()`, `CalcBlockMetaKey()`, `CalcExtCommitKey()`, and `CalcSeenCommitKey()`
…nd memory usage.
| // LBKey implements LightStoreKeyLayout. | ||
| func (v1LegacyLayout) LBKey(height int64, prefix string) []byte { | ||
| return []byte(fmt.Sprintf("lb/%s/%020d", prefix, height)) | ||
| const ( |
There was a problem hiding this comment.
would be even better to extract this so it doesn't get created on every fn call
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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 😄
This PR refactors the
v1LegacyLayoutkey creation to improve its speed and reduce its memory usage.Follow up to #4484.
PR checklist
.changelog(we use unclog to manage our changelog)docs/orspec/) and code comments