-
Notifications
You must be signed in to change notification settings - Fork 3.8k
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
fix: accaddr cachefix #15433
fix: accaddr cachefix #15433
Changes from 6 commits
a81b41b
9a47a18
50a1aa2
fd5e93d
6fe9d1c
1ec7c3d
58c6259
7b80e5b
ab3c459
0b9787b
02b5116
cd44a3d
c4b64b5
407df3a
df63851
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could you add a changelog? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done, thanks. Missed that in the contributor guide, my bad. |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -85,6 +85,8 @@ var ( | |
consAddrCache *simplelru.LRU | ||
valAddrMu sync.Mutex | ||
valAddrCache *simplelru.LRU | ||
|
||
isCachingEnabled bool | ||
) | ||
|
||
// sentinel errors | ||
|
@@ -94,6 +96,8 @@ var ( | |
|
||
func init() { | ||
var err error | ||
isCachingEnabled = true | ||
|
||
// in total the cache size is 61k entries. Key is 32 bytes and value is around 50-70 bytes. | ||
// That will make around 92 * 61k * 2 (LRU) bytes ~ 11 MB | ||
if accAddrCache, err = simplelru.NewLRU(60000, nil); err != nil { | ||
|
@@ -107,6 +111,11 @@ func init() { | |
} | ||
} | ||
|
||
// By default, caches are enabled. This enables or disables accAddrCache, consAddrCache, and valAddrCache. | ||
julienrbrt marked this conversation as resolved.
Show resolved
Hide resolved
|
||
func SetAddrCacheEnabled(enabled bool) { | ||
isCachingEnabled = enabled | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's unsafe to mutate a global variable like this without synchronization (e.g. a mutex). There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's only meant to be used once on client startup. If I put a mutex in There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You're correct that both reads and writes need to be synchronized. You might consider atomic.Bool as an alternative. var cachingEnabled atomic.Bool
func SetCachingEnabled(b bool) { cachingEnabled.Store(b) }
func GetCachingEnabled() bool { return cachingEnabled.Load() } |
||
|
||
// Address is a common interface for different types of addresses used by the SDK | ||
type Address interface { | ||
Equals(Address) bool | ||
|
@@ -287,11 +296,15 @@ func (aa AccAddress) String() string { | |
} | ||
|
||
key := conv.UnsafeBytesToStr(aa) | ||
accAddrMu.Lock() | ||
defer accAddrMu.Unlock() | ||
addr, ok := accAddrCache.Get(key) | ||
if ok { | ||
return addr.(string) | ||
|
||
if isCachingEnabled { | ||
accAddrMu.Lock() | ||
defer accAddrMu.Unlock() | ||
|
||
addr, ok := accAddrCache.Get(key) | ||
if ok { | ||
return addr.(string) | ||
} | ||
} | ||
return cacheBech32Addr(GetConfig().GetBech32AccountAddrPrefix(), aa, accAddrCache, key) | ||
} | ||
|
@@ -437,11 +450,15 @@ func (va ValAddress) String() string { | |
} | ||
|
||
key := conv.UnsafeBytesToStr(va) | ||
valAddrMu.Lock() | ||
defer valAddrMu.Unlock() | ||
addr, ok := valAddrCache.Get(key) | ||
if ok { | ||
return addr.(string) | ||
|
||
if isCachingEnabled { | ||
valAddrMu.Lock() | ||
defer valAddrMu.Unlock() | ||
|
||
addr, ok := valAddrCache.Get(key) | ||
if ok { | ||
return addr.(string) | ||
} | ||
} | ||
return cacheBech32Addr(GetConfig().GetBech32ValidatorAddrPrefix(), va, valAddrCache, key) | ||
} | ||
|
@@ -592,11 +609,15 @@ func (ca ConsAddress) String() string { | |
} | ||
|
||
key := conv.UnsafeBytesToStr(ca) | ||
consAddrMu.Lock() | ||
defer consAddrMu.Unlock() | ||
addr, ok := consAddrCache.Get(key) | ||
if ok { | ||
return addr.(string) | ||
|
||
if isCachingEnabled { | ||
consAddrMu.Lock() | ||
defer consAddrMu.Unlock() | ||
|
||
addr, ok := consAddrCache.Get(key) | ||
if ok { | ||
return addr.(string) | ||
} | ||
} | ||
return cacheBech32Addr(GetConfig().GetBech32ConsensusAddrPrefix(), ca, consAddrCache, key) | ||
} | ||
|
@@ -676,6 +697,8 @@ func cacheBech32Addr(prefix string, addr []byte, cache *simplelru.LRU, cacheKey | |
if err != nil { | ||
panic(err) | ||
} | ||
cache.Add(cacheKey, bech32Addr) | ||
if isCachingEnabled { | ||
cache.Add(cacheKey, bech32Addr) | ||
} | ||
return bech32Addr | ||
} |
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.
The changelog should be under the bugfix or improvement section (whichever you think fits best).
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.
Moved it there. Am I finally doing it right? :)