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

LFU: Optimize frequency list #33

Open
erwanor opened this issue May 1, 2018 · 0 comments
Open

LFU: Optimize frequency list #33

erwanor opened this issue May 1, 2018 · 0 comments
Labels
enhancement New feature or request good first issue Good for newcomers LFU LFU module v2

Comments

@erwanor
Copy link
Owner

erwanor commented May 1, 2018

Consider an LFU cache with capacity=10 i.e

gc, err := gcache.New(10).LFU().Build()

The internal cache.freqList is a linked list. Each of its entries represent a given frequency along with a map (bucket) with all the items with such frequency.

The issue is that we never clean-up empty entries, over time the list grows larger and larger. It's not great.

Example:

We fill-up our cache:

for key := 0; key < 10; key++ {
    value := fmt.Sprintf("val%d", key)
    gc.Set(key, value)
}

Folllowing this, our cache.freqList has one entry (freq=0) containing all the items with such frequency. In that case that's everything.

Let's simulate some read workload:

numReads := 1000
for read := 0; read < numReads; read++ {
     gc.Get(5)
}

Our cache.freqList should only have two entries because there are only two frequencies i.e freq=0 and freq=1000. The current behavior makes it keep tracks of all the frequency historically used. In other words, the cache.freqList will have 1001 entries, even though only two are needed to represent the whole state!

@erwanor erwanor added enhancement New feature or request good first issue Good for newcomers v2 LFU LFU module labels May 1, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request good first issue Good for newcomers LFU LFU module v2
Projects
None yet
Development

No branches or pull requests

1 participant