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/Role/WithDoublyLinkedList.pm ↑ Summary Ref/CLOCK/Base.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.04';
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 211801 ▶sub _next { $_[1][3] }
Subroutine _next: called CC=1 SCAR=0.0
59 219213 ▶sub _set_next {
Subroutine _set_next: called CC=1 SCAR=0.0
60 219213 my ( $self, $node, $next ) = @_;
61 219213 $node->[3] = $next;
62 }
63  
64 32058 ▶sub _prev { $_[1][4] }
Subroutine _prev: called CC=1 SCAR=0.0
65 218461 ▶sub _set_prev {
Subroutine _set_prev: called CC=1 SCAR=0.0
66 218461 my ( $self, $node, $prev ) = @_;
67 218461 $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 46379 ▶sub _inc_mru_size {
Subroutine _inc_mru_size: called CC=1 SCAR=0.0
100 46379 my $self = shift;
101 46379 $self->_set_mru_size( $self->_mru_size + 1 );
102 }
103  
104 46377 ▶sub _dec_mru_size {
Subroutine _dec_mru_size: called CC=1 SCAR=0.0
105 46377 my $self = shift;
106 46377 $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 6576 ▶sub _inc_mfu_size {
Subroutine _inc_mfu_size: called CC=1 SCAR=0.0
115 6576 my $self = shift;
116 6576 $self->_set_mfu_size( $self->_mfu_size + 1 );
117 }
118  
119 6568 ▶sub _dec_mfu_size {
Subroutine _dec_mfu_size: called CC=1 SCAR=0.0
120 6568 my $self = shift;
121 6568 $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 43022 ▶sub _inc_mru_history_size {
Subroutine _inc_mru_history_size: called CC=1 SCAR=0.0
130 43022 my $self = shift;
131 43022 $self->_set_mru_history_size( $self->_mru_history_size + 1 );
132 }
133  
134 43009 ▶sub _dec_mru_history_size {
Subroutine _dec_mru_history_size: called CC=1 SCAR=0.0
135 43009 my $self = shift;
136 43009 $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 6568 ▶sub _inc_mfu_history_size {
Subroutine _inc_mfu_history_size: called CC=1 SCAR=0.0
145 6568 my $self = shift;
146 6568 $self->_set_mfu_history_size( $self->_mfu_history_size + 1 );
147 }
148  
149 6563 ▶sub _dec_mfu_history_size {
Subroutine _dec_mfu_history_size: called CC=1 SCAR=0.0
150 6563 my $self = shift;
151 6563 $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
43515607532812
$_ && $_->[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 52945 ▶sub _circular_splice {
Subroutine _circular_splice: called CC=4 SCAR=13.9
192 52945 my ( $self, $node ) = @_;
193  
194 52945 ▶ my $list = $node->[0] & MFU_BIT ? "_mfu" : "_mru";
Branch: 1 branch
BranchTrueFalseTotal
$node->[0] & 2 ? : 6568 46377 52945
195  
196 52945 my $next = $self->_next($node);
197  
198 52945 ▶ if ( $next == $node ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($next == $node) { } 20887 32058 52945
199 # this is the last element in the list
200 20887 $self->$list(undef);
201 } else {
202 32058 my $prev = $self->_prev($node);
203 32058 $self->_set_next( $prev, $next );
204 32058 $self->_set_prev( $next, $prev );
205  
206 32058 ▶ if ( $self->$list == $node ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->$list == $node) 0 32058 32058
207 0 $self->$list($prev); # only happens on remove()
208 }
209 }
210  
211 52945 $self->_set_next($node, undef);
212 52945 $self->_set_prev($node, undef);
213  
214 52945 $self->${\"_dec${list}_size"};
215 }
216  
217 6576 ▶sub _mfu_push {
Subroutine _mfu_push: called CC=1 SCAR=0.0
218 6576 my ( $self, $node ) = @_;
219 6576 $node->[0] |= MFU_BIT;
220 6576 $self->_circular_push($node);
221 }
222  
223 46379 ▶sub _mru_push {
Subroutine _mru_push: called CC=1 SCAR=0.0
224 46379 my ( $self, $node ) = @_;
225 46379 $node->[0] &= ~MFU_BIT;
226 46379 $self->_circular_push($node);
227 }
228  
229 52955 ▶sub _circular_push {
Subroutine _circular_push: called CC=3 SCAR=11.0
230 52955 my ( $self, $node ) = @_;
231  
232 52955 ▶ my $list = $node->[0] & MFU_BIT ? "_mfu" : "_mru";
Branch: 1 branch
BranchTrueFalseTotal
$node->[0] & 2 ? : 6576 46379 52955
233  
234 52955 ▶ if ( my $tail = $self->$list ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $tail = $self->$list) { } 32065 20890 52955
235 32065 my $head = $self->_next($tail);
236  
237 32065 $self->_set_next($tail, $node);
238 32065 $self->_set_prev($node, $tail);
239  
240 32065 $self->_set_next($node, $head);
241 32065 $self->_set_prev($head, $node);
242 } else {
243 20890 $self->_set_next($node, $node);
244 20890 $self->_set_prev($node, $node);
245 }
246  
247 52955 $self->${\"_inc${list}_size"};
248  
249 # $hand++
250 52955 $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 38891 ▶ if ( exists $entry->[2] ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $entry->[2]) 32816 6075 38891
259 # if it's in T1 ∪ T2, the value is set
260 32816 ▶ $entry->[0] ||= 1;
Condition100%2/2
$entry->[0] ||= 1
l!l
32068748
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 49604 ▶sub set {
Subroutine set: called CC=9 SCAR=22.0
Pod documented
269 49604 my ( $self, $key, $value ) = @_;
270  
271 49604 my $e = $self->_index_get($key);
272  
273 49604 ▶ if ( $e and exists $e->[2] ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($e and exists $e->[2]) 2 49602 49604
Condition100%3/3
$e and exists $e->[2]
!ll&&!rl&&r
4352660762
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 49602 ▶ if ( $self->_mru_size + $self->_mfu_size == $self->size ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_mru_size + $self->_mfu_size == $self->size) 49586 16 49602
281 49586 $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 49586 ▶ if ( !$e ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($e) 43510 6076 49586
286 43510 ▶ if ( $self->_mru_history_too_big ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->_mru_history_too_big) { } 41962 1548 43510
elsif ($self->_mfu_history_too_big) { } 1534 14 1548
287 41962 $self->_index_delete( $self->_mru_history_pop );
288 41962 $self->_dec_mru_history_size;
289 } elsif ( $self->_mfu_history_too_big ) {
290 1534 $self->_index_delete($self->_mfu_history_pop);
291 1534 $self->_dec_mfu_history_size;
292 }
293 }
294 }
295  
296 49602 ▶ if ( !$e ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $e) { } 43526 6076 49602
297 # cache directory miss
298 # this means the key is neither cached nor recently expired
299 43526 $self->_insert_new_entry( $key, $value );
300 } else {
301 # cache directory hit
302  
303 # restore from the appropriate history list
304 6076 ▶ if ( $e->[0] & MFU_BIT ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($e->[0] & 2) { } 5029 1047 6076
305 5029 $e->[0] &= ~MFU_BIT;
306  
307 5029 $self->_decrease_mru_target_size();
308  
309 5029 $self->_mfu_history_splice($e);
310 5029 $self->_dec_mfu_history_size;
311  
312 5029 $self->_restore_from_mfu_history($e);
313 } else {
314 1047 $self->_increase_mru_target_size();
315  
316 1047 $self->_mru_history_splice($e);
317 1047 $self->_dec_mru_history_size;
318  
319 1047 $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 6076 $e->[2] = $value;
326 }
327  
328 49602 return $value;
329 }
330  
331 43526 ▶sub _insert_new_entry {
Subroutine _insert_new_entry: called CC=1 SCAR=0.0
332 43526 my ( $self, $key, $value ) = @_;
333  
334 43526 my $e = [ 0, $key, $value ]; # 0 means no special bits are set
335  
336 # simply insert to the MRU pool
337 43526 $self->_mru_push($e);
338 43526 $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) 13 8 21
366 13 my $next = $cur->[3];
367 13 @$cur = ();
368 13 $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/Role/WithDoublyLinkedList.pm ↑ Summary Ref/CLOCK/Base.pm »