-
Notifications
You must be signed in to change notification settings - Fork 439
Use W-TinyLFU algorithm to enhance the LRU cache #287
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: master
Are you sure you want to change the base?
Conversation
Codecov Report
@@ Coverage Diff @@
## master #287 +/- ##
==========================================
+ Coverage 50.83% 51.90% +1.07%
==========================================
Files 69 76 +7
Lines 3842 4321 +479
==========================================
+ Hits 1953 2243 +290
- Misses 1621 1795 +174
- Partials 268 283 +15
Continue to review full report at Codecov.
|
# Conflicts: # core/hotspot/traffic_shaping.go
# Conflicts: # core/hotspot/cache/lru/concurrent_lru.go # core/hotspot/cache/lru/concurrent_lru_benchmark_test.go # core/hotspot/cache/lru/concurrent_lru_test.go # core/hotspot/cache/lru/lru.go
} | ||
b.ResetTimer() | ||
for i := 0; i < b.N; i++ { | ||
for j := 1000; j <= 1001; j++ { |
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.
Why run it twice
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.
This is to be consistent with lru
// add set a value in the cache | ||
func (slru *slru) add(newItem slruItem) { | ||
newItem.listType = probationSegment | ||
if slru.probationLs.Len() < slru.probationCap || slru.Len() < slru.probationCap+slru.protectedCap { |
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.
How to understand the condition || slru.Len() < slru.probationCap+slru.protectedCap
?
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.
This logic is because if there is only add but no get, probation lru cannot be promoted to protected lru, at this time all elements are in probation
return ok | ||
} | ||
|
||
func (t *TinyLfu) Add(key interface{}, val interface{}) { |
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.
Maybe the implementation Add
make no sense?
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.
This is to be consistent with lru, and there is also this method in the ConcurrentCounterCache interface
} | ||
items := make(map[interface{}]*list.Element) | ||
return &TinyLfu{ | ||
countMinSketch: newCountMinSketch(countersFactor * cap), |
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.
Maybe the countersFactor is not necessary
Describe what this PR does / why we need it
Run testHitRateTest
Use Zipf's law(https://en.wikipedia.org/wiki/Zipf%27s_law) to test hitRate
Refer to https://blog.openacid.com/tech/zipf/
tinyLfu cache size 100, total 2000000, miss 1220740, hitRate 0.389630
lru cache size 100, total 2000000, miss 1447734, hitRate 0.276133
tinyLfu cache size 500, total 2000000, miss 971977, hitRate 0.514011
lru cache size 500, total 2000000, miss 1169400, hitRate 0.415300
tinyLfu cache size 1000, total 2000000, miss 866246, hitRate 0.566877
lru cache size 1000, total 2000000, miss 1048379, hitRate 0.475811
tinyLfu cache size 5000, total 2000000, miss 627779, hitRate 0.686110
lru cache size 5000, total 2000000, miss 765247, hitRate 0.617376
tinyLfu cache size 10000, total 2000000, miss 528916, hitRate 0.735542
lru cache size 10000, total 2000000, miss 641416, hitRate 0.679292
tinyLfu cache size 20000, total 2000000, miss 436149, hitRate 0.781926
lru cache size 20000, total 2000000, miss 517853, hitRate 0.741074
tinyLfu cache size 50000, total 2000000, miss 316985, hitRate 0.841507
lru cache size 50000, total 2000000, miss 352558, hitRate 0.823721
Does this pull request fix one issue?
Fixes #254
Describe how you did it
TinyLfu is an implementation of the TinyLfu algorithm: https://arxiv.org/pdf/1512.00727.pdf

Describe how to verify it
run tinyLFUTest
run testHitRateTest
Special notes for reviews