| 1 | package Cache::Ref::CAR; | ||||||||||||||||||||||||||||
| 2 | 1 | ▶ | BEGIN { | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 3 | 1 | $Cache::Ref::CAR::AUTHORITY = 'cpan:NUFFIN'; | |||||||||||||||||||||||||||
| 4 | } | ||||||||||||||||||||||||||||
| 5 | 1 | ▶ | BEGIN { | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 1 | $Cache::Ref::CAR::VERSION = '0.05'; # TRIAL | |||||||||||||||||||||||||||
| 7 | } | ||||||||||||||||||||||||||||
| 8 | # ABSTRACT: CLOCK with Adaptive Replacement | ||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | 1 | ▶ | use Moose; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | 1 | ▶ | use List::Util qw(max min); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | 1 | ▶ | use Cache::Ref::CAR::Base (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | 1 | ▶ | use namespace::autoclean; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | 1 | extends qw(Cache::Ref); | |||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | 1 | with qw(Cache::Ref::CAR::Base); | |||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | 21398 | ▶ | sub _mru_history_too_big { | ||||||||||||||||||||||||||
|
Subroutine
_mru_history_too_big: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 22 | 21398 | my $self = shift; | |||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | 21398 | ▶ | $self->_mru_history_size | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 25 | and | ||||||||||||||||||||||||||||
| 26 | $self->_mru_history_size + $self->_mru_size == $self->size; | ||||||||||||||||||||||||||||
| 27 | } | ||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | 740 | ▶ | sub _mfu_history_too_big { | ||||||||||||||||||||||||||
|
Subroutine
_mfu_history_too_big: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 30 | 740 | my $self = shift; | |||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | 740 | $self->_index_size == $self->size * 2; | |||||||||||||||||||||||||||
| 33 | } | ||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | 2773 | ▶ | sub _decrease_mru_target_size { | ||||||||||||||||||||||||||
|
Subroutine
_decrease_mru_target_size: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 36 | 2773 | my $self = shift; | |||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | 2773 | ▶ | if ( $self->_mru_target_size > 0 ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 39 | 649 | my $adjustment = int( $self->_mru_history_size / $self->_mfu_history_size ); | |||||||||||||||||||||||||||
| 40 | 649 | $self->_set_mru_target_size( max( 0, $self->_mru_target_size - max(1, $adjustment) ) ); | |||||||||||||||||||||||||||
| 41 | } | ||||||||||||||||||||||||||||
| 42 | } | ||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | 432 | ▶ | sub _increase_mru_target_size { | ||||||||||||||||||||||||||
|
Subroutine
_increase_mru_target_size: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 45 | 432 | my $self = shift; | |||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | 432 | my $adjustment = int( $self->_mfu_history_size / $self->_mru_history_size ); | |||||||||||||||||||||||||||
| 48 | 432 | $self->_set_mru_target_size( min( $self->size, $self->_mru_target_size + max(1, $adjustment) ) ); | |||||||||||||||||||||||||||
| 49 | } | ||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | 2773 | ▶ | sub _restore_from_mfu_history { | ||||||||||||||||||||||||||
|
Subroutine
_restore_from_mfu_history: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 52 | 2773 | my ( $self, $e ) = @_; | |||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | 2773 | $self->_mfu_push($e); | |||||||||||||||||||||||||||
| 55 | } | ||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | 432 | ▶ | sub _restore_from_mru_history { | ||||||||||||||||||||||||||
|
Subroutine
_restore_from_mru_history: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 58 | 432 | my ( $self, $e ) = @_; | |||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | 432 | $self->_mfu_push($e); | |||||||||||||||||||||||||||
| 61 | } | ||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | 24604 | ▶ | sub expire { | ||||||||||||||||||||||||||
|
Subroutine
expire: called CC=14 SCAR=26.7
| |||||||||||||||||||||||||||||
| 64 | 24604 | my ( $self, $how_many ) = @_; | |||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | 24604 | ▶ | $how_many ||= 1; | ||||||||||||||||||||||||||
|
Condition50%1/2
$how_many ||= 1
MC/DC0%0/1
$how_many ||= 1
$how_many
Truth table50%1/2
A
A= $how_many
| |||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | 24604 | ▶ | if ( my $mru = $self->_mru ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 69 | 21748 | my $cur = $self->_next($mru); | |||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | # mru pool is too big | ||||||||||||||||||||||||||||
| 72 | 21748 | ▶ | while ( $cur and $self->_mru_size >= max(1,$self->_mru_target_size) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$cur and $self->_mru_size >= &max(1, $self->_mru_target_size)
MC/DC50%1/2
$cur and $self->_mru_size >= &max(1, $self->_mru_target_size)
$cur
$self->_mru_size >= &max(1, $self->_mru_target_size)
Truth table66%2/3
A and B
A= $curB= $self->_mru_size >= &max(1, $self->_mru_target_size)
| |||||||||||||||||||||||||||||
| 73 | 21404 | my $next = $self->_next($cur); | |||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | 21404 | $self->_circular_splice($cur); | |||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | 21404 | ▶ | if ( $cur->[0] & Cache::Ref::CAR::Base::REF_BIT ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 78 | 308 | $cur->[0] &= ~Cache::Ref::CAR::Base::REF_BIT; # turn off reference bit | |||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | # move to t2 (mfu) | ||||||||||||||||||||||||||||
| 81 | 308 | $self->_mfu_push($cur); | |||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | 308 | $cur = $next; | |||||||||||||||||||||||||||
| 84 | } else { | ||||||||||||||||||||||||||||
| 85 | # reference bit is off, which means this entry is freeable | ||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | 21096 | delete $cur->[2]; # delete the value | |||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | # move to history | ||||||||||||||||||||||||||||
| 90 | # MFU_BIT not set | ||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||
| 92 | 21096 | ▶ | if ( $self->_mru_history_head ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 93 | 20907 | $self->_set_next($cur, $self->_mru_history_head); | |||||||||||||||||||||||||||
| 94 | 20907 | $self->_set_prev($self->_mru_history_head, $cur); | |||||||||||||||||||||||||||
| 95 | } else { | ||||||||||||||||||||||||||||
| 96 | 189 | $self->_set_next($cur, undef); | |||||||||||||||||||||||||||
| 97 | } | ||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||
| 99 | 21096 | $self->_mru_history_head($cur); | |||||||||||||||||||||||||||
| 100 | 21096 | ▶ | $self->_mru_history_tail($cur) unless $self->_mru_history_tail; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 101 | 21096 | $self->_inc_mru_history_size; | |||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||
| 104 | 21096 | return; | |||||||||||||||||||||||||||
| 105 | } | ||||||||||||||||||||||||||||
| 106 | } | ||||||||||||||||||||||||||||
| 107 | } | ||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||
| 109 | 3508 | for ( 1 .. $how_many ) { | |||||||||||||||||||||||||||
| 110 | 3509 | ▶ | my $tail = $self->_mfu || last; | ||||||||||||||||||||||||||
|
Condition50%1/2
$self->_mfu || (last)
MC/DC0%0/1
$self->_mfu || (last)
$self->_mfu
Truth table50%1/2
A
A= $self->_mfu
| |||||||||||||||||||||||||||||
| 111 | 3509 | ▶ | my $cur = $self->_next($tail) || last; | ||||||||||||||||||||||||||
|
Condition50%1/2
$self->_next($tail) || (last)
MC/DC0%0/1
$self->_next($tail) || (last)
$self->_next($tail)
Truth table50%1/2
A
A= $self->_next($tail)
| |||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||
| 113 | loop: { | ||||||||||||||||||||||||||||
| 114 | 3509 | ▶ | if ( $cur->[0] & Cache::Ref::CAR::Base::REF_BIT ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 115 | 0 | $cur->[0] &= ~Cache::Ref::CAR::Base::REF_BIT; | |||||||||||||||||||||||||||
| 116 | 0 | $tail = $cur; | |||||||||||||||||||||||||||
| 117 | 0 | $cur = $self->_next($cur); | |||||||||||||||||||||||||||
| 118 | 0 | redo loop; | |||||||||||||||||||||||||||
| 119 | } else { | ||||||||||||||||||||||||||||
| 120 | # reference bit is off, which means this entry is freeable | ||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||
| 122 | 3509 | $self->_mfu($tail); | |||||||||||||||||||||||||||
| 123 | 3509 | $self->_circular_splice($cur); | |||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||
| 125 | 3509 | delete $cur->[2]; # delete the value | |||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||
| 127 | # move to history | ||||||||||||||||||||||||||||
| 128 | 3509 | $cur->[0] |= Cache::Ref::CAR::Base::MFU_BIT; | |||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||
| 130 | 3509 | ▶ | if ( $self->_mfu_history_head ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 131 | 2856 | $self->_set_prev($self->_mfu_history_head, $cur); | |||||||||||||||||||||||||||
| 132 | 2856 | $self->_set_next($cur, $self->_mfu_history_head); | |||||||||||||||||||||||||||
| 133 | } else { | ||||||||||||||||||||||||||||
| 134 | 653 | $self->_set_next($cur, undef); | |||||||||||||||||||||||||||
| 135 | } | ||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||
| 137 | 3509 | $self->_mfu_history_head($cur); | |||||||||||||||||||||||||||
| 138 | 3509 | ▶ | $self->_mfu_history_tail($cur) unless $self->_mfu_history_tail; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 139 | 3509 | $self->_inc_mfu_history_size; | |||||||||||||||||||||||||||
| 140 | } | ||||||||||||||||||||||||||||
| 141 | } | ||||||||||||||||||||||||||||
| 142 | } | ||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||
| 144 | 3508 | return; | |||||||||||||||||||||||||||
| 145 | } | ||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||
| 147 | 2 | ▶ | sub _clear_additional { } | ||||||||||||||||||||||||||
|
Subroutine
_clear_additional: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||
| 149 | 1 | __PACKAGE__->meta->make_immutable; | |||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||
| 151 | 1 | __PACKAGE__; | |||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||
| 154 | # ex: set sw=4 et: | ||||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||
| 157 | __END__ | ||||||||||||||||||||||||||||