Skip to content

fix(s3fifo): fix eviction policy#59

Merged
samber merged 1 commit intomainfrom
fix/s3fifo-evictino-policy
Mar 11, 2026
Merged

fix(s3fifo): fix eviction policy#59
samber merged 1 commit intomainfrom
fix/s3fifo-evictino-policy

Conversation

@samber
Copy link
Copy Markdown
Owner

@samber samber commented Mar 11, 2026

No description provided.

Copilot AI review requested due to automatic review settings March 11, 2026 02:04
@samber samber merged commit 2532287 into main Mar 11, 2026
10 checks passed
@samber samber deleted the fix/s3fifo-evictino-policy branch March 11, 2026 02:07
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR adjusts the S3-FIFO cache eviction behavior to better match the intended S3-FIFO policy by refining how items are handled when evicting from the small queue and by adding a second-chance style eviction loop for the main queue.

Changes:

  • Change eviction from the small queue to either promote entries with freq > 0 into the main queue or evict freq == 0 entries to the ghost queue.
  • Update main-queue eviction to use a clock-like “second chance” (decrement freq and rotate) before evicting when freq == 0.
  • Rename/replace the small-queue eviction helper to reflect the updated policy.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +365 to +377
// evictFromMain evicts an item from the main queue using a clock-like second chance.
// Per S3-FIFO paper: if front item has freq > 0, decrement and reinsert at tail. If freq == 0, evict.
func (c *S3FIFOCache[K, V]) evictFromMain() bool {
if c.main.Len() == 0 {
return false
}
for c.main.Len() > 0 {
e := c.main.Front()
entry := e.Value

e := c.main.Front()
entry := e.Value
if entry.freq > 0 {
// Second chance: decrement frequency and move to back
entry.freq--
c.freq[entry.key] = entry.freq
c.main.MoveToBack(e)
continue
Copy link

Copilot AI Mar 11, 2026

Choose a reason for hiding this comment

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

The new clock-like/second-chance logic in evictFromMain changes eviction semantics (front entries with freq>0 are rotated and decremented). There are no tests asserting this behavior (e.g., that a hot entry isn’t immediately evicted and that freq is decremented/moved-to-back until it reaches 0). Please add/adjust unit tests to cover these cases so regressions are caught.

Copilot uses AI. Check for mistakes.
Comment on lines +408 to +415
if entry.freq > 0 {
// S3-FIFO paper: promote to main if accessed at least once
entry.queue = 1
entry.freq = 0 // Reset frequency on promotion per paper
newE := c.main.PushBack(entry)
c.cache[entry.key] = newE
c.freq[entry.key] = 0
} else {
Copy link

Copilot AI Mar 11, 2026

Choose a reason for hiding this comment

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

evictFromSmall now conditionally promotes the oldest small entry to main when freq>0 (instead of always evicting it to ghost). The current test suite doesn’t appear to cover this branch (freq==1 case) or verify that the entry remains in-cache and ends up in the main queue. Adding a focused test would help validate the updated policy.

Copilot uses AI. Check for mistakes.
@codecov
Copy link
Copy Markdown

codecov bot commented Mar 11, 2026

Codecov Report

❌ Patch coverage is 21.42857% with 22 lines in your changes missing coverage. Please review.
✅ Project coverage is 64.86%. Comparing base (cb01c6c) to head (8d1da81).
⚠️ Report is 2 commits behind head on main.

Files with missing lines Patch % Lines
pkg/s3fifo/s3fifo.go 21.42% 20 Missing and 2 partials ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main      #59      +/-   ##
==========================================
- Coverage   65.05%   64.86%   -0.20%     
==========================================
  Files          34       34              
  Lines        3835     3848      +13     
==========================================
+ Hits         2495     2496       +1     
- Misses       1253     1263      +10     
- Partials       87       89       +2     
Flag Coverage Δ
unittests 64.86% <21.42%> (-0.20%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

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