| 1 | package Tie::Cache::LRU::Array; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 3 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | 3 | ▶ | use Carp::Assert; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 3 | ▶ | use base qw(Tie::Cache::LRU::Virtual); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | 3 | ▶ | use constant SUCCESS => 1; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 3 | ▶ | use constant FAILURE => 0; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | # Node members. | ||||||||||||||||||||||||||||
| 12 | 3 | ▶ | use enum qw(KEY VALUE PREV NEXT); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | =pod | ||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | =head1 NAME | ||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | Tie::Cache::LRU::Array - Tie::Cache::LRU implemented using arrays | ||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | use Tie::Cache::LRU::Array; | ||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | tie %cache, 'Tie::Cache::LRU::Array', 500; | ||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | ...the rest is as Tie::Cache::LRU... | ||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | This is an alternative implementation of Tie::Cache::LRU using Perl | ||||||||||||||||||||||||||||
| 32 | arrays and built-in array operations instead of a linked list. The | ||||||||||||||||||||||||||||
| 33 | theory is that even though the algorithm employed is more expensive, | ||||||||||||||||||||||||||||
| 34 | it will still be faster for small cache sizes (where small <= ??) | ||||||||||||||||||||||||||||
| 35 | because the work is done inside perl (ie. higer big O, lower | ||||||||||||||||||||||||||||
| 36 | constant). If nothing else, it should use less memory. | ||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | =cut | ||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | 3 | ▶ | sub TIEHASH { | ||||||||||||||||||||||||||
|
Subroutine
TIEHASH: called CC=2 SCAR=7.0
| |||||||||||||||||||||||||||||
| 42 | 3 | my($class, $max_size) = @_; | |||||||||||||||||||||||||||
| 43 | 3 | my $self = bless {}, $class; | |||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | 3 | ▶ | $max_size = $class->DEFAULT_MAX_SIZE unless defined $max_size; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | 3 | $self->_init; | |||||||||||||||||||||||||||
| 48 | 3 | $self->max_size($max_size); | |||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | 3 | return $self; | |||||||||||||||||||||||||||
| 51 | } | ||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | 4 | ▶ | sub _init { | ||||||||||||||||||||||||||
|
Subroutine
_init: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 55 | 4 | my($self) = @_; | |||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | 4 | $self->{size} = 0; | |||||||||||||||||||||||||||
| 58 | 4 | $self->{index} = {}; | |||||||||||||||||||||||||||
| 59 | 4 | $self->{cache} = []; | |||||||||||||||||||||||||||
| 60 | 4 | $self->{low_idx} = -1; | |||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | 4 | return SUCCESS; | |||||||||||||||||||||||||||
| 63 | } | ||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | 6 | ▶ | sub FETCH { | ||||||||||||||||||||||||||
|
Subroutine
FETCH: called CC=2 SCAR=7.0
| |||||||||||||||||||||||||||||
| 67 | 6 | my($self, $key) = @_; | |||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | 6 | ▶ | return unless exists $self->{index}{$key}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | 6 | $self->_promote($key); | |||||||||||||||||||||||||||
| 72 | 6 | return $self->{cache}[-1][VALUE]; | |||||||||||||||||||||||||||
| 73 | } | ||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | 6 | ▶ | sub _promote { | ||||||||||||||||||||||||||
|
Subroutine
_promote: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 77 | 6 | my($self, $key) = @_; | |||||||||||||||||||||||||||
| 78 | 6 | my $cache = $self->{cache}; | |||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | 6 | my $idx = $self->{index}{$key}; | |||||||||||||||||||||||||||
| 81 | 6 | my $node = $cache->[$idx]; | |||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | 6 | ▶ | return $node if $idx == $#{$cache}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | 3 | $cache->[$idx] = undef; | |||||||||||||||||||||||||||
| 86 | 3 | push @$cache, $node; | |||||||||||||||||||||||||||
| 87 | 3 | $self->{index}{$key} = $#{$cache}; | |||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | 3 | ▶ | $self->_reorder_cache if $#$cache > $self->{size} * 2; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 90 | 3 | return $node; | |||||||||||||||||||||||||||
| 91 | } | ||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | 10 | ▶ | sub _cull { | ||||||||||||||||||||||||||
|
Subroutine
_cull: called CC=5 SCAR=16.1
| |||||||||||||||||||||||||||||
| 95 | 10 | my($self) = @_; | |||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | 10 | my $max_size = $self->max_size; | |||||||||||||||||||||||||||
| 98 | 10 | my $cache = $self->{cache}; | |||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||
| 100 | 10 | ▶ | $self->_reorder_cache if $#$cache > $self->{size} * 2; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | 10 | my $idx = $self->{low_idx}; | |||||||||||||||||||||||||||
| 103 | 10 | my $cache_size = $#{$cache}; | |||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | 10 | ▶ | for( ; $self->{size} > $max_size; $self->{size}-- ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 106 | 12 | my $node; | |||||||||||||||||||||||||||
| 107 | 12 | ▶ | do { $node = $cache->[++$idx]; } | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $node or $idx > $cache_size
MC/DC50%1/2
defined $node or $idx > $cache_size
defined $node
$idx > $cache_size
Truth table66%2/3
A or B
A= defined $nodeB= $idx > $cache_size
| |||||||||||||||||||||||||||||
| 108 | until defined $node or $idx > $cache_size; | ||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||
| 110 | 12 | delete $self->{index}{$node->[KEY]}; | |||||||||||||||||||||||||||
| 111 | 12 | $cache->[$idx] = undef; | |||||||||||||||||||||||||||
| 112 | } | ||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | 10 | $self->{low_idx} = $idx; | |||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||
| 116 | 10 | return SUCCESS; | |||||||||||||||||||||||||||
| 117 | } | ||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||
| 120 | 2 | ▶ | sub _reorder_cache { | ||||||||||||||||||||||||||
|
Subroutine
_reorder_cache: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||
| 121 | 2 | my($self) = shift; | |||||||||||||||||||||||||||
| 122 | 2 | my $cache = $self->{cache}; | |||||||||||||||||||||||||||
| 123 | 2 | my $next_spot = 0; | |||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||
| 125 | 2 | foreach my $idx (0..$#{$cache}) { | |||||||||||||||||||||||||||
| 126 | 18 | my $node = $cache->[$idx]; | |||||||||||||||||||||||||||
| 127 | 18 | ▶ | next unless defined $node; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 128 | 7 | ▶ | if( $idx == $next_spot ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 129 | 0 | $next_spot++; | |||||||||||||||||||||||||||
| 130 | } | ||||||||||||||||||||||||||||
| 131 | else { | ||||||||||||||||||||||||||||
| 132 | 7 | $cache->[$next_spot] = $node; | |||||||||||||||||||||||||||
| 133 | 7 | $self->{index}{$node->[KEY]} = $next_spot++; | |||||||||||||||||||||||||||
| 134 | } | ||||||||||||||||||||||||||||
| 135 | } | ||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||
| 137 | 2 | $#{$cache} = $next_spot - 1; | |||||||||||||||||||||||||||
| 138 | 2 | $self->{low_idx} = -1; | |||||||||||||||||||||||||||
| 139 | } | ||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||
| 142 | 2 | ▶ | sub EXISTS { | ||||||||||||||||||||||||||
|
Subroutine
EXISTS: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 143 | 2 | my($self, $key) = @_; | |||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||
| 145 | 2 | return exists $self->{index}{$key}; | |||||||||||||||||||||||||||
| 146 | } | ||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||
| 149 | 1 | ▶ | sub CLEAR { | ||||||||||||||||||||||||||
|
Subroutine
CLEAR: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 150 | 1 | my($self) = @_; | |||||||||||||||||||||||||||
| 151 | 1 | $self->_init; | |||||||||||||||||||||||||||
| 152 | } | ||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||
| 155 | 27 | ▶ | sub STORE { | ||||||||||||||||||||||||||
|
Subroutine
STORE: called CC=3 SCAR=11.1
| |||||||||||||||||||||||||||||
| 156 | 27 | my($self, $key, $val) = @_; | |||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||
| 158 | 27 | ▶ | if( exists $self->{index}{$key} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 159 | 0 | my $node = $self->_promote($key); | |||||||||||||||||||||||||||
| 160 | 0 | $node->[VALUE] = $val; | |||||||||||||||||||||||||||
| 161 | } | ||||||||||||||||||||||||||||
| 162 | else { | ||||||||||||||||||||||||||||
| 163 | 27 | my $node = []; | |||||||||||||||||||||||||||
| 164 | 27 | @{$node}[KEY, VALUE] = ($key, $val); | |||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||
| 166 | 27 | my $cache = $self->{cache}; | |||||||||||||||||||||||||||
| 167 | 27 | push @$cache, $node; | |||||||||||||||||||||||||||
| 168 | 27 | $self->{index}{$key} = $#{$cache}; | |||||||||||||||||||||||||||
| 169 | 27 | $self->{size}++; | |||||||||||||||||||||||||||
| 170 | 27 | ▶ | $self->_cull if $self->{size} > $self->{max_size}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 171 | } | ||||||||||||||||||||||||||||
| 172 | 27 | return SUCCESS; | |||||||||||||||||||||||||||
| 173 | } | ||||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||
| 175 | |||||||||||||||||||||||||||||
| 176 | 5 | ▶ | sub DELETE { | ||||||||||||||||||||||||||
|
Subroutine
DELETE: called CC=2 SCAR=7.0
| |||||||||||||||||||||||||||||
| 177 | 5 | my($self, $key) = @_; | |||||||||||||||||||||||||||
| 178 | |||||||||||||||||||||||||||||
| 179 | 5 | ▶ | return unless exists $self->{index}{$key}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 180 | |||||||||||||||||||||||||||||
| 181 | 5 | my $cache = $self->{cache}; | |||||||||||||||||||||||||||
| 182 | |||||||||||||||||||||||||||||
| 183 | 5 | my $idx = delete $self->{index}{$key}; | |||||||||||||||||||||||||||
| 184 | 5 | my $node = $cache->[$idx]; | |||||||||||||||||||||||||||
| 185 | 5 | $cache->[$idx] = undef; | |||||||||||||||||||||||||||
| 186 | |||||||||||||||||||||||||||||
| 187 | 5 | $self->{size}--; | |||||||||||||||||||||||||||
| 188 | |||||||||||||||||||||||||||||
| 189 | 5 | return $node->[VALUE]; | |||||||||||||||||||||||||||
| 190 | } | ||||||||||||||||||||||||||||
| 191 | |||||||||||||||||||||||||||||
| 192 | |||||||||||||||||||||||||||||
| 193 | 17 | ▶ | sub FIRSTKEY { | ||||||||||||||||||||||||||
|
Subroutine
FIRSTKEY: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||
| 194 | 17 | my($self) = shift; | |||||||||||||||||||||||||||
| 195 | |||||||||||||||||||||||||||||
| 196 | 17 | ▶ | return unless $self->{size}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 197 | |||||||||||||||||||||||||||||
| 198 | 12 | my $cache = $self->{cache}; | |||||||||||||||||||||||||||
| 199 | |||||||||||||||||||||||||||||
| 200 | 12 | my @nodes; | |||||||||||||||||||||||||||
| 201 | 12 | for my $node (@$cache) { | |||||||||||||||||||||||||||
| 202 | 84 | ▶ | push @nodes, $node if defined $node; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 203 | } | ||||||||||||||||||||||||||||
| 204 | |||||||||||||||||||||||||||||
| 205 | 12 | $self->{nodes} = \@nodes; | |||||||||||||||||||||||||||
| 206 | 12 | $self->NEXTKEY; | |||||||||||||||||||||||||||
| 207 | } | ||||||||||||||||||||||||||||
| 208 | |||||||||||||||||||||||||||||
| 209 | |||||||||||||||||||||||||||||
| 210 | 55 | ▶ | sub NEXTKEY { | ||||||||||||||||||||||||||
|
Subroutine
NEXTKEY: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 211 | 55 | my $self = shift; | |||||||||||||||||||||||||||
| 212 | |||||||||||||||||||||||||||||
| 213 | 55 | my $node = pop @{$self->{nodes}}; | |||||||||||||||||||||||||||
| 214 | 55 | return $node->[KEY]; | |||||||||||||||||||||||||||
| 215 | } | ||||||||||||||||||||||||||||
| 216 | |||||||||||||||||||||||||||||
| 217 | |||||||||||||||||||||||||||||
| 218 | 18 | ▶ | sub max_size { | ||||||||||||||||||||||||||
|
Subroutine
max_size: called CC=4 SCAR=14.0
Pod
documented
| |||||||||||||||||||||||||||||
| 219 | 18 | my($self) = shift; | |||||||||||||||||||||||||||
| 220 | |||||||||||||||||||||||||||||
| 221 | 18 | ▶ | if(@_) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 222 | 5 | my($new_max_size) = shift; | |||||||||||||||||||||||||||
| 223 | 5 | ▶ | assert( defined $new_max_size && $new_max_size !~ /\D/ ) if DEBUG; | ||||||||||||||||||||||||||
|
Condition33%1/3
defined $new_max_size && !($new_max_size =~ /\D/)
MC/DC0%0/2
defined $new_max_size && !($new_max_size =~ /\D/)
defined $new_max_size
!($new_max_size =~ /\D/)
Truth table33%1/3
A && not(B)
A= defined $new_max_sizeB= !($new_max_size =~ /\D/)
| |||||||||||||||||||||||||||||
| 224 | 5 | $self->{max_size} = $new_max_size; | |||||||||||||||||||||||||||
| 225 | |||||||||||||||||||||||||||||
| 226 | 5 | ▶ | $self->_cull if $self->{size} > $new_max_size; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 227 | |||||||||||||||||||||||||||||
| 228 | 5 | return SUCCESS; | |||||||||||||||||||||||||||
| 229 | } | ||||||||||||||||||||||||||||
| 230 | else { | ||||||||||||||||||||||||||||
| 231 | 13 | return $self->{max_size}; | |||||||||||||||||||||||||||
| 232 | } | ||||||||||||||||||||||||||||
| 233 | } | ||||||||||||||||||||||||||||
| 234 | |||||||||||||||||||||||||||||
| 235 | |||||||||||||||||||||||||||||
| 236 | 6 | ▶ | sub curr_size { | ||||||||||||||||||||||||||
|
Subroutine
curr_size: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 237 | 6 | my($self) = shift; | |||||||||||||||||||||||||||
| 238 | |||||||||||||||||||||||||||||
| 239 | 6 | assert(!@_) if DEBUG; | |||||||||||||||||||||||||||
| 240 | |||||||||||||||||||||||||||||
| 241 | 6 | return $self->{size}; | |||||||||||||||||||||||||||
| 242 | } | ||||||||||||||||||||||||||||
| 243 | |||||||||||||||||||||||||||||
| 244 | |||||||||||||||||||||||||||||
| 245 | 3 | ▶ | sub DESTROY { | ||||||||||||||||||||||||||
|
Subroutine
DESTROY: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 246 | 3 | my $self = shift; | |||||||||||||||||||||||||||
| 247 | |||||||||||||||||||||||||||||
| 248 | # Break a possible circular reference, just to be thorough. | ||||||||||||||||||||||||||||
| 249 | 3 | $self->{nodes} = []; | |||||||||||||||||||||||||||
| 250 | } | ||||||||||||||||||||||||||||
| 251 | |||||||||||||||||||||||||||||
| 252 | |||||||||||||||||||||||||||||
| 253 | =pod | ||||||||||||||||||||||||||||
| 254 | |||||||||||||||||||||||||||||
| 255 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 256 | |||||||||||||||||||||||||||||
| 257 | Michael G Schwern <schwern@pobox.com> | ||||||||||||||||||||||||||||
| 258 | |||||||||||||||||||||||||||||
| 259 | =head1 SEE ALSO | ||||||||||||||||||||||||||||
| 260 | |||||||||||||||||||||||||||||
| 261 | L<Tie::Cache::LRU>, L<Tie::Cache::LRU::Virtual>, L<Tie::Cache> | ||||||||||||||||||||||||||||
| 262 | |||||||||||||||||||||||||||||
| 263 | =cut | ||||||||||||||||||||||||||||
| 264 | |||||||||||||||||||||||||||||
| 265 | 3 | 1; | |||||||||||||||||||||||||||