Ref/CART.pm

filter: Statementstmt 94.6% 106 / 112 Branchbran 90.4% 38 / 42 Conditioncond 50.0% 6 / 12 MC/DCmcdc 14.2% 1 / 7 Subroutinesub 100% 20 / 20 Podpod n/a totaltotal 88.6% 171 / 193 CC 28 SCAR 23.6
CC
28
Cov
90%
expire
21.9
_mru_history_too_big
5.0
_restore_from_mfu_history
3.0
CRAP 10.6
↑ Summary Ref/CAR.pm »
1 package Cache::Ref::CART;
2 1 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 $Cache::Ref::CART::AUTHORITY = 'cpan:NUFFIN';
4 }
5 1 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 $Cache::Ref::CART::VERSION = '0.04';
7 }
8 # ABSTRACT: CAR with temporal filtering
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 1 has _long_term_utility_size => ( # q
22 is => "ro",
23 writer => "_set_long_term_utility_size",
24 default => 0,
25 );
26  
27 1 has _mru_history_target_size => ( # q
28 is => "ro",
29 writer => "_set_mru_history_target_size",
30 default => 0,
31 );
32  
33 3028 ▶sub _inc_long_term_utility_size {
Subroutine _inc_long_term_utility_size: called CC=1 SCAR=0.0
34 3028 my $self = shift;
35 3028 $self->_set_long_term_utility_size( $self->_long_term_utility_size + 1 );
36 }
37  
38 3024 ▶sub _dec_long_term_utility_size {
Subroutine _dec_long_term_utility_size: called CC=1 SCAR=0.0
39 3024 my $self = shift;
40 3024 $self->_set_long_term_utility_size( $self->_long_term_utility_size - 1 );
41 }
42  
43 2 ▶sub _reset_long_term_utility_size {
Subroutine _reset_long_term_utility_size: called CC=1 SCAR=0.0
44 2 my $self = shift;
45 2 $self->_set_long_term_utility_size(0);
46 }
47  
48 2 ▶sub _reset_mru_history_target_size {
Subroutine _reset_mru_history_target_size: called CC=1 SCAR=0.0
49 2 my $self = shift;
50 2 $self->_set_mru_history_target_size(0);
51 }
52  
53 22079 ▶sub _mru_history_too_big {
Subroutine _mru_history_too_big: called CC=5 SCAR=16.1
54 22079 my $self = shift;
55  
56 # only if there's something to purge
57 22079 ▶ return unless $self->_mru_history_size;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_mru_history_size 197 21882 22079
58  
59 # only if we need to purge
60 21882 ▶ return unless $self->_mru_history_size + $self->_mfu_history_size == $self->size + 1;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_mru_history_size + $self->_mfu_history_size == $self->size + 1 2 21880 21882
61  
62 # purge from here if there's nothing to purge from mfu
63 21880 ▶ return 1 if $self->_mfu_history_size == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $self->_mfu_history_size == 0 16225 5655 21880
64  
65 # or if the target size is too big
66 5655 ▶ return 1 if $self->_mru_history_size > $self->_mru_history_target_size;
Branch: 1 branch
BranchTrueFalseTotal
if $self->_mru_history_size > $self->_mru_history_target_size 5088 567 5655
67  
68 567 return;
69 }
70  
71 766 ▶sub _mfu_history_too_big {
Subroutine _mfu_history_too_big: called CC=3 SCAR=11.0
72 766 my $self = shift;
73  
74 766 ▶ return unless $self->_mfu_history_size;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_mfu_history_size 2 764 766
75  
76 # only purge if we actually need to
77 764 ▶ return 1 if $self->_mru_history_size + $self->_mfu_history_size == $self->size + 1;
Branch: 1 branch
BranchTrueFalseTotal
if $self->_mru_history_size + $self->_mfu_history_size == $self->size + 1 759 5 764
78  
79 5 return;
80 }
81  
82 591 ▶sub _increase_mru_target_size {
Subroutine _increase_mru_target_size: called CC=1 SCAR=0.0
83 591 my $self = shift;
84  
85 591 my $adjustment = int( ( $self->_mru_size + $self->_mfu_size - $self->_long_term_utility_size ) / $self->_mru_history_size );
86 591 $self->_set_mru_target_size( min( $self->size, $self->_mru_target_size + max(1, $adjustment) ) );
87 }
88  
89 2262 ▶sub _decrease_mru_target_size {
Subroutine _decrease_mru_target_size: called CC=1 SCAR=0.0
90 2262 my $self = shift;
91  
92 2262 my $adjustment = int( $self->_long_term_utility_size / $self->_mfu_history_size );
93 2262 $self->_set_mru_target_size( max( 0, $self->_mru_target_size - max(1, $adjustment) ) );
94 }
95  
96 1912 ▶sub _increase_mru_history_target_size {
Subroutine _increase_mru_history_target_size: called CC=1 SCAR=0.0
97 1912 my $self = shift;
98  
99 1912 $self->_set_mru_history_target_size( min($self->_mru_history_target_size + 1, 2 * $self->size - $self->_mru_size ) );
100 }
101  
102 3027 ▶sub _decrease_mru_history_target_size {
Subroutine _decrease_mru_history_target_size: called CC=1 SCAR=0.0
103 3027 my $self = shift;
104  
105 3027 $self->_set_mru_history_target_size( max($self->_mru_history_target_size - 1, $self->size - $self->_mru_size) );
106 }
107  
108 2262 ▶sub _restore_from_mfu_history {
Subroutine _restore_from_mfu_history: called CC=3 SCAR=11.0
109 2262 my ( $self, $e ) = @_;
110  
111 2262 ▶ if ( $self->_mfu_history_size + $self->_long_term_utility_size >= $self->size ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_mfu_history_size + $self->_long_term_utility_size >= $self->size) 1912 350 2262
112 1912 $self->_increase_mru_history_target_size();
113 }
114  
115 2262 ▶ die unless $e->[0] & Cache::Ref::CAR::Base::LONG_TERM_BIT();
Branch: 1 branch
BranchTrueFalseTotal
unless $e->[0] & 4 0 2262 2262
116 2262 $self->_inc_long_term_utility_size();
117  
118 2262 $self->_mru_push($e);
119 }
120  
121 591 ▶sub _restore_from_mru_history {
Subroutine _restore_from_mru_history: called CC=1 SCAR=0.0
122 591 my ( $self, $e ) = @_;
123  
124 591 $e->[0] |= Cache::Ref::CAR::Base::LONG_TERM_BIT();
125 591 $self->_inc_long_term_utility_size();
126  
127 591 $self->_mru_push($e);
128 }
129  
130 24933 ▶sub expire {
Subroutine expire: called CC=20 SCAR=30.9
131 24933 my ( $self, $how_many ) = @_;
132  
133 24933 ▶ $how_many ||= 1;
Condition50%1/2
$how_many ||= 1
l!l
249330
MC/DC0%0/1
$how_many ||= 1
$how_many
Truth table50%1/2
A
A= $how_many
Aresult
11
00
134  
135 24933 ▶ if ( my $mfu = $self->_mfu ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $mfu = $self->_mfu) 23669 1264 24933
136 23669 my $cur = $self->_next($mfu);
137  
138 # mru pool is too big
139 23669 ▶ while ( $cur and $cur->[0] & Cache::Ref::CAR::Base::REF_BIT ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cur and $cur->[0] & 1) 0 23669 23669
Condition33%1/3
$cur and $cur->[0] & 1
!ll&&!rl&&r
0236690
MC/DC0%0/2
$cur and $cur->[0] & 1
$cur $cur->[0] & 1
Truth table33%1/3
A and B
A= $curB= $cur->[0] & 1
ABresult
0X0
100
111
140 0 $self->_circular_splice($cur);
141  
142 0 $cur->[0] &= ~Cache::Ref::CAR::Base::REF_BIT; # turn off reference bit
143  
144 # move to t1 (mru)
145 0 $self->_mru_push($cur);
146 0 $cur = $self->_next($cur);
147  
148 0 ▶ if ( $self->_mfu_history_size + $self->_long_term_utility_size >= $self->size ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_mfu_history_size + $self->_long_term_utility_size >= $self->size) 0 0 0
149 0 $self->_increase_mru_history_target_size;
150 }
151 }
152 }
153  
154 24933 ▶ if ( my $mru = $self->_mru ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $mru = $self->_mru) 24932 1 24933
155 24932 my $cur = $self->_next($mru);
156  
157 24932 ▶ while ( $cur ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cur) 26141 2240 28381
158 26141 ▶ if ( $cur->[0] & Cache::Ref::CAR::Base::REF_BIT ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($cur->[0] & 1) { } 422 25719 26141
elsif ($cur->[0] & 4) { } 3027 22692 25719
159 422 $cur->[0] &= ~Cache::Ref::CAR::Base::REF_BIT;
160  
161 422 ▶ if ( $self->_mru_size >= max($self->_mru_history_size, $self->_mru_target_size + 1)
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_mru_size >= &max($self->_mru_history_size, $self->_mru_target_size + 1) and not $cur->[0] & 4) 175 247 422
Condition66%2/3
$self->_mru_size >= &max($self->_mru_history_size, $self->_mru_target_size + 1) and not $cur->[0] & 4
!ll&&!rl&&r
2470175
MC/DC50%1/2
$self->_mru_size >= &max($self->_mru_history_size, $self->_mru_target_size + 1) and not $cur->[0] & 4
$self->_mru_size >= &max($self->_mru_history_size, $self->_mru_target_size + 1) not $cur->[0] & 4
Truth table66%2/3
A and not(B)
A= $self->_mru_size >= &max($self->_mru_history_size, $self->_mru_target_size + 1)B= not $cur->[0] & 4
ABresult
0X0
100
111
162 and
163 not( $cur->[0] & Cache::Ref::CAR::Base::LONG_TERM_BIT )
164 ) {
165 # FIXME spec says 'x', is this the same as 'head'?
166 175 $cur->[0] |= Cache::Ref::CAR::Base::LONG_TERM_BIT;
167 175 $self->_inc_long_term_utility_size;
168 }
169  
170 # $hand++
171 422 $self->_mru($cur);
172 422 $cur = $self->_next($cur);
173 } elsif ( $cur->[0] & Cache::Ref::CAR::Base::LONG_TERM_BIT ) {
174 3027 my $next = $self->_next($cur);
175 3027 $self->_circular_splice($cur);
176 3027 $self->_mfu_push($cur);
177 3027 $cur = $self->_next($self->_mru);;
178  
179 3027 $self->_decrease_mru_history_target_size();
180 } else {
181 # found a candidate page for removal
182 22692 last;
183 }
184 }
185 }
186  
187 24933 for ( 1 .. $how_many ) {
188 24934 ▶ if ( $self->_mru_size >= max(1, $self->_mru_target_size) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_mru_size >= &max(1, $self->_mru_target_size)) { } 21910 3024 24934
189 21910 my $head = $self->_next($self->_mru);
190 21910 $self->_circular_splice($head);
191  
192 21910 ▶ if ( $self->_mru_history_head ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_mru_history_head) 21832 78 21910
193 21832 $self->_set_next($head, $self->_mru_history_head);
194 21832 $self->_set_prev($self->_mru_history_head, $head);
195 }
196  
197 21910 $self->_mru_history_head($head);
198 21910 ▶ $self->_mru_history_tail($head) unless $self->_mru_history_tail;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_mru_history_tail 78 21832 21910
199 21910 $self->_inc_mru_history_size;
200  
201 21910 delete $head->[2]; # delete the value
202 } else {
203 3024 ▶ my $tail = $self->_mfu || last;
Condition50%1/2
$self->_mfu || (last)
l!l
30240
MC/DC0%0/1
$self->_mfu || (last)
$self->_mfu
Truth table50%1/2
A
A= $self->_mfu
Aresult
11
00
204 3024 ▶ my $head = $self->_next($tail) || last;
Condition50%1/2
$self->_next($tail) || (last)
l!l
30240
MC/DC0%0/1
$self->_next($tail) || (last)
$self->_next($tail)
Truth table50%1/2
A
A= $self->_next($tail)
Aresult
11
00
205 3024 $self->_circular_splice($head);
206  
207 3024 $self->_dec_long_term_utility_size; # entries in mfu *always* have long term set
208  
209 3024 ▶ if ( $self->_mfu_history_head ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_mfu_history_head) 2702 322 3024
210 2702 $self->_set_next($head, $self->_mfu_history_head);
211 2702 $self->_set_prev($self->_mfu_history_head, $head);
212 }
213  
214 3024 $self->_mfu_history_head($head);
215 3024 ▶ $self->_mfu_history_tail($head) unless $self->_mfu_history_tail;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_mfu_history_tail 322 2702 3024
216 3024 $self->_inc_mfu_history_size;
217  
218 3024 delete $head->[2]; # delete the value
219 3024 $head->[0] |= Cache::Ref::CAR::Base::MFU_BIT;
220 }
221 }
222  
223 24933 return;
224 }
225  
226 2 ▶sub _clear_additional {
Subroutine _clear_additional: called CC=1 SCAR=0.0
227 2 my $self = shift;
228  
229 2 $self->_reset_long_term_utility_size;
230 2 $self->_reset_mru_history_target_size;
231 }
232  
233 1 __PACKAGE__->meta->make_immutable;
234  
235 1 __PACKAGE__;
236  
237  
238 # ex: set sw=4 et:
239  
240  
241 __END__
↑ Summary Ref/CAR.pm »