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.05'; # TRIAL
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 2980 ▶sub _inc_long_term_utility_size {
Subroutine _inc_long_term_utility_size: called CC=1 SCAR=0.0
34 2980 my $self = shift;
35 2980 $self->_set_long_term_utility_size( $self->_long_term_utility_size + 1 );
36 }
37  
38 2976 ▶sub _dec_long_term_utility_size {
Subroutine _dec_long_term_utility_size: called CC=1 SCAR=0.0
39 2976 my $self = shift;
40 2976 $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 22971 ▶sub _mru_history_too_big {
Subroutine _mru_history_too_big: called CC=5 SCAR=16.1
54 22971 my $self = shift;
55  
56 # only if there's something to purge
57 22971 ▶ return unless $self->_mru_history_size;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_mru_history_size 192 22779 22971
58  
59 # only if we need to purge
60 22779 ▶ 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 22777 22779
61  
62 # purge from here if there's nothing to purge from mfu
63 22777 ▶ return 1 if $self->_mfu_history_size == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $self->_mfu_history_size == 0 9846 12931 22777
64  
65 # or if the target size is too big
66 12931 ▶ 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 12431 500 12931
67  
68 500 return;
69 }
70  
71 694 ▶sub _mfu_history_too_big {
Subroutine _mfu_history_too_big: called CC=3 SCAR=11.0
72 694 my $self = shift;
73  
74 694 ▶ return unless $self->_mfu_history_size;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_mfu_history_size 2 692 694
75  
76 # only purge if we actually need to
77 692 ▶ 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 687 5 692
78  
79 5 return;
80 }
81  
82 643 ▶sub _increase_mru_target_size {
Subroutine _increase_mru_target_size: called CC=1 SCAR=0.0
83 643 my $self = shift;
84  
85 643 my $adjustment = int( ( $self->_mru_size + $self->_mfu_size - $self->_long_term_utility_size ) / $self->_mru_history_size );
86 643 $self->_set_mru_target_size( min( $self->size, $self->_mru_target_size + max(1, $adjustment) ) );
87 }
88  
89 2287 ▶sub _decrease_mru_target_size {
Subroutine _decrease_mru_target_size: called CC=1 SCAR=0.0
90 2287 my $self = shift;
91  
92 2287 my $adjustment = int( $self->_long_term_utility_size / $self->_mfu_history_size );
93 2287 $self->_set_mru_target_size( max( 0, $self->_mru_target_size - max(1, $adjustment) ) );
94 }
95  
96 1986 ▶sub _increase_mru_history_target_size {
Subroutine _increase_mru_history_target_size: called CC=1 SCAR=0.0
97 1986 my $self = shift;
98  
99 1986 $self->_set_mru_history_target_size( min($self->_mru_history_target_size + 1, 2 * $self->size - $self->_mru_size ) );
100 }
101  
102 2979 ▶sub _decrease_mru_history_target_size {
Subroutine _decrease_mru_history_target_size: called CC=1 SCAR=0.0
103 2979 my $self = shift;
104  
105 2979 $self->_set_mru_history_target_size( max($self->_mru_history_target_size - 1, $self->size - $self->_mru_size) );
106 }
107  
108 2287 ▶sub _restore_from_mfu_history {
Subroutine _restore_from_mfu_history: called CC=3 SCAR=11.0
109 2287 my ( $self, $e ) = @_;
110  
111 2287 ▶ 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) 1986 301 2287
112 1986 $self->_increase_mru_history_target_size();
113 }
114  
115 2287 ▶ die unless $e->[0] & Cache::Ref::CAR::Base::LONG_TERM_BIT();
Branch: 1 branch
BranchTrueFalseTotal
unless $e->[0] & 4 0 2287 2287
116 2287 $self->_inc_long_term_utility_size();
117  
118 2287 $self->_mru_push($e);
119 }
120  
121 643 ▶sub _restore_from_mru_history {
Subroutine _restore_from_mru_history: called CC=1 SCAR=0.0
122 643 my ( $self, $e ) = @_;
123  
124 643 $e->[0] |= Cache::Ref::CAR::Base::LONG_TERM_BIT();
125 643 $self->_inc_long_term_utility_size();
126  
127 643 $self->_mru_push($e);
128 }
129  
130 25902 ▶sub expire {
Subroutine expire: called CC=20 SCAR=30.9
131 25902 my ( $self, $how_many ) = @_;
132  
133 25902 ▶ $how_many ||= 1;
Condition50%1/2
$how_many ||= 1
l!l
259020
MC/DC0%0/1
$how_many ||= 1
$how_many
Truth table50%1/2
A
A= $how_many
Aresult
11
00
134  
135 25902 ▶ if ( my $mfu = $self->_mfu ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $mfu = $self->_mfu) 17110 8792 25902
136 17110 my $cur = $self->_next($mfu);
137  
138 # mru pool is too big
139 17110 ▶ while ( $cur and $cur->[0] & Cache::Ref::CAR::Base::REF_BIT ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cur and $cur->[0] & 1) 0 17110 17110
Condition33%1/3
$cur and $cur->[0] & 1
!ll&&!rl&&r
0171100
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 25902 ▶ if ( my $mru = $self->_mru ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $mru = $self->_mru) 25901 1 25902
155 25901 my $cur = $self->_next($mru);
156  
157 25901 ▶ while ( $cur ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cur) 27822 2317 30139
158 27822 ▶ if ( $cur->[0] & Cache::Ref::CAR::Base::REF_BIT ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($cur->[0] & 1) { } 1259 26563 27822
elsif ($cur->[0] & 4) { } 2979 23584 26563
159 1259 $cur->[0] &= ~Cache::Ref::CAR::Base::REF_BIT;
160  
161 1259 ▶ 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) 50 1209 1259
Condition66%2/3
$self->_mru_size >= &max($self->_mru_history_size, $self->_mru_target_size + 1) and not $cur->[0] & 4
!ll&&!rl&&r
1209050
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 50 $cur->[0] |= Cache::Ref::CAR::Base::LONG_TERM_BIT;
167 50 $self->_inc_long_term_utility_size;
168 }
169  
170 # $hand++
171 1259 $self->_mru($cur);
172 1259 $cur = $self->_next($cur);
173 } elsif ( $cur->[0] & Cache::Ref::CAR::Base::LONG_TERM_BIT ) {
174 2979 my $next = $self->_next($cur);
175 2979 $self->_circular_splice($cur);
176 2979 $self->_mfu_push($cur);
177 2979 $cur = $self->_next($self->_mru);;
178  
179 2979 $self->_decrease_mru_history_target_size();
180 } else {
181 # found a candidate page for removal
182 23584 last;
183 }
184 }
185 }
186  
187 25902 for ( 1 .. $how_many ) {
188 25903 ▶ if ( $self->_mru_size >= max(1, $self->_mru_target_size) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_mru_size >= &max(1, $self->_mru_target_size)) { } 22927 2976 25903
189 22927 my $head = $self->_next($self->_mru);
190 22927 $self->_circular_splice($head);
191  
192 22927 ▶ if ( $self->_mru_history_head ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_mru_history_head) 22755 172 22927
193 22755 $self->_set_next($head, $self->_mru_history_head);
194 22755 $self->_set_prev($self->_mru_history_head, $head);
195 }
196  
197 22927 $self->_mru_history_head($head);
198 22927 ▶ $self->_mru_history_tail($head) unless $self->_mru_history_tail;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_mru_history_tail 172 22755 22927
199 22927 $self->_inc_mru_history_size;
200  
201 22927 delete $head->[2]; # delete the value
202 } else {
203 2976 ▶ my $tail = $self->_mfu || last;
Condition50%1/2
$self->_mfu || (last)
l!l
29760
MC/DC0%0/1
$self->_mfu || (last)
$self->_mfu
Truth table50%1/2
A
A= $self->_mfu
Aresult
11
00
204 2976 ▶ my $head = $self->_next($tail) || last;
Condition50%1/2
$self->_next($tail) || (last)
l!l
29760
MC/DC0%0/1
$self->_next($tail) || (last)
$self->_next($tail)
Truth table50%1/2
A
A= $self->_next($tail)
Aresult
11
00
205 2976 $self->_circular_splice($head);
206  
207 2976 $self->_dec_long_term_utility_size; # entries in mfu *always* have long term set
208  
209 2976 ▶ if ( $self->_mfu_history_head ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_mfu_history_head) 2641 335 2976
210 2641 $self->_set_next($head, $self->_mfu_history_head);
211 2641 $self->_set_prev($self->_mfu_history_head, $head);
212 }
213  
214 2976 $self->_mfu_history_head($head);
215 2976 ▶ $self->_mfu_history_tail($head) unless $self->_mfu_history_tail;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_mfu_history_tail 335 2641 2976
216 2976 $self->_inc_mfu_history_size;
217  
218 2976 delete $head->[2]; # delete the value
219 2976 $head->[0] |= Cache::Ref::CAR::Base::MFU_BIT;
220 }
221 }
222  
223 25902 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 »