Skip to content

Conversation

@JimB123
Copy link
Member

@JimB123 JimB123 commented Nov 19, 2025

General cleanup on LRU/LFU code. Improve modularity and maintainability.

Specifically:

  • Consolidates the mathematical logic for LRU/LFU into lrulfu.c, with an API in lrulfu.h. Knowledge of the LRU/LFU implementation was previously spread out across db.c, evict.c, object.c, server.c, and server.h.
  • Separates knowledge of the LRU from knowledge of the object containing the LRU value. lrulfu.c knows about the LRU/LFU algorithms, without knowing about the robj. object.c knows about the robj without knowing about the details of the LRU/LFU algorithms.
  • Eliminated server.lruclock, instead using server.unixtime. This also eliminates the periodic need to call mstime() to maintain the lru clock.
  • Fixed a minor computation bug in the old LFUTimeElapsed function (off by 1 after rollover).
  • Eliminate specific IF checks for rollover, using defined behavior for unsigned rollover instead.
  • Fixed a bug in debug.c which would perform LFU modification on an LRU value.

@JimB123 JimB123 marked this pull request as ready for review November 19, 2025 18:53
Copy link
Member

@ranshid ranshid left a comment

Choose a reason for hiding this comment

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

This is a great refactor @JimB123 !

I could not locate any special issues with the proposed change so gave some small comments. My only concern is that I am not sure how much our test coverage is great when it comes to eviction logic. I guess most of the tests are in maxmemory.tcl which are validated. I think that introducing it now (in early development stage) is probably fine.

int objectSetLRUOrLFU(robj *val, long long lfu_freq, long long lru_idle_secs) {
if (lrulfu_isUsingLFU()) {
if (lfu_freq >= 0) {
serverAssert(lfu_freq <= 255);
Copy link
Member

Choose a reason for hiding this comment

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

if we are refactoring...

Suggested change
serverAssert(lfu_freq <= 255);
serverAssert(lfu_freq <= UINT8_MAX);

} else {
val->lru = LRU_CLOCK();
}
val->lru = lrulfu_touch(val->lru);
Copy link
Member

Choose a reason for hiding this comment

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

why not wrap the touch as an Object API as well?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants