-
Notifications
You must be signed in to change notification settings - Fork 231
refactor(lru): switch LRU implementation to golang-lru #3674
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
base: main
Are you sure you want to change the base?
Changes from 2 commits
ecb8eea
f68d61e
bfeda6c
3ad6be2
4354dd1
1d8d578
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -129,7 +129,7 @@ func New(database db.KeyValueStore, network *networks.Network, opts ...Option) * | |
| opt(&o) | ||
| } | ||
|
|
||
| cachedFilters := NewAggregatedBloomCache(AggregatedBloomFilterCacheSize) | ||
| cachedFilters := NewAggregatedBloomCache() | ||
|
Contributor
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. Why remove the input param?
Contributor
Author
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. This constructor is used only with a comptime const: I'm not sure what the rationale was to make the cache size runtime configurable, but currently it's not being used anywhere. If we'd leave it as is, we'd make the constructor to panic on invalid size values (<= 0), or we'd have to do a size check, and return and propagate error across the stack i.e. changing return signature of So I've decided to remove the configurationability until it is needed.
Contributor
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. Add the configuration, panic on invalid size. There is no fear of panicking here, since it means it is a programmer error.
Contributor
Author
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. reverted in 1d8d578 |
||
| fallback := func(key EventFiltersCacheKey) (core.AggregatedBloomFilter, error) { | ||
| return core.GetAggregatedBloomFilter(database, key.fromBlock, key.toBlock) | ||
| } | ||
|
|
||
|
Contributor
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. I am thinking it is better to create a package inside utils/lru/lru.go so that imports are called
Contributor
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. I see this comment was marked as resolved without being resolved, why?
Contributor
Author
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. I think it was updated in 3ad6be2. Let me know if that's not enough, or it's not what you meant. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| package utils | ||
|
|
||
| import ( | ||
| "fmt" | ||
|
|
||
| lru "github.com/hashicorp/golang-lru/v2" | ||
| "github.com/hashicorp/golang-lru/v2/simplelru" | ||
| ) | ||
|
|
||
| // NewLRU returns a new thread-safe LRU cache or panics if size <= 0. | ||
| // Use for caches sized by constants or validated config, where a zero size | ||
| // indicates a programmer error rather than a runtime condition. | ||
| func NewLRU[K comparable, V any](size int) *lru.Cache[K, V] { | ||
| c, err := lru.New[K, V](size) | ||
| if err != nil { | ||
| panic(fmt.Errorf("lru: %w (size=%d)", err, size)) | ||
| } | ||
| return c | ||
| } | ||
|
|
||
| // NewSimpleLRU returns a new non-thread-safe LRU cache or panics if size <= 0. | ||
| // Use the same way as NewLRU when external synchronization is provided | ||
| // (e.g. single-goroutine ownership). | ||
| func NewSimpleLRU[K comparable, V any](size int) *simplelru.LRU[K, V] { | ||
| c, err := simplelru.NewLRU[K, V](size, nil) | ||
| if err != nil { | ||
| panic(fmt.Errorf("simplelru: %w (size=%d)", err, size)) | ||
| } | ||
| return c | ||
|
brbrr marked this conversation as resolved.
Outdated
|
||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.