Ref/CAR.pm

filter: Statementstmt 94.5% 70 / 74 Branchbran 95.0% 19 / 20 Conditioncond 55.5% 5 / 9 MC/DCmcdc 20.0% 1 / 5 Subroutinesub 100% 14 / 14 Podpod n/a totaltotal 89.3% 109 / 122 CC 16 SCAR 20.4
CC
16
Cov
91%
expire
14.5
_mru_history_too_big
2.0
_decrease_mru_target_size
2.0
CRAP 7.7
« Ref/CART.pm ↑ Summary Ref/Role/WithDoublyLinkedList.pm »
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.04';
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 21431 ▶sub _mru_history_too_big {
Subroutine _mru_history_too_big: called CC=2 SCAR=6.9
22 21431 my $self = shift;
23  
24 21431 ▶ $self->_mru_history_size
Branch: 1 branch
BranchTrueFalseTotal
$self->_mru_history_size and $self->_mru_history_size + $self->_mru_size == $self->size 21219 212 21431
25 and
26 $self->_mru_history_size + $self->_mru_size == $self->size;
27 }
28  
29 782 ▶sub _mfu_history_too_big {
Subroutine _mfu_history_too_big: called CC=1 SCAR=0.0
30 782 my $self = shift;
31  
32 782 $self->_index_size == $self->size * 2;
33 }
34  
35 2767 ▶sub _decrease_mru_target_size {
Subroutine _decrease_mru_target_size: called CC=2 SCAR=6.9
36 2767 my $self = shift;
37  
38 2767 ▶ if ( $self->_mru_target_size > 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_mru_target_size > 0) 692 2075 2767
39 692 my $adjustment = int( $self->_mru_history_size / $self->_mfu_history_size );
40 692 $self->_set_mru_target_size( max( 0, $self->_mru_target_size - max(1, $adjustment) ) );
41 }
42 }
43  
44 456 ▶sub _increase_mru_target_size {
Subroutine _increase_mru_target_size: called CC=1 SCAR=0.0
45 456 my $self = shift;
46  
47 456 my $adjustment = int( $self->_mfu_history_size / $self->_mru_history_size );
48 456 $self->_set_mru_target_size( min( $self->size, $self->_mru_target_size + max(1, $adjustment) ) );
49 }
50  
51 2767 ▶sub _restore_from_mfu_history {
Subroutine _restore_from_mfu_history: called CC=1 SCAR=0.0
52 2767 my ( $self, $e ) = @_;
53  
54 2767 $self->_mfu_push($e);
55 }
56  
57 456 ▶sub _restore_from_mru_history {
Subroutine _restore_from_mru_history: called CC=1 SCAR=0.0
58 456 my ( $self, $e ) = @_;
59  
60 456 $self->_mfu_push($e);
61 }
62  
63 24655 ▶sub expire {
Subroutine expire: called CC=14 SCAR=26.7
64 24655 my ( $self, $how_many ) = @_;
65  
66 24655 ▶ $how_many ||= 1;
Condition50%1/2
$how_many ||= 1
l!l
246550
MC/DC0%0/1
$how_many ||= 1
$how_many
Truth table50%1/2
A
A= $how_many
Aresult
11
00
67  
68 24655 ▶ if ( my $mru = $self->_mru ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $mru = $self->_mru) 21798 2857 24655
69 21798 my $cur = $self->_next($mru);
70  
71 # mru pool is too big
72 21798 ▶ while ( $cur and $self->_mru_size >= max(1,$self->_mru_target_size) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cur and $self->_mru_size >= &max(1, $self->_mru_target_size)) 21438 686 22124
Condition66%2/3
$cur and $self->_mru_size >= &max(1, $self->_mru_target_size)
!ll&&!rl&&r
068621438
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)
ABresult
0X0
100
111
73 21438 my $next = $self->_next($cur);
74  
75 21438 $self->_circular_splice($cur);
76  
77 21438 ▶ if ( $cur->[0] & Cache::Ref::CAR::Base::REF_BIT ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cur->[0] & 1) { } 326 21112 21438
78 326 $cur->[0] &= ~Cache::Ref::CAR::Base::REF_BIT; # turn off reference bit
79  
80 # move to t2 (mfu)
81 326 $self->_mfu_push($cur);
82  
83 326 $cur = $next;
84 } else {
85 # reference bit is off, which means this entry is freeable
86  
87 21112 delete $cur->[2]; # delete the value
88  
89 # move to history
90 # MFU_BIT not set
91  
92 21112 ▶ if ( $self->_mru_history_head ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_mru_history_head) { } 20973 139 21112
93 20973 $self->_set_next($cur, $self->_mru_history_head);
94 20973 $self->_set_prev($self->_mru_history_head, $cur);
95 } else {
96 139 $self->_set_next($cur, undef);
97 }
98  
99 21112 $self->_mru_history_head($cur);
100 21112 ▶ $self->_mru_history_tail($cur) unless $self->_mru_history_tail;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_mru_history_tail 139 20973 21112
101 21112 $self->_inc_mru_history_size;
102  
103  
104 21112 return;
105 }
106 }
107 }
108  
109 3543 for ( 1 .. $how_many ) {
110 3544 ▶ my $tail = $self->_mfu || last;
Condition50%1/2
$self->_mfu || (last)
l!l
35440
MC/DC0%0/1
$self->_mfu || (last)
$self->_mfu
Truth table50%1/2
A
A= $self->_mfu
Aresult
11
00
111 3544 ▶ my $cur = $self->_next($tail) || last;
Condition50%1/2
$self->_next($tail) || (last)
l!l
35440
MC/DC0%0/1
$self->_next($tail) || (last)
$self->_next($tail)
Truth table50%1/2
A
A= $self->_next($tail)
Aresult
11
00
112  
113 loop: {
114 3544 ▶ if ( $cur->[0] & Cache::Ref::CAR::Base::REF_BIT ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cur->[0] & 1) { } 0 3544 3544
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 3544 $self->_mfu($tail);
123 3544 $self->_circular_splice($cur);
124  
125 3544 delete $cur->[2]; # delete the value
126  
127 # move to history
128 3544 $cur->[0] |= Cache::Ref::CAR::Base::MFU_BIT;
129  
130 3544 ▶ if ( $self->_mfu_history_head ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_mfu_history_head) { } 2931 613 3544
131 2931 $self->_set_prev($self->_mfu_history_head, $cur);
132 2931 $self->_set_next($cur, $self->_mfu_history_head);
133 } else {
134 613 $self->_set_next($cur, undef);
135 }
136  
137 3544 $self->_mfu_history_head($cur);
138 3544 ▶ $self->_mfu_history_tail($cur) unless $self->_mfu_history_tail;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_mfu_history_tail 613 2931 3544
139 3544 $self->_inc_mfu_history_size;
140 }
141 }
142 }
143  
144 3543 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__
« Ref/CART.pm ↑ Summary Ref/Role/WithDoublyLinkedList.pm »