Skip to content

Refactor of LFU/LRU code for modularity#2857

Merged
ranshid merged 2 commits into
valkey-io:unstablefrom
JimB123:lru
Dec 2, 2025
Merged

Refactor of LFU/LRU code for modularity#2857
ranshid merged 2 commits into
valkey-io:unstablefrom
JimB123:lru

Conversation

@JimB123

@JimB123 JimB123 commented Nov 19, 2025

Copy link
Copy Markdown
Member

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.

Signed-off-by: Jim Brunner <brunnerj@amazon.com>
@JimB123 JimB123 marked this pull request as ready for review November 19, 2025 18:53

@ranshid ranshid left a comment

Copy link
Copy Markdown
Member

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.

Comment thread src/object.c Outdated
Comment thread src/db.c
} else {
val->lru = LRU_CLOCK();
}
val->lru = lrulfu_touch(val->lru);

Copy link
Copy Markdown
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?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

There's already pretty tight coupling between db.c and object.c. My intent was to clean up the LRU/LFU API (which I did). I wasn't intending to clean up the db/object coupling - that's a bigger task. Given that this was the only place that needed this, and the files are already coupled, I didn't see a reason to create an API for this.

db.c is already directly accessing the lru field when updating in dbSetValue. So there's no point in trying to completely hide it... without further refactor between db/object.

Co-authored-by: Ran Shidlansik <ranshid@amazon.com>
Signed-off-by: Jim Brunner <brunnerj@amazon.com>
@codecov

codecov Bot commented Dec 1, 2025

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 92.94118% with 6 lines in your changes missing coverage. Please review.
✅ Project coverage is 72.43%. Comparing base (2da21d9) to head (1059cd0).
⚠️ Report is 36 commits behind head on unstable.

Files with missing lines Patch % Lines
src/module.c 0.00% 4 Missing ⚠️
src/debug.c 66.66% 1 Missing ⚠️
src/object.c 94.73% 1 Missing ⚠️
Additional details and impacted files
@@             Coverage Diff              @@
##           unstable    #2857      +/-   ##
============================================
+ Coverage     72.42%   72.43%   +0.01%     
============================================
  Files           128      129       +1     
  Lines         70300    70502     +202     
============================================
+ Hits          50917    51071     +154     
- Misses        19383    19431      +48     
Files with missing lines Coverage Δ
src/cluster.c 90.45% <100.00%> (+0.25%) ⬆️
src/db.c 93.12% <100.00%> (-0.04%) ⬇️
src/evict.c 99.15% <100.00%> (+0.63%) ⬆️
src/lrulfu.c 100.00% <100.00%> (ø)
src/rdb.c 77.16% <ø> (+0.20%) ⬆️
src/server.c 88.42% <ø> (-0.03%) ⬇️
src/server.h 100.00% <ø> (ø)
src/debug.c 54.58% <66.66%> (-0.01%) ⬇️
src/object.c 82.03% <94.73%> (+0.09%) ⬆️
src/module.c 9.77% <0.00%> (-0.01%) ⬇️

... and 24 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@ranshid ranshid merged commit 1b5f245 into valkey-io:unstable Dec 2, 2025
55 checks passed
@JimB123 JimB123 deleted the lru branch December 3, 2025 23:31
aradz44 pushed a commit to aradz44/valkey that referenced this pull request Dec 23, 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.

---------

Signed-off-by: Jim Brunner <brunnerj@amazon.com>
Co-authored-by: Ran Shidlansik <ranshid@amazon.com>
jdheyburn pushed a commit to jdheyburn/valkey that referenced this pull request Jan 8, 2026
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.

---------

Signed-off-by: Jim Brunner <brunnerj@amazon.com>
Co-authored-by: Ran Shidlansik <ranshid@amazon.com>
hpatro pushed a commit to hpatro/valkey that referenced this pull request Mar 5, 2026
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.

---------

Signed-off-by: Jim Brunner <brunnerj@amazon.com>
Co-authored-by: Ran Shidlansik <ranshid@amazon.com>
Signed-off-by: Harkrishn Patro <bunty.hari@gmail.com>
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