Skip to content
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

common/lru: add generic LRU implementation #1361

Merged
merged 2 commits into from Nov 17, 2022
Merged

common/lru: add generic LRU implementation #1361

merged 2 commits into from Nov 17, 2022

Conversation

ucwong
Copy link
Member

@ucwong ucwong commented Nov 16, 2022

It seems there is no fully typed library implementation of an LRU cache. So I wrote one. Method names are the same as github.com/hashicorp/golang-lru, and the new type can be used as a drop-in replacement.

Two reasons to do this:

  • It's much easier to understand what a cache is for when the types are right there.
  • Performance: the new implementation is slightly faster and performs zero memory allocations in Add when the cache is at capacity. Overall, memory usage of the cache is much reduced because keys are values are no longer wrapped in interface.

fjl and others added 2 commits November 15, 2022 07:42
It seems there is no fully typed library implementation of an LRU cache.
So I wrote one. Method names are the same as github.com/hashicorp/golang-lru,
and the new type can be used as a drop-in replacement.

Two reasons to do this:

- It's much easier to understand what a cache is for when the types are right there.
- Performance: the new implementation is slightly faster and performs zero memory
   allocations in Add when the cache is at capacity. Overall, memory usage of the cache
   is much reduced because keys are values are no longer wrapped in interface.
@codecov
Copy link

codecov bot commented Nov 16, 2022

Codecov Report

Merging #1361 (0857410) into master (cb0ca1d) will increase coverage by 0.02%.
The diff coverage is 73.51%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #1361      +/-   ##
==========================================
+ Coverage   51.58%   51.61%   +0.02%     
==========================================
  Files         458      460       +2     
  Lines       57899    58008     +109     
==========================================
+ Hits        29870    29942      +72     
- Misses      25754    25801      +47     
+ Partials     2275     2265      -10     
Impacted Files Coverage Δ
common/lru/legacy/lru_legacy.go 80.00% <ø> (ø)
common/lru/lru.go 0.00% <0.00%> (ø)
core/blockchain_reader.go 24.70% <20.00%> (+0.14%) ⬆️
core/state/database.go 64.15% <55.55%> (-1.24%) ⬇️
core/headerchain.go 47.53% <75.00%> (-0.50%) ⬇️
core/blockchain.go 22.55% <90.90%> (-0.49%) ⬇️
common/lru/basiclru.go 96.11% <96.11%> (ø)
common/lru/blob_lru.go 100.00% <100.00%> (+7.14%) ⬆️
ctxc/filters/filter_system.go 61.50% <100.00%> (+0.31%) ⬆️
p2p/discover/v5wire/session.go 100.00% <100.00%> (+4.34%) ⬆️
... and 16 more

@ucwong ucwong merged commit 602056f into master Nov 17, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants