16
16
#define GOLDEN_RATIO_32 0x61C88647
17
17
#define HASH (val ) \
18
18
(((val) * (GOLDEN_RATIO_32)) >> (32 - (cache_size_bits))) & (cache_size - 1)
19
+
19
20
/* THRESHOLD is set to identify hot spots. Once the frequency of use for a block
20
- * exceeds the THRESHOLD, the JIT compiler flow is triggered. */
21
+ * exceeds the THRESHOLD, the JIT compiler flow is triggered.
22
+ */
21
23
#define THRESHOLD 1000
22
24
23
25
static uint32_t cache_size , cache_size_bits ;
24
26
static struct mpool * cache_mp ;
25
27
26
28
#if RV32_HAS (ARC )
27
- /*
28
- * Adaptive Replacement Cache (ARC) improves the fundamental LRU strategy
29
- * by dividing the cache into two lists, T1 and T2. list T1 is for LRU
30
- * strategy and list T2 is for LFU strategy. Moreover, it keeps two ghost
31
- * lists, B1 and B2, with replaced entries from the LRU list going into B1
32
- * and the LFU list going into B2.
29
+ /* The Adaptive Replacement Cache (ARC) improves the traditional LRU strategy
30
+ * by dividing the cache into two lists: T1 and T2. T1 follows the LRU
31
+ * strategy, while T2 follows the LFU strategy. Additionally, ARC maintains two
32
+ * ghost lists, B1 and B2, which store replaced entries from the LRU and LFU
33
+ * lists, respectively.
33
34
*
34
- * Based on B1 and B2, ARC will modify the size of T1 and T2. When a cache
35
- * hit occurs in B1, it indicates that T1's capacity is too little, therefore
36
- * we increase T1's size while decreasing T2. But, if the cache hit occurs in
37
- * B2, we would increase the size of T2 and decrease the size of T1.
35
+ * Based on the contents of B1 and B2, ARC dynamically adjusts the sizes of T1
36
+ * and T2. If a cache hit occurs in B1, it indicates that the size of T1 is
37
+ * insufficient, leading to an increase in T1's size and a decrease in T2's
38
+ * size. Conversely, if a cache hit occurs in B2, T2's size is increased while
39
+ * T1's size is decreased.
38
40
*/
39
41
typedef enum {
40
42
LRU_list ,
@@ -57,8 +59,7 @@ struct hlist_node {
57
59
};
58
60
59
61
#if RV32_HAS (ARC )
60
- /*
61
- * list maintains four cache lists T1, T2, B1, and B2.
62
+ /* list maintains four cache lists T1, T2, B1, and B2.
62
63
* ht_list maintains hashtable and improves the performance of cache searching.
63
64
*/
64
65
typedef struct {
@@ -225,7 +226,6 @@ static inline void hlist_del_init(struct hlist_node *n)
225
226
pos = hlist_entry_safe((pos)->member.next, type, member))
226
227
#endif
227
228
228
-
229
229
cache_t * cache_create (int size_bits )
230
230
{
231
231
cache_t * cache = malloc (sizeof (cache_t ));
@@ -291,12 +291,12 @@ cache_t *cache_create(int size_bits)
291
291
292
292
293
293
#if RV32_HAS (ARC )
294
- /* Rules of ARC
294
+ /* Rules of ARC:
295
295
* 1. size of LRU_list + size of LFU_list <= c
296
296
* 2. size of LRU_list + size of LRU_ghost_list <= c
297
297
* 3. size of LFU_list + size of LFU_ghost_list <= 2c
298
298
* 4. size of LRU_list + size of LFU_list + size of LRU_ghost_list + size of
299
- * LFU_ghost_list <= 2c
299
+ * LFU_ghost_list <= 2c
300
300
*/
301
301
#define CACHE_ASSERT (cache ) \
302
302
assert(cache->list_size[LRU_list] + cache->list_size[LFU_list] <= \
@@ -401,10 +401,11 @@ void *cache_get(cache_t *cache, uint32_t key)
401
401
if (!entry || entry -> key != key )
402
402
return NULL ;
403
403
404
- /* Once the frequency of use for a specific block exceeds the predetermined
405
- * THRESHOLD, we dispatch the block to the code generator for the purpose of
406
- * generating C code. Subsequently, the generated C code is compiled into
407
- * machine code by the target compiler. */
404
+ /* When the frequency of use for a specific block exceeds the predetermined
405
+ * THRESHOLD, the block is dispatched to the code generator to generate C
406
+ * code. The generated C code is then compiled into machine code by the
407
+ * target compiler.
408
+ */
408
409
if (entry -> frequency < THRESHOLD ) {
409
410
list_del_init (& entry -> list );
410
411
list_add (& entry -> list , cache -> lists [entry -> frequency ++ ]);
@@ -420,8 +421,8 @@ void *cache_put(cache_t *cache, uint32_t key, void *value)
420
421
#if RV32_HAS (ARC )
421
422
assert (cache -> list_size [LRU_list ] + cache -> list_size [LRU_ghost_list ] <=
422
423
cache -> capacity );
423
- /* Before adding new element to cach, we should check the status
424
- * of cache.
424
+ /* Before adding a new element to the cache, it is necessary to check the
425
+ * status of the cache.
425
426
*/
426
427
if ((cache -> list_size [LRU_list ] + cache -> list_size [LRU_ghost_list ]) ==
427
428
cache -> capacity ) {
@@ -539,4 +540,4 @@ void cache_free(cache_t *cache, void (*callback)(void *))
539
540
free (cache -> map -> ht_list_head );
540
541
free (cache -> map );
541
542
free (cache );
542
- }
543
+ }
0 commit comments