Ref/CAR/Base.pm

filter: Statementstmt 95.7% 157 / 164 Branchbran 86.1% 31 / 36 Conditioncond 82.3% 14 / 17 MC/DCmcdc 81.8% 9 / 11 Subroutinesub 100% 35 / 35 Podpod 42.8% 3 / 7 totaltotal 92.2% 249 / 270 CC 27 SCAR 12.5
CC
27
Cov
93%
set
9.0
remove
6.4
get
4.6
CRAP 3.5
« Ref/CAR.pm ↑ Summary Ref/Role/WithDoublyLinkedList.pm »
1 package Cache::Ref::CAR::Base;
2 1 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 $Cache::Ref::CAR::Base::AUTHORITY = 'cpan:NUFFIN';
4 }
5 1 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 $Cache::Ref::CAR::Base::VERSION = '0.05'; # TRIAL
7 }
8 # ABSTRACT: base clase for CAR and CART
9  
10 1 ▶use Moose::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 # TODO
13 # this needs lot of cleanup but I ran out of motivation. it works, though.
14 #
15 # the circular buffers should finish being implemented using the doubly linked
16 # list role
17 #
18 # CART needs a bunch of simplifications in the code
19 #
20 # the various linked list APIs should probably be consolidated to respect the
21 # MFU bit, with _move_to_history, _restore_from_history etc checking that
22 # instead of having two methods per each
23  
24 1 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
25  
26 sub REF_BIT () { 0x01 }
27 sub MFU_BIT () { 0x02 }
28 sub LONG_TERM_BIT () { 0x04 }
29  
30 1 requires qw(
31 _mru_history_too_big
32 _mfu_history_too_big
33  
34 _restore_from_mfu_history
35 _restore_from_mru_history
36  
37 _clear_additional
38  
39 _decrease_mru_target_size
40 _increase_mru_target_size
41 );
42  
43 with (
44 qw(
45 Cache::Ref::Role::API
46 Cache::Ref::Role::Index
47 ),
48 map {
49 1 ('Cache::Ref::Role::WithDoublyLinkedList' => { # FIXME can it just be circular too?
50 name => $_,
51 value_offset => 1, # the cache key
52 next_offset => 3,
53 prev_offset => 4,
54 }),
55 } qw(_mru_history _mfu_history), # b1, b2
56 );
57  
58 208450 ▶sub _next { $_[1][3] }
Subroutine _next: called CC=1 SCAR=0.0
59 221321 ▶sub _set_next {
Subroutine _set_next: called CC=1 SCAR=0.0
60 221321 my ( $self, $node, $next ) = @_;
61 221321 $node->[3] = $next;
62 }
63  
64 31855 ▶sub _prev { $_[1][4] }
Subroutine _prev: called CC=1 SCAR=0.0
65 220479 ▶sub _set_prev {
Subroutine _set_prev: called CC=1 SCAR=0.0
66 220479 my ( $self, $node, $prev ) = @_;
67 220479 $node->[4] = $prev;
68 }
69  
70 1 has size => (
71 isa => "Int",
72 is => "ro",
73 required => 1,
74 );
75  
76 1 foreach my $pool (qw(mfu mru)) { # t1, t2
77 2 has "_$pool" => ( is => "rw" ); # circular linked list tail
78  
79 2 foreach my $counter (qw(size history_size)) {
80 has "_${pool}_$counter" => (
81 #traits => [qw(Counter)], # too slow, not inlined, nytprof gives it about 60% of runtime =P
82 is => "ro",
83 writer => "_set_${pool}_$counter",
84 16 ▶ default => sub { 0 },
Subroutine __ANON__: called CC=1 SCAR=0.0
85 #handles => {
86 # "_inc_${pool}_$counter" => "inc",
87 # "_dec_${pool}_$counter" => "dec",
88 # "_reset_${pool}_$counter" => "reset",
89 #},
90 4 );
91 }
92 }
93  
94 4 ▶sub _reset_mru_size {
Subroutine _reset_mru_size: called CC=1 SCAR=0.0
95 4 my $self = shift;
96 4 $self->_set_mru_size(0);
97 }
98  
99 47315 ▶sub _inc_mru_size {
Subroutine _inc_mru_size: called CC=1 SCAR=0.0
100 47315 my $self = shift;
101 47315 $self->_set_mru_size( $self->_mru_size + 1 );
102 }
103  
104 47312 ▶sub _dec_mru_size {
Subroutine _dec_mru_size: called CC=1 SCAR=0.0
105 47312 my $self = shift;
106 47312 $self->_set_mru_size( $self->_mru_size - 1 );
107 }
108  
109 4 ▶sub _reset_mfu_size {
Subroutine _reset_mfu_size: called CC=1 SCAR=0.0
110 4 my $self = shift;
111 4 $self->_set_mfu_size(0);
112 }
113  
114 6492 ▶sub _inc_mfu_size {
Subroutine _inc_mfu_size: called CC=1 SCAR=0.0
115 6492 my $self = shift;
116 6492 $self->_set_mfu_size( $self->_mfu_size + 1 );
117 }
118  
119 6485 ▶sub _dec_mfu_size {
Subroutine _dec_mfu_size: called CC=1 SCAR=0.0
120 6485 my $self = shift;
121 6485 $self->_set_mfu_size( $self->_mfu_size - 1 );
122 }
123  
124 4 ▶sub _reset_mru_history_size {
Subroutine _reset_mru_history_size: called CC=1 SCAR=0.0
125 4 my $self = shift;
126 4 $self->_set_mru_history_size(0);
127 }
128  
129 44023 ▶sub _inc_mru_history_size {
Subroutine _inc_mru_history_size: called CC=1 SCAR=0.0
130 44023 my $self = shift;
131 44023 $self->_set_mru_history_size( $self->_mru_history_size + 1 );
132 }
133  
134 44010 ▶sub _dec_mru_history_size {
Subroutine _dec_mru_history_size: called CC=1 SCAR=0.0
135 44010 my $self = shift;
136 44010 $self->_set_mru_history_size( $self->_mru_history_size - 1 );
137 }
138  
139 4 ▶sub _reset_mfu_history_size {
Subroutine _reset_mfu_history_size: called CC=1 SCAR=0.0
140 4 my $self = shift;
141 4 $self->_set_mfu_history_size(0);
142 }
143  
144 6485 ▶sub _inc_mfu_history_size {
Subroutine _inc_mfu_history_size: called CC=1 SCAR=0.0
145 6485 my $self = shift;
146 6485 $self->_set_mfu_history_size( $self->_mfu_history_size + 1 );
147 }
148  
149 6480 ▶sub _dec_mfu_history_size {
Subroutine _dec_mfu_history_size: called CC=1 SCAR=0.0
150 6480 my $self = shift;
151 6480 $self->_set_mfu_history_size( $self->_mfu_history_size - 1 );
152 }
153  
154  
155 1 has _mru_target_size => ( # p
156 is => "ro",
157 writer => "_set_mru_target_size",
158 default => 0,
159 );
160  
161 2 ▶sub hit {
Subroutine hit: called CC=1 SCAR=0.0
Pod undocumented
162 2 my ( $self, @keys ) = @_;
163  
164 2 $self->_hit( [ grep { defined } $self->_index_get(@keys) ] );
165  
166 2 return;
167 }
168  
169 46 ▶sub peek {
Subroutine peek: called CC=4 SCAR=13.9
Pod undocumented
170 46 my ( $self, @keys ) = @_;
171  
172 46 my @ret;
173  
174 46 my @entries = $self->_index_get(@keys);
175  
176 46 ▶ return ( @keys == 1 ? ($entries[0] && $entries[0][2]) : map { $_ && $_->[2] } @entries );
Branch: 1 branch
BranchTrueFalseTotal
@keys == 1 ? : 42 4 46
Condition100%6/6
$entries[0] and $entries[0][2]
!ll&&!rl&&r
4830
$_ && $_->[2]
!ll&&!rl&&r
646
MC/DC100%2/2
$entries[0] and $entries[0][2]
$entries[0] $entries[0][2]
MC/DC100%2/2
$_ && $_->[2]
$_ $_->[2]
Truth table100%3/3
A and B
A= $entries[0]B= $entries[0][2]
ABresult
0X0
100
111
Truth table100%3/3
A && B
A= $_B= $_->[2]
ABresult
0X0
100
111
177 }
178  
179 82402 ▶sub get {
Subroutine get: called CC=4 SCAR=15.2
Pod documented
180 82402 my ( $self, @keys ) = @_;
181  
182 82402 my @ret;
183  
184 82402 my @entries = $self->_index_get(@keys);
185  
186 82402 $self->_hit( [ grep { defined } @entries ] );
187  
188 0 ▶ return ( @keys == 1 ? ($entries[0] && $entries[0][2]) : map { $_ && $_->[2] } @entries );
Branch: 1 branch
BranchTrueFalseTotal
@keys == 1 ? : 82402 0 82402
Condition50%3/6
$entries[0] and $entries[0][2]
!ll&&!rl&&r
44374613431894
$_ && $_->[2]
!ll&&!rl&&r
000
MC/DC100%2/2
$entries[0] and $entries[0][2]
$entries[0] $entries[0][2]
MC/DC0%0/2
$_ && $_->[2]
$_ $_->[2]
Truth table100%3/3
A and B
A= $entries[0]B= $entries[0][2]
ABresult
0X0
100
111
Truth table0%0/3
A && B
A= $_B= $_->[2]
ABresult
0X0
100
111
189 }
190  
191 53797 ▶sub _circular_splice {
Subroutine _circular_splice: called CC=4 SCAR=13.9
192 53797 my ( $self, $node ) = @_;
193  
194 53797 ▶ my $list = $node->[0] & MFU_BIT ? "_mfu" : "_mru";
Branch: 1 branch
BranchTrueFalseTotal
$node->[0] & 2 ? : 6485 47312 53797
195  
196 53797 my $next = $self->_next($node);
197  
198 53797 ▶ if ( $next == $node ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($next == $node) { } 21942 31855 53797
199 # this is the last element in the list
200 21942 $self->$list(undef);
201 } else {
202 31855 my $prev = $self->_prev($node);
203 31855 $self->_set_next( $prev, $next );
204 31855 $self->_set_prev( $next, $prev );
205  
206 31855 ▶ if ( $self->$list == $node ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->$list == $node) 0 31855 31855
207 0 $self->$list($prev); # only happens on remove()
208 }
209 }
210  
211 53797 $self->_set_next($node, undef);
212 53797 $self->_set_prev($node, undef);
213  
214 53797 $self->${\"_dec${list}_size"};
215 }
216  
217 6492 ▶sub _mfu_push {
Subroutine _mfu_push: called CC=1 SCAR=0.0
218 6492 my ( $self, $node ) = @_;
219 6492 $node->[0] |= MFU_BIT;
220 6492 $self->_circular_push($node);
221 }
222  
223 47315 ▶sub _mru_push {
Subroutine _mru_push: called CC=1 SCAR=0.0
224 47315 my ( $self, $node ) = @_;
225 47315 $node->[0] &= ~MFU_BIT;
226 47315 $self->_circular_push($node);
227 }
228  
229 53807 ▶sub _circular_push {
Subroutine _circular_push: called CC=3 SCAR=11.0
230 53807 my ( $self, $node ) = @_;
231  
232 53807 ▶ my $list = $node->[0] & MFU_BIT ? "_mfu" : "_mru";
Branch: 1 branch
BranchTrueFalseTotal
$node->[0] & 2 ? : 6492 47315 53807
233  
234 53807 ▶ if ( my $tail = $self->$list ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $tail = $self->$list) { } 31861 21946 53807
235 31861 my $head = $self->_next($tail);
236  
237 31861 $self->_set_next($tail, $node);
238 31861 $self->_set_prev($node, $tail);
239  
240 31861 $self->_set_next($node, $head);
241 31861 $self->_set_prev($head, $node);
242 } else {
243 21946 $self->_set_next($node, $node);
244 21946 $self->_set_prev($node, $node);
245 }
246  
247 53807 $self->${\"_inc${list}_size"};
248  
249 # $hand++
250 53807 $self->$list($node);
251 }
252  
253  
254 82406 ▶sub _hit {
Subroutine _hit: called CC=4 SCAR=13.9
255 82406 my ( $self, $e ) = @_;
256  
257 82406 foreach my $entry ( @$e ) {
258 38032 ▶ if ( exists $entry->[2] ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $entry->[2]) 31898 6134 38032
259 # if it's in T1 ∪ T2, the value is set
260 31898 ▶ $entry->[0] ||= 1;
Condition100%2/2
$entry->[0] ||= 1
l!l
303311567
MC/DC100%1/1
$entry->[0] ||= 1
$entry->[0]
Truth table100%2/2
A
A= $entry->[0]
Aresult
11
00
261 #} else {
262 # cache history hit
263 # has no effect until 'set'
264 }
265 }
266 }
267  
268 50522 ▶sub set {
Subroutine set: called CC=9 SCAR=22.0
Pod documented
269 50522 my ( $self, $key, $value ) = @_;
270  
271 50522 my $e = $self->_index_get($key);
272  
273 50522 ▶ if ( $e and exists $e->[2] ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($e and exists $e->[2]) 2 50520 50522
Condition100%3/3
$e and exists $e->[2]
!ll&&!rl&&r
4438561352
MC/DC100%2/2
$e and exists $e->[2]
$e exists $e->[2]
Truth table100%3/3
A and B
A= $eB= exists $e->[2]
ABresult
0X0
100
111
274 # just a value update
275 2 $self->_hit([$e]);
276 2 return $e->[2] = $value;
277 }
278  
279 # the live cache entries are full, we need to expire something
280 50520 ▶ if ( $self->_mru_size + $self->_mfu_size == $self->size ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_mru_size + $self->_mfu_size == $self->size) 50504 16 50520
281 50504 $self->expire(1);
282  
283 # if the entry wasn't in history we may need to free up something from
284 # there too, to make room for whatever just expired
285 50504 ▶ if ( !$e ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($e) 44369 6135 50504
286 44369 ▶ if ( $self->_mru_history_too_big ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->_mru_history_too_big) { } 42935 1434 44369
elsif ($self->_mfu_history_too_big) { } 1420 14 1434
287 42935 $self->_index_delete( $self->_mru_history_pop );
288 42935 $self->_dec_mru_history_size;
289 } elsif ( $self->_mfu_history_too_big ) {
290 1420 $self->_index_delete($self->_mfu_history_pop);
291 1420 $self->_dec_mfu_history_size;
292 }
293 }
294 }
295  
296 50520 ▶ if ( !$e ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $e) { } 44385 6135 50520
297 # cache directory miss
298 # this means the key is neither cached nor recently expired
299 44385 $self->_insert_new_entry( $key, $value );
300 } else {
301 # cache directory hit
302  
303 # restore from the appropriate history list
304 6135 ▶ if ( $e->[0] & MFU_BIT ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($e->[0] & 2) { } 5060 1075 6135
305 5060 $e->[0] &= ~MFU_BIT;
306  
307 5060 $self->_decrease_mru_target_size();
308  
309 5060 $self->_mfu_history_splice($e);
310 5060 $self->_dec_mfu_history_size;
311  
312 5060 $self->_restore_from_mfu_history($e);
313 } else {
314 1075 $self->_increase_mru_target_size();
315  
316 1075 $self->_mru_history_splice($e);
317 1075 $self->_dec_mru_history_size;
318  
319 1075 $self->_restore_from_mru_history($e);
320  
321 }
322  
323 # the entry has a key and flags but no value
324 # it's already indexed currectly, so no need for _index_set
325 6135 $e->[2] = $value;
326 }
327  
328 50520 return $value;
329 }
330  
331 44385 ▶sub _insert_new_entry {
Subroutine _insert_new_entry: called CC=1 SCAR=0.0
332 44385 my ( $self, $key, $value ) = @_;
333  
334 44385 my $e = [ 0, $key, $value ]; # 0 means no special bits are set
335  
336 # simply insert to the MRU pool
337 44385 $self->_mru_push($e);
338 44385 $self->_index_set( $key => $e );
339 }
340  
341 4 ▶sub clear {
Subroutine clear: called CC=1 SCAR=0.0
Pod undocumented
342 4 my $self = shift;
343  
344 4 $self->_index_clear;
345 4 $self->_mfu_history_clear;
346 4 $self->_mru_history_clear;
347 4 $self->_reset_mru_history_size;
348 4 $self->_reset_mfu_history_size;
349 4 $self->_reset_mfu_size;
350 4 $self->_reset_mru_size;
351 4 $self->_circular_clear("_mfu");
352 4 $self->_circular_clear("_mru");
353  
354 4 $self->_clear_additional;
355  
356 4 return;
357 }
358  
359 8 ▶sub _circular_clear {
Subroutine _circular_clear: called CC=2 SCAR=6.9
360 8 my ( $self, $list ) = @_;
361  
362 8 my $cur = $self->$list;
363 8 $self->$list(undef);
364  
365 8 ▶ while ( $cur ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cur) 14 8 22
366 14 my $next = $cur->[3];
367 14 @$cur = ();
368 14 $cur = $next;
369 }
370 }
371  
372 4 ▶sub DEMOLISH { shift->clear }
Subroutine DEMOLISH: called CC=1 SCAR=0.0
Pod undocumented
373  
374 2 ▶sub remove {
Subroutine remove: called CC=4 SCAR=18.6
Pod documented
375 2 my ( $self, @keys ) = @_;
376  
377 2 foreach my $e ( grep { defined } $self->_index_delete(@keys) ) {
378 2 ▶ if ( exists $e->[2] ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $e->[2]) { } 2 0 2
379 2 $self->_circular_splice($e);
380 } else {
381 0 ▶ if ( $e->[0] & MFU_BIT ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($e->[0] & 2) { } 0 0 0
382 0 $self->_mfu_history_pop;
383 0 $self->_dec_mfu_history_size;
384 } else {
385 0 $self->_mru_history_pop;
386 0 $self->_dec_mru_history_size;
387 }
388 }
389 }
390  
391 2 return;
392 }
393  
394 1 __PACKAGE__;
395  
396  
397 __END__
« Ref/CAR.pm ↑ Summary Ref/Role/WithDoublyLinkedList.pm »