-
Notifications
You must be signed in to change notification settings - Fork 14
make caching/lru a real LRU #491
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
Open
omar-polo
wants to merge
12
commits into
main
Choose a base branch
from
op/lru-really-lru
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
a7bac80
caching/lru: augment the coverage
omar-polo 90e88fb
caching/lru: add some benchmarks
omar-polo 714aaeb
caching/lru: refactor: move the value from the hash-map into the list
omar-polo adf9b0e
caching/lru: turn the linked list into a double-linked list
omar-polo 96d73cc
caching/lru: actually behave like a LRU
omar-polo 3f2612b
caching/lru: add a doc for New and silently bump size
omar-polo 8aeff7f
caching/lru: improve eviction test
omar-polo 8cdd02b
caching/lru: add a "promote middle node" test for full coverage
omar-polo fbc583d
caching/lru: rename flush() to evictItem
omar-polo dfe7598
btree: fix tests after LRU rework
omar-polo 0b201e3
caching/lru: add some docs
omar-polo f29ebf2
caching/lru: simplify Close
omar-polo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,159 @@ | ||
| package lru | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "testing" | ||
| ) | ||
|
|
||
| func BenchmarkPut(b *testing.B) { | ||
| for _, target := range []int{16, 256, 4096} { | ||
| b.Run(fmt.Sprintf("target=%d", target), func(b *testing.B) { | ||
| cache := New[int, int](target, nil) | ||
| defer cache.Close() | ||
|
|
||
| b.ReportAllocs() | ||
| for i := 0; i < b.N; i++ { | ||
| if err := cache.Put(i, i); err != nil { | ||
| b.Fatal(err) | ||
| } | ||
| } | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| func BenchmarkPutUpdateExisting(b *testing.B) { | ||
| cache := New[int, int](256, nil) | ||
| defer cache.Close() | ||
|
|
||
| for i := range 256 { | ||
| if err := cache.Put(i, i); err != nil { | ||
| b.Fatal(err) | ||
| } | ||
| } | ||
|
|
||
| b.ReportAllocs() | ||
| for i := 0; b.Loop(); i++ { | ||
| if err := cache.Put(i%256, i); err != nil { | ||
| b.Fatal(err) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| func BenchmarkGetHit(b *testing.B) { | ||
| cache := New[int, int](256, nil) | ||
| defer cache.Close() | ||
|
|
||
| for i := range 256 { | ||
| if err := cache.Put(i, i); err != nil { | ||
| b.Fatal(err) | ||
| } | ||
| } | ||
|
|
||
| b.ReportAllocs() | ||
| for i := 0; b.Loop(); i++ { | ||
| _, _ = cache.Get(i % 256) | ||
| } | ||
| } | ||
|
|
||
| func BenchmarkGetMiss(b *testing.B) { | ||
| cache := New[int, int](256, nil) | ||
| defer cache.Close() | ||
|
|
||
| for i := range 256 { | ||
| if err := cache.Put(i, i); err != nil { | ||
| b.Fatal(err) | ||
| } | ||
| } | ||
|
|
||
| b.ReportAllocs() | ||
| for i := 0; b.Loop(); i++ { | ||
| _, _ = cache.Get(i + 1_000_000) | ||
| } | ||
| } | ||
|
|
||
| func BenchmarkGetParallel(b *testing.B) { | ||
| cache := New[int, int](256, nil) | ||
| defer cache.Close() | ||
|
|
||
| for i := range 256 { | ||
| if err := cache.Put(i, i); err != nil { | ||
| b.Fatal(err) | ||
| } | ||
| } | ||
|
|
||
| b.ReportAllocs() | ||
| b.ResetTimer() | ||
| b.RunParallel(func(pb *testing.PB) { | ||
| i := 0 | ||
| for pb.Next() { | ||
| _, _ = cache.Get(i % 256) | ||
| i++ | ||
| } | ||
| }) | ||
| } | ||
|
|
||
| // BenchmarkPutEviction measures Put cost when every insert forces an | ||
| // eviction, i.e. the cache is kept permanently full. | ||
| func BenchmarkPutEviction(b *testing.B) { | ||
| const target = 256 | ||
| cache := New[int, int](target, nil) | ||
| defer cache.Close() | ||
|
|
||
| for i := range target { | ||
| if err := cache.Put(i, i); err != nil { | ||
| b.Fatal(err) | ||
| } | ||
| } | ||
|
|
||
| b.ReportAllocs() | ||
| for i := 0; b.Loop(); i++ { | ||
| if err := cache.Put(target+i, i); err != nil { | ||
| b.Fatal(err) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| func BenchmarkPutEvictionWithCallback(b *testing.B) { | ||
| const target = 256 | ||
| cache := New(target, func(K int, V int) error { return nil }) | ||
| defer cache.Close() | ||
|
|
||
| for i := range target { | ||
| if err := cache.Put(i, i); err != nil { | ||
| b.Fatal(err) | ||
| } | ||
| } | ||
|
|
||
| b.ReportAllocs() | ||
| for i := 0; b.Loop(); i++ { | ||
| if err := cache.Put(target+i, i); err != nil { | ||
| b.Fatal(err) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // BenchmarkMixed simulates a workload with 90% reads and 10% writes, | ||
| // where writes cycle through a fixed key space and steadily trigger | ||
| // evictions once the cache is warm. | ||
| func BenchmarkMixed(b *testing.B) { | ||
| const target = 512 | ||
| cache := New[int, int](target, nil) | ||
| defer cache.Close() | ||
|
|
||
| for i := range target { | ||
| if err := cache.Put(i, i); err != nil { | ||
| b.Fatal(err) | ||
| } | ||
| } | ||
|
|
||
| b.ReportAllocs() | ||
| for i := 0; b.Loop(); i++ { | ||
| if i%10 == 0 { | ||
| if err := cache.Put(target+i, i); err != nil { | ||
| b.Fatal(err) | ||
| } | ||
| } else { | ||
| _, _ = cache.Get(i % target) | ||
| } | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.