@@ -22,7 +22,7 @@ use foyer_common::{
22
22
use intrusive_collections:: { intrusive_adapter, LinkedList , LinkedListAtomicLink } ;
23
23
use serde:: { Deserialize , Serialize } ;
24
24
25
- use super :: { Eviction , Operator } ;
25
+ use super :: { Eviction , Op } ;
26
26
use crate :: record:: { CacheHint , Record } ;
27
27
28
28
/// w-TinyLFU eviction algorithm config.
@@ -347,71 +347,56 @@ where
347
347
}
348
348
}
349
349
350
- fn acquire_operator ( ) -> super :: Operator {
351
- // TODO(MrCroxx): use a count-min-sketch with atomic u16 impl.
352
- Operator :: Mutable
353
- }
354
-
355
- fn acquire_immutable ( & self , _record : & Arc < Record < Self > > ) {
356
- unreachable ! ( )
357
- }
358
-
359
- fn acquire_mutable ( & mut self , record : & Arc < Record < Self > > ) {
360
- // Update frequency by access.
361
- self . update_frequencies ( record. hash ( ) ) ;
350
+ fn acquire ( ) -> Op < Self > {
351
+ Op :: mutable ( |this : & mut Self , record| {
352
+ // Update frequency by access.
353
+ this. update_frequencies ( record. hash ( ) ) ;
362
354
363
- if !record. is_in_eviction ( ) {
364
- return ;
365
- }
355
+ if !record. is_in_eviction ( ) {
356
+ return ;
357
+ }
366
358
367
- let state = unsafe { & mut * record. state ( ) . get ( ) } ;
359
+ let state = unsafe { & mut * record. state ( ) . get ( ) } ;
368
360
369
- strict_assert ! ( state. link. is_linked( ) ) ;
361
+ strict_assert ! ( state. link. is_linked( ) ) ;
370
362
371
- match state. queue {
372
- Queue :: None => unreachable ! ( ) ,
373
- Queue :: Window => {
374
- // Move to MRU position of `window`.
375
- let r = unsafe { self . window . remove_from_ptr ( Arc :: as_ptr ( record) ) } ;
376
- self . window . push_back ( r) ;
377
- }
378
- Queue :: Probation => {
379
- // Promote to MRU position of `protected`.
380
- let r = unsafe { self . probation . remove_from_ptr ( Arc :: as_ptr ( record) ) } ;
381
- self . decrease_queue_weight ( Queue :: Probation , record. weight ( ) ) ;
382
- state. queue = Queue :: Protected ;
383
- self . increase_queue_weight ( Queue :: Protected , record. weight ( ) ) ;
384
- self . protected . push_back ( r) ;
385
-
386
- // If `protected` weight exceeds the capacity, overflow entry from `protected` to `probation`.
387
- while self . protected_weight > self . protected_weight_capacity {
388
- strict_assert ! ( !self . protected. is_empty( ) ) ;
389
- let r = self . protected . pop_front ( ) . unwrap ( ) ;
390
- let s = unsafe { & mut * r. state ( ) . get ( ) } ;
391
- self . decrease_queue_weight ( Queue :: Protected , r. weight ( ) ) ;
392
- s. queue = Queue :: Probation ;
393
- self . increase_queue_weight ( Queue :: Probation , r. weight ( ) ) ;
394
- self . probation . push_back ( r) ;
363
+ match state. queue {
364
+ Queue :: None => unreachable ! ( ) ,
365
+ Queue :: Window => {
366
+ // Move to MRU position of `window`.
367
+ let r = unsafe { this. window . remove_from_ptr ( Arc :: as_ptr ( record) ) } ;
368
+ this. window . push_back ( r) ;
369
+ }
370
+ Queue :: Probation => {
371
+ // Promote to MRU position of `protected`.
372
+ let r = unsafe { this. probation . remove_from_ptr ( Arc :: as_ptr ( record) ) } ;
373
+ this. decrease_queue_weight ( Queue :: Probation , record. weight ( ) ) ;
374
+ state. queue = Queue :: Protected ;
375
+ this. increase_queue_weight ( Queue :: Protected , record. weight ( ) ) ;
376
+ this. protected . push_back ( r) ;
377
+
378
+ // If `protected` weight exceeds the capacity, overflow entry from `protected` to `probation`.
379
+ while this. protected_weight > this. protected_weight_capacity {
380
+ strict_assert ! ( !this. protected. is_empty( ) ) ;
381
+ let r = this. protected . pop_front ( ) . unwrap ( ) ;
382
+ let s = unsafe { & mut * r. state ( ) . get ( ) } ;
383
+ this. decrease_queue_weight ( Queue :: Protected , r. weight ( ) ) ;
384
+ s. queue = Queue :: Probation ;
385
+ this. increase_queue_weight ( Queue :: Probation , r. weight ( ) ) ;
386
+ this. probation . push_back ( r) ;
387
+ }
388
+ }
389
+ Queue :: Protected => {
390
+ // Move to MRU position of `protected`.
391
+ let r = unsafe { this. protected . remove_from_ptr ( Arc :: as_ptr ( record) ) } ;
392
+ this. protected . push_back ( r) ;
395
393
}
396
394
}
397
- Queue :: Protected => {
398
- // Move to MRU position of `protected`.
399
- let r = unsafe { self . protected . remove_from_ptr ( Arc :: as_ptr ( record) ) } ;
400
- self . protected . push_back ( r) ;
401
- }
402
- }
403
- }
404
-
405
- fn release_operator ( ) -> Operator {
406
- Operator :: Noop
407
- }
408
-
409
- fn release_immutable ( & self , _record : & Arc < Record < Self > > ) {
410
- unreachable ! ( )
395
+ } )
411
396
}
412
397
413
- fn release_mutable ( & mut self , _record : & Arc < Record < Self > > ) {
414
- unreachable ! ( )
398
+ fn release ( ) -> Op < Self > {
399
+ Op :: noop ( )
415
400
}
416
401
}
417
402
@@ -422,17 +407,17 @@ mod tests {
422
407
423
408
use super :: * ;
424
409
use crate :: {
425
- eviction:: test_utils:: { assert_ptr_eq, assert_ptr_vec_vec_eq, TestEviction } ,
410
+ eviction:: test_utils:: { assert_ptr_eq, assert_ptr_vec_vec_eq, Dump , OpExt } ,
426
411
record:: Data ,
427
412
} ;
428
413
429
- impl < K , V > TestEviction for Lfu < K , V >
414
+ impl < K , V > Dump for Lfu < K , V >
430
415
where
431
416
K : Key + Clone ,
432
417
V : Value + Clone ,
433
418
{
434
- type Dump = Vec < Vec < Arc < Record < Self > > > > ;
435
- fn dump ( & self ) -> Self :: Dump {
419
+ type Output = Vec < Vec < Arc < Record < Self > > > > ;
420
+ fn dump ( & self ) -> Self :: Output {
436
421
let mut window = vec ! [ ] ;
437
422
let mut probation = vec ! [ ] ;
438
423
let mut protected = vec ! [ ] ;
0 commit comments