-
Notifications
You must be signed in to change notification settings - Fork 3.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
perf: add cache to address codec #20122
Conversation
Note Reviews pausedUse the following commands to manage reviews:
WalkthroughThe changes introduce a caching mechanism to the Bech32 codec for improved performance, including a new Changes
Sequence Diagram(s) (Beta)sequenceDiagram
participant Client
participant Bech32Codec
participant Cache
participant Mutex
Client->>Bech32Codec: NewCachedBech32Codec(prefix, opts)
Bech32Codec->>Cache: Initialize Cache
Bech32Codec->>Mutex: Initialize Mutex
Bech32Codec-->>Client: Return cachedBech32Codec instance
Client->>Bech32Codec: BytesToString(bz)
Bech32Codec->>Mutex: Lock
alt Cache Hit
Bech32Codec->>Cache: Retrieve from Cache
Cache-->>Bech32Codec: Cached Result
else Cache Miss
Bech32Codec->>Bech32Codec: Perform Conversion
Bech32Codec->>Cache: Store in Cache
end
Bech32Codec->>Mutex: Unlock
Bech32Codec-->>Client: Return Result
Client->>Bech32Codec: StringToBytes(text)
Bech32Codec->>Mutex: Lock
alt Cache Hit
Bech32Codec->>Cache: Retrieve from Cache
Cache-->>Bech32Codec: Cached Result
else Cache Miss
Bech32Codec->>Bech32Codec: Perform Conversion
Bech32Codec->>Cache: Store in Cache
end
Bech32Codec->>Mutex: Unlock
Bech32Codec-->>Client: Return Result
Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media? TipsChatThere are 3 ways to chat with CodeRabbit:
Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (invoked as PR comments)
Additionally, you can add CodeRabbit Configration File (
|
This comment has been minimized.
This comment has been minimized.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 1
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good start! 🏃
I added some thoughts and ideas although I have only limited context on this feature.
When it comes to caching, I also have telemetry on my list to learn about the utilization. This may be overkill but just sharing this collector as example.
|
||
addr, err := cbc.codec.StringToBytes(text) | ||
if err != nil { | ||
return nil, err |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🤔 it can be worth to cache failures, too. Some benchmarks would be interesting.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 2
Out of diff range and nitpick comments (1)
codec/address/bech32_codec.go (1)
39-54
: Consider consolidating cache types or making cache configuration more flexible, as multiple cache types might not be necessary and could complicate maintenance.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 1
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 1
Review Details
Configuration used: .coderabbit.yml
Review profile: CHILL
Files selected for processing (2)
- codec/address/bech32_codec.go (2 hunks)
- codec/address/bech32_codec_test.go (1 hunks)
Additional Context Used
Path-based Instructions (2)
codec/address/bech32_codec_test.go (2)
Pattern
**/*.go
: Review the Golang code for conformity with the Uber Golang style guide, highlighting any deviations.
Pattern
**/*_test.go
: "Assess the unit test code assessing sufficient code coverage for the changes associated in the pull request"codec/address/bech32_codec.go (1)
Pattern
**/*.go
: Review the Golang code for conformity with the Uber Golang style guide, highlighting any deviations.
Additional comments not posted (6)
codec/address/bech32_codec_test.go (2)
17-66
: The unit tests inTestNewBech32Codec
effectively validate the caching mechanism and address conversions. Good coverage of different prefixes and cache interactions.
70-92
: The tests inTestMultipleBech32Codec
correctly ensure that separate caches for different codecs do not interfere with each other and validate the caching behavior.codec/address/bech32_codec.go (4)
39-53
: Initialization of caches and mutexes is correctly implemented, ensuring separate caches for different address types and appropriate error handling.
69-91
: TheNewBech32Codec
function is well-implemented, correctly handling the selection of caches and mutexes based on the prefix and the caching condition.
Line range hint
91-133
: MethodsStringToBytes
andBytesToString
inBech32Codec
are correctly implemented with comprehensive error handling and validation.
134-168
: Methods incachedBech32Codec
are correctly implemented with thread safety and caching. Consider optimizing the locking strategy to improve performance, as discussed in previous comments.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 0
Review Details
Configuration used: .coderabbit.yml
Review profile: CHILL
Files selected for processing (1)
- codec/address/bech32_codec_test.go (1 hunks)
Files skipped from review as they are similar to previous changes (1)
- codec/address/bech32_codec_test.go
Benchmark results
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 0
Review Details
Configuration used: .coderabbit.yml
Review profile: CHILL
Files selected for processing (4)
- codec/address/bech32_codec.go (2 hunks)
- codec/address/bech32_codec_test.go (1 hunks)
- codec/address/bench_test.go (1 hunks)
- codec/address/fuzz_test.go (1 hunks)
Additional Context Used
Path-based Instructions (4)
codec/address/bench_test.go (2)
Pattern
**/*.go
: Review the Golang code for conformity with the Uber Golang style guide, highlighting any deviations.
Pattern
**/*_test.go
: "Assess the unit test code assessing sufficient code coverage for the changes associated in the pull request"codec/address/fuzz_test.go (2)
Pattern
**/*.go
: Review the Golang code for conformity with the Uber Golang style guide, highlighting any deviations.
Pattern
**/*_test.go
: "Assess the unit test code assessing sufficient code coverage for the changes associated in the pull request"codec/address/bech32_codec_test.go (2)
Pattern
**/*.go
: Review the Golang code for conformity with the Uber Golang style guide, highlighting any deviations.
Pattern
**/*_test.go
: "Assess the unit test code assessing sufficient code coverage for the changes associated in the pull request"codec/address/bech32_codec.go (1)
Pattern
**/*.go
: Review the Golang code for conformity with the Uber Golang style guide, highlighting any deviations.
Additional comments not posted (17)
codec/address/bench_test.go (3)
11-14
: Benchmark function for codec with cache looks good.
16-19
: Benchmark function for codec without cache looks good.
21-32
: Helper function for benchmarkingBytesToString
looks good.codec/address/fuzz_test.go (3)
14-27
: Fuzz test for codec with cache looks good.
30-43
: Fuzz test for codec without cache looks good.
46-61
: Helper function for checking address conversion looks good.codec/address/bech32_codec_test.go (5)
17-24
: Helper function for generating addresses looks good.
26-77
: Unit test for creating Bech32 codec with cache looks good.
79-100
: Unit test for creating multiple Bech32 codecs with cache looks good.
102-124
: Unit test for Bech32 codec race conditions looks good.
126-148
: Helper function for testing Bech32 codec race conditions looks good.codec/address/bech32_codec.go (6)
41-56
: Initialization of cache variables looks good.
73-93
: Function for creating new Bech32 codec with optional caching looks good.
Line range hint
98-113
: Function for converting Bech32 string to bytes looks good.
Line range hint
122-134
: Function for converting bytes to Bech32 string looks good.
136-157
: Function for converting bytes to Bech32 string with caching looks good.
159-174
: Function for converting Bech32 string to bytes with caching looks good.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 1
Review Details
Configuration used: .coderabbit.yml
Review profile: CHILL
Files selected for processing (1)
- CHANGELOG.md (1 hunks)
Additional Context Used
Path-based Instructions (1)
CHANGELOG.md (1)
Pattern
**/*.md
: "Assess the documentation for misspellings, grammatical errors, missing documentation and correctness"
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 0
Review Details
Configuration used: .coderabbit.yml
Review profile: CHILL
Files selected for processing (1)
- codec/address/bench_test.go (1 hunks)
Files skipped from review as they are similar to previous changes (1)
- codec/address/bench_test.go
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the updates. This looks good for me to start. I would love to see more configuration options and metrics but this can go into a new PR if needed.
Good work!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 0
Review Details
Configuration used: .coderabbit.yml
Review profile: CHILL
Files selected for processing (1)
- codec/address/bech32_codec_test.go (1 hunks)
Files skipped from review as they are similar to previous changes (1)
- codec/address/bech32_codec_test.go
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM, but I'd prefer this option you listed here:
Store a map in the LRU with prefix as keys (e.g., map["cosmos"] = "cosmos1dr3...")
It would reduce the amount of code and complexity. Also no hardcoded constants.
We would need to figure out how set the size of the cache, but maybe that's something we'd like to have anyway?
codec/address/bech32_codec.go
Outdated
|
||
// cache variables | ||
var ( | ||
accAddrMu sync.Mutex |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If we want to avoid globals, we should consider adding a NewBech32CodecWithOptions
constructor which keeps references to the cache in the codec struct. This would (I think) solve the problem of what looks like 3 global caches; we'd instead of 3 instances of an address codec in the application. This is easily done with depinject but can be handled manually too.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 7
Outside diff range and nitpick comments (2)
codec/address/bech32_codec.go (2)
Line range hint
71-91
: Refactor to improve error handling inStringToBytes
.- if len(bz) > sdkAddress.MaxAddrLen { - return nil, errorsmod.Wrapf(sdkerrors.ErrUnknownAddress, "address max length is %d, got %d", sdkAddress.MaxAddrLen, len(bz)) - } + if len(bz) > sdkAddress.MaxAddrLen { + return nil, sdkerrors.ErrUnknownAddress + }
Line range hint
99-110
: Refactor to improve error handling inBytesToString
.- if len(bz) > sdkAddress.MaxAddrLen { - return "", errorsmod.Wrapf(sdkerrors.ErrUnknownAddress, "address max length is %d, got %d", sdkAddress.MaxAddrLen, len(bz)) - } + if len(bz) > sdkAddress.MaxAddrLen { + return "", sdkerrors.ErrUnknownAddress + }
Review details
Configuration used: .coderabbit.yml
Review profile: CHILL
Files selected for processing (4)
- codec/address/bech32_codec.go (2 hunks)
- codec/address/bech32_codec_test.go (1 hunks)
- codec/address/bench_test.go (1 hunks)
- codec/address/fuzz_test.go (1 hunks)
Additional context used
Path-based instructions (4)
codec/address/bench_test.go (2)
Pattern
**/*.go
: Review the Golang code for conformity with the Uber Golang style guide, highlighting any deviations.
Pattern
**/*_test.go
: "Assess the unit test code assessing sufficient code coverage for the changes associated in the pull request"codec/address/fuzz_test.go (2)
Pattern
**/*.go
: Review the Golang code for conformity with the Uber Golang style guide, highlighting any deviations.
Pattern
**/*_test.go
: "Assess the unit test code assessing sufficient code coverage for the changes associated in the pull request"codec/address/bech32_codec.go (1)
Pattern
**/*.go
: Review the Golang code for conformity with the Uber Golang style guide, highlighting any deviations.codec/address/bech32_codec_test.go (2)
Pattern
**/*.go
: Review the Golang code for conformity with the Uber Golang style guide, highlighting any deviations.
Pattern
**/*_test.go
: "Assess the unit test code assessing sufficient code coverage for the changes associated in the pull request"
Additional comments not posted (5)
codec/address/bench_test.go (2)
16-19
: LGTM! Proper use of the Bech32Codec without cache for benchmarking.
11-14
: Ensure proper initialization oflru
andmu
before use inBenchmarkCodecWithCache
.codec/address/fuzz_test.go (2)
32-48
: LGTM! Proper use of the Bech32Codec without cache for fuzz testing.
14-30
: Ensure proper initialization oflru
andmu
before use inFuzzCachedAddressCodec
.Verification successful
Let's perform a more comprehensive search to verify the initialization of
lru
andmu
.
The search results indicate that
lru
andmu
are indeed initialized in the codebase:
lru
is initialized incodec/address/bech32_codec_test.go
with the linelru, _ = simplelru.NewLRU(500, nil)
.mu
is initialized incodec/address/bech32_codec_test.go
with the linemu = &sync.Mutex{}
.These initializations confirm that
lru
andmu
are properly set up before being used in theFuzzCachedAddressCodec
function.
Scripts executed
The following scripts were executed for the analysis:
Script:
#!/bin/bash # Description: Verify that `lru` and `mu` are properly initialized before use. # Test: Search for their initialization in the codebase. rg --type go 'lru, _ := simplelru.NewLRU' && rg --type go 'mu := &sync.Mutex{}'Length of output: 43
Script:
#!/bin/bash # Description: Verify that `lru` and `mu` are properly initialized before use. # Test: Search for the initialization of `lru` and `mu` in the codebase. rg --type go 'simplelru.NewLRU' && rg --type go 'sync.Mutex'Length of output: 1977
codec/address/bech32_codec_test.go (1)
34-84
: Ensure proper initialization oflru
andmu
before use inTestNewBech32Codec
.Verification successful
Let's perform a more comprehensive search to verify the initialization of
lru
andmu
.
The initialization of
lru
andmu
is confirmed within thecodec/address/bech32_codec_test.go
file:
lru
is initialized withsimplelru.NewLRU(500, nil)
.mu
is initialized with&sync.Mutex{}
.This ensures that the test setup is correct and the review comment is valid.
Scripts executed
The following scripts were executed for the analysis:
Script:
#!/bin/bash # Description: Verify that `lru` and `mu` are properly initialized before use. # Test: Search for their initialization in the codebase. rg --type go 'lru, _ := simplelru.NewLRU' && rg --type go 'mu := &sync.Mutex{}'Length of output: 43
Script:
#!/bin/bash # Description: Verify that `lru` and `mu` are properly initialized before use. # Test: Search for their initialization in the codebase. rg --type go 'simplelru.NewLRU' && rg --type go 'sync.Mutex'Length of output: 1977
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 0
Review details
Configuration used: .coderabbit.yml
Review profile: CHILL
Files selected for processing (4)
- codec/address/bech32_codec.go (2 hunks)
- codec/address/bech32_codec_test.go (1 hunks)
- codec/address/bench_test.go (1 hunks)
- codec/address/fuzz_test.go (1 hunks)
Files skipped from review as they are similar to previous changes (4)
- codec/address/bech32_codec.go
- codec/address/bech32_codec_test.go
- codec/address/bench_test.go
- codec/address/fuzz_test.go
@coderabbitai pause |
Actions performedReviews paused. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM, just one nit
codec/address/bech32_codec.go
Outdated
func NewCachedBech32Codec(prefix string, opts CachedCodecOptions) (address.Codec, error) { | ||
ac := Bech32Codec{prefix} | ||
if opts.Mu == nil { | ||
return nil, errors.New("mutex cannot be nil") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
would it be so bad to provide some sane default instead of erroring on nil?
* main: fix(x/staking): stop validators from rotating to the same key on the same block (#20649) perf: add cache to address codec (#20122) build(deps): Bump google.golang.org/protobuf from 1.34.1 to 1.34.2 (#20632) fix: remove recipient amount from map (#20625) fix(proto): remove conditional preventing proper generated file placement (#20650) (serverv2/cometbft) Read config from commands & handle `FlagNode` (#20621) fix(x/consensus): fix .proto file placement (#20646) fix(store): avoid nil error on not exhausted payload stream (#20644) fix (x/accounts): Fix genesis condition check (#20645) feat(accounts): add genesis account initialization (#20642) fix(x/gov): limit execution in gov (#20348)
Description
ref:
#13140
#7448
Benchmark
Author Checklist
All items are required. Please add a note to the item if the item is not applicable and
please add links to any relevant follow up issues.
I have...
!
in the type prefix if API or client breaking changeCHANGELOG.md
Reviewers Checklist
All items are required. Please add a note if the item is not applicable and please add
your handle next to the items reviewed if you only reviewed selected items.
I have...
Summary by CodeRabbit
New Features
Tests