moka-rs / moka

A high performance concurrent caching library for Rust
Apache License 2.0
1.61k stars 73 forks source link

Refactoring: Cache policy implementations #389

Open tatsuya6502 opened 9 months ago

tatsuya6502 commented 9 months ago

As of v0.12.4, Moka supports only the TinyLFU cache policy. However, there are some requests and plans to support other cache policies. I am going to implement the LRU policy without refactoring the cache policy implementation, mainly due to time constraints. But, later when I have time, I would like to refactor it.

Cache policies

  1. TinyLFU, with LRU eviction strategy, supported by all Moka versions
  2. TinyLFU, with sampled LFU eviction strategy (Ristretto style)
    • Requested by #247.
  3. Window TinyLFU (Caffeine style)
    • Planned as #249.
  4. LRU
    • Requested by #388.

I am not sure if 2. is worth implementing. But I could write a prototype implementation and run it against the Caffeine simulator (a Moka driver) to see how well it will do.

How these policies work

Moka W-TinyLFU

Read

TinyLFU

Window TinyLFU

LRU

Insert or Update

TinyLFU, with LRU eviction strategy

TinyLFU, with sampled LFU eviction strategy

Window TinyLFU

LRU

ben-manes commented 9 months ago

fwiw, I don't know if SLRU is helpful anymore. It very well may be and would make perfect sense to be, but I have not evaluated it for a very long time. The rational would be that the victim should have a historical high frequency and low recency or that sketch collisions might have mistakenly admitted an entry that could age out quickly. However, that aging only makes sense with jitter or else TinyLFU could excessively reject candidates due to an artificially hot victim.

If I remember correctly, I originally saw SLRU improve the WikiBench trace by a good margin. I later fixed flaws in my CountMin sketch that was introducing hash bias and recall that may have corrected it on that trace. I left it as is since I had already done all the work, the researchers had written the paper section, and I saw no downsides. It might help on traces or it may be busy work, I just don't know.

If you are in the mood of analyzing variations then you might try to see if it is worthwhile. But analyzing takes a lot of time so only do that if interested or it would save you work.

tatsuya6502 commented 6 months ago

I started to implement Window-TinyLFU policy here: #249