| 1 | #------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 2 | # | ||||||||||||||||||||||||||||
| 3 | # Sub::Contract::Memoizer - Implement the memoizing behaviour of a contract | ||||||||||||||||||||||||||||
| 4 | # | ||||||||||||||||||||||||||||
| 5 | # $Id: Memoizer.pm,v 1.12 2009/06/16 12:23:58 erwan_lemonnier Exp $ | ||||||||||||||||||||||||||||
| 6 | # | ||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | package Sub::Contract::Memoizer; | ||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | 23 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 23 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | 23 | ▶ | use Carp qw(croak confess); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | 23 | ▶ | use Data::Dumper; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | 23 | ▶ | use Symbol; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 15 | 23 | ▶ | use Sub::Contract::Cache; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | 23 | our $VERSION = '0.12'; | |||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | #--------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 20 | # | ||||||||||||||||||||||||||||
| 21 | # | ||||||||||||||||||||||||||||
| 22 | # the cache profiler | ||||||||||||||||||||||||||||
| 23 | # | ||||||||||||||||||||||||||||
| 24 | # | ||||||||||||||||||||||||||||
| 25 | #--------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | # turn on cache statistics | ||||||||||||||||||||||||||||
| 28 | 23 | my $CACHE_STATS_ON = 0; | |||||||||||||||||||||||||||
| 29 | 23 | ▶ | if (defined $ENV{PERL5SUBCONTRACTSTATS} && $ENV{PERL5SUBCONTRACTSTATS} eq '1') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $ENV{"PERL5SUBCONTRACTSTATS"} and $ENV{"PERL5SUBCONTRACTSTATS"} eq 1
MC/DC50%1/2
defined $ENV{"PERL5SUBCONTRACTSTATS"} and $ENV{"PERL5SUBCONTRACTSTATS"} eq 1
defined $ENV{"PERL5SUBCONTRACTSTATS"}
$ENV{"PERL5SUBCONTRACTSTATS"} eq 1
Truth table66%2/3
A and B
A= defined $ENV{"PERL5SUBCONTRACTSTATS"}B= $ENV{"PERL5SUBCONTRACTSTATS"} eq 1
| |||||||||||||||||||||||||||||
| 30 | 1 | $CACHE_STATS_ON = 1; | |||||||||||||||||||||||||||
| 31 | } | ||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | 23 | my %CACHE_STATS; | |||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | # for the compiler to know if the cache profiler is on | ||||||||||||||||||||||||||||
| 36 | 32 | ▶ | sub _is_profiler_on { | ||||||||||||||||||||||||||
|
Subroutine
_is_profiler_on: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 37 | 32 | return $CACHE_STATS_ON; | |||||||||||||||||||||||||||
| 38 | } | ||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | # private functions used to update stat counters | ||||||||||||||||||||||||||||
| 41 | 27 | ▶ | sub _incr_miss { | ||||||||||||||||||||||||||
|
Subroutine
_incr_miss: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 42 | 27 | $CACHE_STATS{$_[0]}->{calls}++; | |||||||||||||||||||||||||||
| 43 | } | ||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | 36 | ▶ | sub _incr_hit { | ||||||||||||||||||||||||||
|
Subroutine
_incr_hit: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 46 | 36 | $CACHE_STATS{$_[0]}->{calls}++; | |||||||||||||||||||||||||||
| 47 | 36 | $CACHE_STATS{$_[0]}->{hits}++; | |||||||||||||||||||||||||||
| 48 | } | ||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | 27 | ▶ | sub _incr_max_reached { | ||||||||||||||||||||||||||
|
Subroutine
_incr_max_reached: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 51 | # we don't want to increase this counter from within Sub::Contract::Cache->set for | ||||||||||||||||||||||||||||
| 52 | # efficiency reasons, so we check after a set if the cache only contains 1 entry. | ||||||||||||||||||||||||||||
| 53 | # if so, either it is the first entry (and then it happens only once and we count | ||||||||||||||||||||||||||||
| 54 | # it away by setting maxsize to -1 at start) or the cache just got cleared | ||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | 27 | ▶ | if ($CACHE_STATS{$_[0]}->{cache}->{cache_size} == 1) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 57 | 8 | $CACHE_STATS{$_[0]}->{maxsize}++; | |||||||||||||||||||||||||||
| 58 | } | ||||||||||||||||||||||||||||
| 59 | } | ||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | 4 | ▶ | sub _get_cache_stats_report { | ||||||||||||||||||||||||||
|
Subroutine
_get_cache_stats_report: called CC=6 SCAR=17.9
| |||||||||||||||||||||||||||||
| 62 | 4 | my $report = ""; | |||||||||||||||||||||||||||
| 63 | 4 | my ($cntfnc,$cnthit,$cntqry,$cntclr) = (0,0,0,0); | |||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | # find out max lentgh of functions fully qualified names | ||||||||||||||||||||||||||||
| 66 | 4 | my $length = 0; | |||||||||||||||||||||||||||
| 67 | 4 | foreach my $func (sort keys %CACHE_STATS) { | |||||||||||||||||||||||||||
| 68 | 28 | ▶ | $length = length $func if (length $func > $length); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 69 | } | ||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | 4 | $report .= "------------------------------------------------------\n"; | |||||||||||||||||||||||||||
| 72 | 4 | $report .= "Statistics from Sub::Contract's function result cache:\n"; | |||||||||||||||||||||||||||
| 73 | 4 | $report .= "\n"; | |||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | 4 | foreach my $func (sort keys %CACHE_STATS) { | |||||||||||||||||||||||||||
| 76 | 28 | my $hits = $CACHE_STATS{$func}->{hits}; | |||||||||||||||||||||||||||
| 77 | 28 | my $calls = $CACHE_STATS{$func}->{calls}; | |||||||||||||||||||||||||||
| 78 | 28 | my $max = $CACHE_STATS{$func}->{maxsize}; | |||||||||||||||||||||||||||
| 79 | 28 | ▶ | $max = 0 if ($max == -1); # see _incr_max_reached for explanation | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 80 | 28 | $cntfnc++; | |||||||||||||||||||||||||||
| 81 | 28 | $cnthit += $hits; | |||||||||||||||||||||||||||
| 82 | 28 | $cntqry += $calls; | |||||||||||||||||||||||||||
| 83 | 28 | $cntclr += $max; | |||||||||||||||||||||||||||
| 84 | 28 | ▶ | if ($calls) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 85 | 28 | my $rate = int(1000*$hits/$calls)/10; | |||||||||||||||||||||||||||
| 86 | 28 | $report .= " ".sprintf("%-".$length."s :",$func)." $rate % hits (calls: $calls, hits: $hits, max size reached: $max)\n"; | |||||||||||||||||||||||||||
| 87 | } | ||||||||||||||||||||||||||||
| 88 | } | ||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | 4 | $report .= "\n"; | |||||||||||||||||||||||||||
| 91 | 4 | $report .= " number of caches: $cntfnc\n"; | |||||||||||||||||||||||||||
| 92 | 4 | $report .= " total calls: $cntqry\n"; | |||||||||||||||||||||||||||
| 93 | 4 | $report .= " total hits: $cnthit\n"; | |||||||||||||||||||||||||||
| 94 | 4 | $report .= " total max size reached: $cntclr\n"; | |||||||||||||||||||||||||||
| 95 | 4 | $report .= "\n"; | |||||||||||||||||||||||||||
| 96 | 4 | $report .= "------------------------------------------------------\n"; | |||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | 4 | return $report; | |||||||||||||||||||||||||||
| 99 | } | ||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | # show cache statistics, if any | ||||||||||||||||||||||||||||
| 102 | 22 | ▶ | END { | ||||||||||||||||||||||||||
|
Subroutine
END: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 103 | 22 | ▶ | print _get_cache_stats_report if ($CACHE_STATS_ON); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 104 | } | ||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||
| 106 | #--------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 107 | # | ||||||||||||||||||||||||||||
| 108 | # | ||||||||||||||||||||||||||||
| 109 | # memoization - implement the cache behavior of a contract | ||||||||||||||||||||||||||||
| 110 | # | ||||||||||||||||||||||||||||
| 111 | # | ||||||||||||||||||||||||||||
| 112 | #--------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | 8 | ▶ | sub cache { | ||||||||||||||||||||||||||
|
Subroutine
cache: called CC=7 SCAR=20.2
Pod
documented
| |||||||||||||||||||||||||||||
| 115 | 8 | my ($self,%args) = @_; | |||||||||||||||||||||||||||
| 116 | 8 | ▶ | my $size = delete $args{size} || 10000; # default size 10000 elements | ||||||||||||||||||||||||||
|
Condition100%2/2
delete $args{'size'} || 10000
MC/DC100%1/1
delete $args{'size'} || 10000
delete $args{'size'}
Truth table100%2/2
A
A= delete $args{'size'}
| |||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||
| 118 | 8 | ▶ | croak "cache() got unknown arguments: ".Dumper(%args) if (%args); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 119 | 8 | ▶ | croak "size should be a number" if (!defined $size || $size !~ /^\d+$/); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $size and $size =~ /^\d+$/
MC/DC0%0/2
defined $size and $size =~ /^\d+$/
defined $size
$size =~ /^\d+$/
Truth table33%1/3
A and B
A= defined $sizeB= $size =~ /^\d+$/
| |||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||
| 121 | # NOTE: $contract->reset() deletes this cache | ||||||||||||||||||||||||||||
| 122 | 8 | $self->{cache} = new Sub::Contract::Cache( namespace => $self->contractor, | |||||||||||||||||||||||||||
| 123 | size => $size ); | ||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||
| 125 | 8 | ▶ | if ($CACHE_STATS_ON && !exists $CACHE_STATS{$self->contractor}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$CACHE_STATS_ON and not exists $CACHE_STATS{$self->contractor}
MC/DC50%1/2
$CACHE_STATS_ON and not exists $CACHE_STATS{$self->contractor}
$CACHE_STATS_ON
not exists $CACHE_STATS{$self->contractor}
Truth table66%2/3
A and not(B)
A= $CACHE_STATS_ONB= not exists $CACHE_STATS{$self->contractor}
| |||||||||||||||||||||||||||||
| 126 | 7 | $CACHE_STATS{$self->contractor} = { calls => 0, hits => 0, maxsize => -1, cache => $self->{cache} }; | |||||||||||||||||||||||||||
| 127 | } | ||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||
| 129 | 8 | return $self; | |||||||||||||||||||||||||||
| 130 | } | ||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||
| 132 | 0 | ▶ | sub get_cache { | ||||||||||||||||||||||||||
|
Subroutine
get_cache: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 133 | 0 | return $_->{cache}; | |||||||||||||||||||||||||||
| 134 | } | ||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||
| 136 | 0 | ▶ | sub has_cache { | ||||||||||||||||||||||||||
|
Subroutine
has_cache: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 137 | 0 | ▶ | return (exists $_->{cache}) ? 1:0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 138 | } | ||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||
| 140 | 0 | ▶ | sub clear_cache { | ||||||||||||||||||||||||||
|
Subroutine
clear_cache: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 141 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 142 | 0 | ▶ | confess "contract defines no cache" if (!exists $self->{cache}); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 143 | 0 | $self->{cache}->clear; | |||||||||||||||||||||||||||
| 144 | 0 | return $self; | |||||||||||||||||||||||||||
| 145 | } | ||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||
| 147 | 0 | ▶ | sub add_to_cache { | ||||||||||||||||||||||||||
|
Subroutine
add_to_cache: not called CC=8 SCAR=42.8
Pod
documented
| |||||||||||||||||||||||||||||
| 148 | 0 | my ($self,$args,$results) = @_; | |||||||||||||||||||||||||||
| 149 | 0 | ▶ | confess "add_to_cache expects an array ref of arguments" if (!defined $args || ref $args ne "ARRAY"); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
not defined $args or ref $args ne "ARRAY"
MC/DC0%0/2
not defined $args or ref $args ne "ARRAY"
not defined $args
ref $args ne "ARRAY"
Truth table0%0/3
not(A) or B
A= not defined $argsB= ref $args ne "ARRAY"
| |||||||||||||||||||||||||||||
| 150 | 0 | ▶ | confess "add_to_cache expects an array ref of results" if (!defined $results || ref $results ne "ARRAY"); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
not defined $results or ref $results ne "ARRAY"
MC/DC0%0/2
not defined $results or ref $results ne "ARRAY"
not defined $results
ref $results ne "ARRAY"
Truth table0%0/3
not(A) or B
A= not defined $resultsB= ref $results ne "ARRAY"
| |||||||||||||||||||||||||||||
| 151 | 0 | ▶ | confess "contract defines no cache" if (!exists $self->{cache}); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||
| 153 | # we assume that the cached function is context insensitive and hence should | ||||||||||||||||||||||||||||
| 154 | # return the same result independently of context. | ||||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||
| 156 | # the code to generate the key has to be the same as in Compiler.pm | ||||||||||||||||||||||||||||
| 157 | 0 | ▶ | my $key_array = join( ":", map( { (defined $_) ? $_ : "undef"; } "array", @$args) ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 158 | 0 | ▶ | my $key_scalar = join( ":", map( { (defined $_) ? $_ : "undef"; } "scalar", @$args) ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||
| 160 | 0 | $self->{cache}->set($key_array,$results); | |||||||||||||||||||||||||||
| 161 | 0 | $self->{cache}->set($key_scalar,$results); | |||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||
| 163 | 0 | return $self; | |||||||||||||||||||||||||||
| 164 | } | ||||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||
| 166 | 23 | 1; | |||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||
| 168 | =pod | ||||||||||||||||||||||||||||
| 169 | |||||||||||||||||||||||||||||
| 170 | =head1 NAME | ||||||||||||||||||||||||||||
| 171 | |||||||||||||||||||||||||||||
| 172 | Sub::Contract::Memoizer - Implement the caching behaviour of a contract | ||||||||||||||||||||||||||||
| 173 | |||||||||||||||||||||||||||||
| 174 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 175 | |||||||||||||||||||||||||||||
| 176 | See 'Sub::Contract'. | ||||||||||||||||||||||||||||
| 177 | |||||||||||||||||||||||||||||
| 178 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 179 | |||||||||||||||||||||||||||||
| 180 | Subroutine contracts defined with Sub::Contract can memoize | ||||||||||||||||||||||||||||
| 181 | the contractor's results. This optional behaviour is implemented | ||||||||||||||||||||||||||||
| 182 | in Sub::Contract::Memoizer. | ||||||||||||||||||||||||||||
| 183 | |||||||||||||||||||||||||||||
| 184 | =head1 API | ||||||||||||||||||||||||||||
| 185 | |||||||||||||||||||||||||||||
| 186 | See 'Sub::Contract'. | ||||||||||||||||||||||||||||
| 187 | |||||||||||||||||||||||||||||
| 188 | =over 4 | ||||||||||||||||||||||||||||
| 189 | |||||||||||||||||||||||||||||
| 190 | =item C<< $contract->cache([size => $max_size]) >> | ||||||||||||||||||||||||||||
| 191 | |||||||||||||||||||||||||||||
| 192 | =item C<< $contract->has_cache([size => $max_size]) >> | ||||||||||||||||||||||||||||
| 193 | |||||||||||||||||||||||||||||
| 194 | =item C<< $contract->get_cache >> | ||||||||||||||||||||||||||||
| 195 | |||||||||||||||||||||||||||||
| 196 | =item C<< $contract->clear_cache >> | ||||||||||||||||||||||||||||
| 197 | |||||||||||||||||||||||||||||
| 198 | =item C<< $contract->add_to_cache(\@args, \@results) >> | ||||||||||||||||||||||||||||
| 199 | |||||||||||||||||||||||||||||
| 200 | =back | ||||||||||||||||||||||||||||
| 201 | |||||||||||||||||||||||||||||
| 202 | =head1 SEE ALSO | ||||||||||||||||||||||||||||
| 203 | |||||||||||||||||||||||||||||
| 204 | See 'Sub::Contract'. | ||||||||||||||||||||||||||||
| 205 | |||||||||||||||||||||||||||||
| 206 | =head1 VERSION | ||||||||||||||||||||||||||||
| 207 | |||||||||||||||||||||||||||||
| 208 | $Id: Memoizer.pm,v 1.12 2009/06/16 12:23:58 erwan_lemonnier Exp $ | ||||||||||||||||||||||||||||
| 209 | |||||||||||||||||||||||||||||
| 210 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 211 | |||||||||||||||||||||||||||||
| 212 | Erwan Lemonnier C<< <erwan@cpan.org> >> | ||||||||||||||||||||||||||||
| 213 | |||||||||||||||||||||||||||||
| 214 | =head1 LICENSE | ||||||||||||||||||||||||||||
| 215 | |||||||||||||||||||||||||||||
| 216 | See Sub::Contract. | ||||||||||||||||||||||||||||
| 217 | |||||||||||||||||||||||||||||
| 218 | =cut | ||||||||||||||||||||||||||||
| 219 | |||||||||||||||||||||||||||||
| 220 | |||||||||||||||||||||||||||||
| 221 | |||||||||||||||||||||||||||||