Skip to content

fix(SLRU): give the seg-size rounding remainder to the last segment - #4

Open
marcft wants to merge 4 commits into
developfrom
fix/slru-seg-size-remainder
Open

marcft wants to merge 4 commits into
developfrom
fix/slru-seg-size-remainder

Conversation

@marcft

@marcft marcft commented Nov 13, 2026 •

Copy link
Copy Markdown
Owner

Hi, while working on 1a1a11a#385 I kept seeing weird spikes in the fixed-window W-TinyLFU curves, and the reason I gave for them in that PR was wrong.

The reason was SLRU rounds both segment down, so W-TinyLFU's main ends up one slot short (unless its size is a multiple of 5). W-TinyLFU sees the free slot and puts the next object without checking the sketch. For example in some few traces, more than half the objects just skipped the sketch check at all. The spikes were the few sizes where the bug doesn't happen.

And this is the plot twist, the bug often actually helps. Fixing it makes default W-TinyLFU worse in 50 of the 76 cells from 1a1a11a#385. On cluster26 at the smallest size the miss ratio goes from about 0.26 to 0.47.

I give you here the AI summary ⬇️

What does this PR do?

SLRU_parse_params() rounds each seg-size segment down on its own, so the segments can add up to less than the cache.

Type of change

  • Bug fix
  • New eviction / admission / prefetch algorithm
  • New trace reader or trace format
  • Performance improvement
  • Documentation
  • Build / CI
  • Other:

How was it tested?

  • New unit test test_SLRU_seg_size_fills_cache: fails on develop (5 == 6), passes with the fix.

  • test/test_cli.sh: 428 passed, 0 failed.

  • ctest --test-dir _build --output-on-failure passes (16/16)

  • Build is warning-free (CI uses -Wall -Wextra -Werror)

Results

All runs: --ignore-obj-size=1 --num-thread=1, Release.

The leak is gone.

./bin/cachesim cluster26.oracleGeneral.sample10.zst oracleGeneral wtinylfu <size> --ignore-obj-size=1 --num-thread=1 -e window-size=0.14
size main % 5 develop this PR
1223 2 (leaks) 0.2547 0.4138
1220 0 (no leak) 0.4143 0.4143

At 1223 this PR matches develop with the segments forced to add up exactly, and sizes that were already exact are untouched (13/13 in 1a1a11a#385). Over 208 cells on cluster26 and alibabaBlock_117, the spikes are exactly the cells with main % 5 == 0.

Effect on the 76 cells of 1a1a11a#385 (miss ratio, pp; "same" = within 0.05 pp; negative = first one better):

comparison better / same / worse mean
this PR vs develop, fixed 1% window 4 / 22 / 50 +1.10
this PR vs develop, hill climber 10 / 43 / 23 +0.07
climber vs fixed 1%, as published in 1a1a11a#385 35 / 9 / 32 −1.58
climber vs fixed 1%, both leak-free (this PR) 54 / 9 / 13 −2.61

Not tested: byte mode with variable object sizes, where SLRU may still overfill probation.

Checklist

  • Formatted with clang-format (or the pre-commit hook from scripts/setup_hooks.sh)
  • Added or updated tests
  • Added or updated documentation
  • New algorithms are registered in the CLI and listed in the README

Copilot AI balanced review requested due to automatic review settings November 13, 2026 19:51

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

SLRU_parse_params() rounds each seg-size segment down on its own, so the
segments add up to less than the cache unless the cache size divides
evenly. 1a1a11a#343 fixed this for the default layout but kept explicit
seg-size layouts as they were.

With W-TinyLFU's seg-size=1:4 main cache this is one object short unless
the main size is a multiple of 5. Once every segment is full, a hit in
probation evicts inside find(), and W-TinyLFU then admits its next
window victim without the frequency check.

Call SLRU_resize() after parsing, which gives the remainder to the last
segment. The seg-size=1:2:3:4 print in test_cli.sh changes from
9:19:29:39 to 9:19:29:40.
@marcft
marcft force-pushed the fix/slru-seg-size-remainder branch from 7a95ce2 to 2a04d23 Compare November 13, 2026 20:01
SLRU_cool() moved obj_size between segments while promote, insert and
evict moved obj_size + obj_md_size, so with --consider-obj-metadata
each promote-then-demote left the metadata on the upper segment's
count. When no segment had room, SLRU_insert() evicted until the whole
cache fit, which could push the lowest segment past its share when
objects differ in size; it now evicts until that segment fits too.

The cache name and -e print now show the shares asked for (seg_ratio)
instead of the byte limits, so seg-size=1:1 is S2LRU(50:50) at every
cache size and seg-size=1:2:3:4 prints 10:20:30:40.

Count mode (--ignore-obj-size=1) is unchanged.
In byte mode the main cache evicts whole objects, so it can sit a
little below its size. A window victim that fit that space was admitted
without the frequency check, even long after the main cache had filled.
Remember that the main cache has filled, and compare from then on; the
hill climber clears this when it grows the main cache. A winning window
victim larger than the main victim now evicts until it fits.

Two tests push a cold object out of the window into a full main cache
with free space: from SLRU segments that did not add up (count mode;
fails on develop 4e3521a) and from evicting whole objects (byte mode;
fails without this fix).

Count mode (--ignore-obj-size=1) is unchanged.
-e print exits from SLRU_parse_params() without freeing the cache.
LeakSanitizer stayed quiet only while the cache pointer was still live
in the print call; since the label change no longer passes it, the
Ubuntu job reports the whole cache as leaked. Free it before exiting.

The cache name loops now stop once the name fills the buffer, as the
-e print loop already does.
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