Skip to content

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

Open
wants to merge 26 commits into
base: master
Choose a base branch
from

Conversation

liqiangz
Copy link
Contributor

@liqiangz liqiangz commented Oct 13, 2020

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
image

Describe how to verify it

run tinyLFUTest
run testHitRateTest

Special notes for reviews

@CLAassistant
Copy link

CLAassistant commented Oct 13, 2020

CLA assistant check
All committers have signed the CLA.

@louyuting louyuting added area/performance Issues or PRs related to runtime performance kind/enhancement Category issues or PRs related to enhancement labels Oct 13, 2020
@codecov-io
Copy link

codecov-io commented Oct 14, 2020

Codecov Report

Merging #287 (eb34490) into master (f854c93) will increase coverage by 1.07%.
The diff coverage is 58.56%.

Impacted file tree graph

@@            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     
Impacted Files Coverage Δ
core/hotspot/cache/wtinylfu/concurrent_tinylfu.go 0.00% <0.00%> (ø)
core/hotspot/cache/lru/lru.go 44.11% <11.76%> (ø)
core/hotspot/cache/wtinylfu/hash.go 21.42% <21.42%> (ø)
core/hotspot/cache/wtinylfu/tinylfu.go 67.02% <67.02%> (ø)
core/hotspot/cache/wtinylfu/slru.go 73.07% <73.07%> (ø)
core/hotspot/cache/wtinylfu/doorkeeper.go 82.50% <82.50%> (ø)
core/hotspot/cache/stats/cache_stats.go 83.33% <83.33%> (ø)
core/hotspot/cache/lru/concurrent_lru.go 87.50% <85.71%> (ø)
core/hotspot/cache/wtinylfu/count_min_sketch.go 93.33% <93.33%> (ø)
core/hotspot/traffic_shaping.go 62.26% <100.00%> (ø)
... and 21 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update f854c93...eb34490. Read the comment docs.

@louyuting louyuting added this to the 1.1.0 milestone Nov 15, 2020
@louyuting louyuting added the area/flow-hotspot Issues or PRs related to hotspot flow control label Dec 7, 2020
# 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
@liqiangz liqiangz requested a review from louyuting December 19, 2020 11:02
@liqiangz liqiangz requested a review from louyuting December 30, 2020 13:24
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
for j := 1000; j <= 1001; j++ {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why run it twice

Copy link
Contributor Author

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 {
Copy link
Collaborator

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 ?

Copy link
Contributor Author

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{}) {
Copy link
Collaborator

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?

Copy link
Contributor Author

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

@louyuting louyuting modified the milestones: 1.1.0, 1.2.0 Jan 19, 2021
}
items := make(map[interface{}]*list.Element)
return &TinyLfu{
countMinSketch: newCountMinSketch(countersFactor * cap),
Copy link
Collaborator

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/flow-hotspot Issues or PRs related to hotspot flow control area/performance Issues or PRs related to runtime performance kind/enhancement Category issues or PRs related to enhancement
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Hotspot: enhance the LRU cache victim mechanism in hotspot module
5 participants