From e9e840d6d64c34df7658223c261248a7633ca126 Mon Sep 17 00:00:00 2001 From: Marc Frances Date: Sat, 7 Nov 2026 20:15:29 +0100 Subject: [PATCH 1/2] fix(WTinyLFU): do not count leaving the window as an access WTinyLFU_evict added 1 to the frequency sketch for every object as it left the window, whether it was admitted to the main cache or dropped. Leaving the window is not an access, so an object read once was counted twice, and a main-cache victim read once tied a candidate read twice; a tie keeps the victim. The W-TinyLFU paper, Caffeine and mandreyel/w-tinylfu, which this file credits, do not count it. Remove the increment. test_WTinyLFU_counts_window_hits now reads object 102 twice instead of six times, so it must beat a victim read once: it needs both the window hit counted and the window exit not counted, and fails without this change. Follow-up to #376, which lists this difference. --- libCacheSim/cache/eviction/WTinyLFU.c | 2 -- test/test_evictionAlgo.c | 11 +++++------ 2 files changed, 5 insertions(+), 8 deletions(-) diff --git a/libCacheSim/cache/eviction/WTinyLFU.c b/libCacheSim/cache/eviction/WTinyLFU.c index 0c91b6d4..9ae81f4a 100644 --- a/libCacheSim/cache/eviction/WTinyLFU.c +++ b/libCacheSim/cache/eviction/WTinyLFU.c @@ -358,8 +358,6 @@ static void WTinyLFU_evict(cache_t *cache, const request_t *req) { } } // TODO @ Ziyue: add doorkeeper - minimalIncrementCBF_add(params->CBF, (void *)(¶ms->req_local->obj_id), - sizeof(obj_id_t)); } else { DEBUG_ASSERT(window->get_occupied_byte(window) == 0); main_cache->evict(main_cache, req); diff --git a/test/test_evictionAlgo.c b/test/test_evictionAlgo.c index db3500fe..adab1db7 100644 --- a/test/test_evictionAlgo.c +++ b/test/test_evictionAlgo.c @@ -582,9 +582,10 @@ static void test_3LCache(gconstpointer user_data) { #endif /* ENABLE_3L_CACHE */ // W-TinyLFU counts every access, so a hit in the window must raise an object's -// frequency just as a hit in the main cache does. Object 102 is read six times -// while it sits in the window; when it reaches the window's tail it must beat -// the main cache's victim, counted twice, and be admitted. +// frequency just as a hit in the main cache does, while leaving the window is +// not an access. Object 102 is read twice while it sits in the window; when it +// reaches the window's tail it must beat the main cache's victim, read once, +// and be admitted. static void test_WTinyLFU_counts_window_hits(void) { // 1000-byte objects: 100 fit, half in the window and half in the main cache. // The sketch is sized from the main cache's bytes, so with this few objects @@ -603,9 +604,7 @@ static void test_WTinyLFU_counts_window_hits(void) { g_assert_false(cache->get(cache, req)); } req->obj_id = 102; - for (int i = 0; i < 5; i++) { - g_assert_true(cache->get(cache, req)); - } + g_assert_true(cache->get(cache, req)); // 49 new objects bring 102 to the window's tail, and the 50th evicts it. for (obj_id_t id = 103; id <= 152; id++) { From 0e3eb1c00cfd2135d37bbc9f988fa37308b63e17 Mon Sep 17 00:00:00 2001 From: Marc Frances Date: Sat, 7 Nov 2026 20:18:17 +0100 Subject: [PATCH 2/2] fix(WTinyLFU): use Caffeine's 4-bit counters and 10x sample size The sketch's counters were unbounded, and the sketch was halved after 32 x the main cache's size accesses, with the access count restarting at 0. Stop counters at 15 (4 bits) and halve after 10 x the cache size accesses, as Caffeine does; this is the Caffeine configuration that the TinyLFU paper evaluates. Halve the access count along with the counters, as the paper's reset does (Sec. 3.3), so later halvings come every 5 x the cache size accesses. minimalIncrementCBF gains a max_count field. It defaults to UINT_MAX, so Merlin is unchanged. The file's preamble now lists what still differs from the paper: there is no doorkeeper, the sketch is sized and its period counted in bytes, an add increments all of an object's counters, and the cap is 15 rather than the paper's W/C. Follow-up to #376, which lists these differences. --- libCacheSim/cache/eviction/WTinyLFU.c | 16 +++++++++++----- libCacheSim/dataStructure/minimalIncrementCBF.c | 7 +++++-- libCacheSim/dataStructure/minimalIncrementCBF.h | 1 + test/test_dataStructure.c | 17 +++++++++++++++++ 4 files changed, 34 insertions(+), 7 deletions(-) diff --git a/libCacheSim/cache/eviction/WTinyLFU.c b/libCacheSim/cache/eviction/WTinyLFU.c index 9ae81f4a..0418aa58 100644 --- a/libCacheSim/cache/eviction/WTinyLFU.c +++ b/libCacheSim/cache/eviction/WTinyLFU.c @@ -8,6 +8,14 @@ // // part of the details borrowed from https://github.com/mandreyel/w-tinylfu // +// the frequency sketch follows Caffeine: counters stop at 15 (4 bits), and +// once the access count reaches 10x the cache size, the counters and the +// access count are halved. Differences from the TinyLFU paper: +// 1. no doorkeeper +// 2. the sketch is sized and aged in bytes, not objects +// 3. an add increments all counters of an object, not only the smallest +// 4. counters stop at 15 instead of W/C (= 10) +// // WTinyLFU.c // libCacheSim // @@ -169,8 +177,7 @@ cache_t *WTinyLFU_init(const common_cache_params_t ccache_params, params->req_local = new_request(); params->n_admit_bytes = 0; - params->max_request_num = - 32 * params->main_cache->cache_size; // sample size is 32 + params->max_request_num = 10 * cache->cache_size; // sample size is 10x params->CBF = (struct minimalIncrementCBF *)malloc(sizeof(struct minimalIncrementCBF)); @@ -183,6 +190,7 @@ cache_t *WTinyLFU_init(const common_cache_params_t ccache_params, if (ret != 0) { ERROR("CBF init failed\n"); } + params->CBF->max_count = 15; // 4-bit counters #ifdef DEBUG_MODE minimalIncrementCBF_print(params->CBF); @@ -234,7 +242,7 @@ static void WTinyLFU_record_access(WTinyLFU_params_t *params, obj_id_t obj_id) { params->request_counter++; if (params->request_counter >= params->max_request_num) { - params->request_counter = 0; + params->request_counter /= 2; minimalIncrementCBF_decay(params->CBF); } } @@ -392,8 +400,6 @@ static void WTinyLFU_parse_params(cache_t *cache, const char *cache_specific_params) { WTinyLFU_params_t *params = (WTinyLFU_params_t *)cache->eviction_params; - // params->max_request_num = 32 * cache->cache_size; // 32 * cache_size - char *params_str = strdup(cache_specific_params); char *old_params_str = params_str; while (params_str != NULL && params_str[0] != '\0') { diff --git a/libCacheSim/dataStructure/minimalIncrementCBF.c b/libCacheSim/dataStructure/minimalIncrementCBF.c index b8667eb8..97c2389f 100644 --- a/libCacheSim/dataStructure/minimalIncrementCBF.c +++ b/libCacheSim/dataStructure/minimalIncrementCBF.c @@ -68,6 +68,7 @@ int minimalIncrementCBF_init(struct minimalIncrementCBF *CBF, int entries, // #endif exit(1); } + CBF->max_count = UINT_MAX; CBF->ready = 1; return 0; } @@ -116,7 +117,7 @@ static int minimalIncrementCBF_check_add(struct minimalIncrementCBF *CBF, } } - if (add == 1 && min_count < UINT_MAX) { + if (add == 1 && min_count < CBF->max_count) { for (i = 0; i < CBF->hashes; i++) { x = (a + i * b) % CBF->counter_num; if (CBF->bf[x] == min_count) { @@ -125,7 +126,9 @@ static int minimalIncrementCBF_check_add(struct minimalIncrementCBF *CBF, CBF->bf[x], min_count); #endif } - CBF->bf[x]++; + if (CBF->bf[x] < CBF->max_count) { + CBF->bf[x]++; + } } min_count++; } diff --git a/libCacheSim/dataStructure/minimalIncrementCBF.h b/libCacheSim/dataStructure/minimalIncrementCBF.h index af73f716..2a2801b5 100644 --- a/libCacheSim/dataStructure/minimalIncrementCBF.h +++ b/libCacheSim/dataStructure/minimalIncrementCBF.h @@ -27,6 +27,7 @@ struct minimalIncrementCBF { // on these. double bpe; unsigned int* bf; + unsigned int max_count; int ready; }; diff --git a/test/test_dataStructure.c b/test/test_dataStructure.c index 7b298e36..41b3a3ac 100644 --- a/test/test_dataStructure.c +++ b/test/test_dataStructure.c @@ -5,6 +5,7 @@ #include "common.h" #include "dataStructure/hashtable/chainedHashTableV2.h" #include "dataStructure/hashtable/hashtable.h" +#include "dataStructure/minimalIncrementCBF.h" void test_chained_hashtable_v2(gconstpointer user_data) { set_rand_seed(rand()); @@ -33,11 +34,27 @@ void test_chained_hashtable_v2(gconstpointer user_data) { free_chained_hashtable_v2(hashtable); } +void test_minimalIncrementCBF_max_count(gconstpointer user_data) { + struct minimalIncrementCBF cbf = {.ready = 0}; + minimalIncrementCBF_init(&cbf, 1000, 0.001); + cbf.max_count = 15; + + obj_id_t id = 42; + for (int i = 0; i < 20; i++) { + minimalIncrementCBF_add(&cbf, &id, sizeof(id)); + } + g_assert_cmpint(minimalIncrementCBF_estimate(&cbf, &id, sizeof(id)), ==, 15); + + minimalIncrementCBF_free(&cbf); +} + int main(int argc, char *argv[]) { g_test_init(&argc, &argv, NULL); g_test_add_data_func("/libCacheSim/test_chained_hashtable_v2", NULL, test_chained_hashtable_v2); + g_test_add_data_func("/libCacheSim/test_minimalIncrementCBF_max_count", NULL, + test_minimalIncrementCBF_max_count); return g_test_run(); }