xref: /openbsd-src/gnu/usr.bin/perl/cpan/Memoize/Memoize.pm (revision e068048151d29f2562a32185e21a8ba885482260)
1b39c5158Smillert# -*- mode: perl; perl-indent-level: 2; -*-
2*e0680481Safresh1# vim: ts=8 sw=2 sts=2 noexpandtab
3*e0680481Safresh1
4b39c5158Smillert# Memoize.pm
5b39c5158Smillert#
6e9ce3842Safresh1# Copyright 1998, 1999, 2000, 2001, 2012 M. J. Dominus.
7b39c5158Smillert# You may copy and distribute this program under the
8*e0680481Safresh1# same terms as Perl itself.
9*e0680481Safresh1
10*e0680481Safresh1use strict; use warnings;
11b39c5158Smillert
12b39c5158Smillertpackage Memoize;
13*e0680481Safresh1our $VERSION = '1.16';
14b39c5158Smillert
15b39c5158Smillertuse Carp;
16*e0680481Safresh1use Scalar::Util 1.11 (); # for set_prototype
17*e0680481Safresh1
18*e0680481Safresh1BEGIN { require Exporter; *import = \&Exporter::import }
19*e0680481Safresh1our @EXPORT = qw(memoize);
20*e0680481Safresh1our @EXPORT_OK = qw(unmemoize flush_cache);
21b39c5158Smillert
22b39c5158Smillertmy %memotable;
23b39c5158Smillert
24*e0680481Safresh1sub CLONE {
25*e0680481Safresh1  my @info = values %memotable;
26*e0680481Safresh1  %memotable = map +($_->{WRAPPER} => $_), @info;
27*e0680481Safresh1}
28b39c5158Smillert
29b39c5158Smillertsub memoize {
30b39c5158Smillert  my $fn = shift;
31b39c5158Smillert  my %options = @_;
32b39c5158Smillert
33b39c5158Smillert  unless (defined($fn) &&
34b39c5158Smillert	  (ref $fn eq 'CODE' || ref $fn eq '')) {
35b39c5158Smillert    croak "Usage: memoize 'functionname'|coderef {OPTIONS}";
36b39c5158Smillert  }
37b39c5158Smillert
38b39c5158Smillert  my $uppack = caller;		# TCL me Elmo!
39b39c5158Smillert  my $name = (ref $fn ? undef : $fn);
40*e0680481Safresh1  my $cref = _make_cref($fn, $uppack);
41b39c5158Smillert
42b39c5158Smillert  my $normalizer = $options{NORMALIZER};
43b39c5158Smillert  if (defined $normalizer  && ! ref $normalizer) {
44b39c5158Smillert    $normalizer = _make_cref($normalizer, $uppack);
45b39c5158Smillert  }
46b39c5158Smillert
47*e0680481Safresh1  my $install_name = exists $options{INSTALL}
48*e0680481Safresh1    ? $options{INSTALL} # use given name (or, if undef: do not install)
49*e0680481Safresh1    : $name; # no INSTALL option provided: default to original name if possible
50b39c5158Smillert
51b39c5158Smillert  if (defined $install_name) {
52b39c5158Smillert    $install_name = $uppack . '::' . $install_name
53b39c5158Smillert	unless $install_name =~ /::/;
54b39c5158Smillert  }
55b39c5158Smillert
56*e0680481Safresh1  # convert LIST_CACHE => MERGE to SCALAR_CACHE => MERGE
57*e0680481Safresh1  # to ensure TIE/HASH will always be checked by _check_suitable
58*e0680481Safresh1  if (($options{LIST_CACHE} || '') eq 'MERGE') {
59*e0680481Safresh1    $options{LIST_CACHE} = $options{SCALAR_CACHE};
60*e0680481Safresh1    $options{SCALAR_CACHE} = 'MERGE';
61*e0680481Safresh1  }
62b39c5158Smillert
63b39c5158Smillert  # These will be the caches
64b39c5158Smillert  my %caches;
65*e0680481Safresh1  for my $context (qw(LIST SCALAR)) { # SCALAR_CACHE must be last, to process MERGE
66*e0680481Safresh1    my $fullopt = $options{"${context}_CACHE"} ||= 'MEMORY';
67*e0680481Safresh1    my ($cache_opt, @cache_opt_args) = ref $fullopt ? @$fullopt : $fullopt;
68b39c5158Smillert    if ($cache_opt eq 'FAULT') { # no cache
69b39c5158Smillert      $caches{$context} = undef;
70b39c5158Smillert    } elsif ($cache_opt eq 'HASH') { # user-supplied hash
71b39c5158Smillert      my $cache = $cache_opt_args[0];
72*e0680481Safresh1      _check_suitable($context, ref tied %$cache);
73b39c5158Smillert      $caches{$context} = $cache;
74*e0680481Safresh1    } elsif ($cache_opt eq 'TIE') {
75*e0680481Safresh1      carp("TIE option to memoize() is deprecated; use HASH instead")
76*e0680481Safresh1        if warnings::enabled('all');
77*e0680481Safresh1      my $module = shift(@cache_opt_args) || '';
78*e0680481Safresh1      _check_suitable($context, $module);
79*e0680481Safresh1      my $hash = $caches{$context} = {};
80*e0680481Safresh1      (my $modulefile = $module . '.pm') =~ s{::}{/}g;
81*e0680481Safresh1      require $modulefile;
82*e0680481Safresh1      tie(%$hash, $module, @cache_opt_args)
83*e0680481Safresh1        or croak "Couldn't tie memoize hash to `$module': $!";
84*e0680481Safresh1    } elsif ($cache_opt eq 'MEMORY') {
85b39c5158Smillert      $caches{$context} = {};
86*e0680481Safresh1    } elsif ($cache_opt eq 'MERGE' and not ref $fullopt) { # ['MERGE'] was never supported
87*e0680481Safresh1      die "cannot MERGE $context\_CACHE" if $context ne 'SCALAR'; # should never happen
88*e0680481Safresh1      die 'bad cache setup order' if not exists $caches{LIST}; # should never happen
89e9ce3842Safresh1      $options{MERGED} = 1;
90b39c5158Smillert      $caches{SCALAR} = $caches{LIST};
91*e0680481Safresh1    } else {
92*e0680481Safresh1      croak "Unrecognized option to `${context}_CACHE': `$cache_opt' should be one of (MERGE TIE MEMORY FAULT HASH)";
93b39c5158Smillert    }
94b39c5158Smillert  }
95b39c5158Smillert
96*e0680481Safresh1  my $wrapper = _wrap($install_name, $cref, $normalizer, $options{MERGED}, \%caches);
97*e0680481Safresh1
98*e0680481Safresh1  if (defined $install_name) {
99*e0680481Safresh1    no strict;
100*e0680481Safresh1    no warnings 'redefine';
101*e0680481Safresh1    *{$install_name} = $wrapper;
102*e0680481Safresh1  }
103*e0680481Safresh1
104*e0680481Safresh1  $memotable{$wrapper} = {
105b39c5158Smillert    L => $caches{LIST},
106*e0680481Safresh1    S => $caches{SCALAR},
107*e0680481Safresh1    U => $cref,
108*e0680481Safresh1    NAME => $install_name,
109*e0680481Safresh1    WRAPPER => $wrapper,
110b39c5158Smillert  };
111b39c5158Smillert
112b39c5158Smillert  $wrapper			# Return just memoized version
113b39c5158Smillert}
114b39c5158Smillert
115b39c5158Smillertsub flush_cache {
116b39c5158Smillert  my $func = _make_cref($_[0], scalar caller);
117*e0680481Safresh1  my $info = $memotable{$func};
118b39c5158Smillert  die "$func not memoized" unless defined $info;
119b39c5158Smillert  for my $context (qw(S L)) {
120b39c5158Smillert    my $cache = $info->{$context};
121b39c5158Smillert    if (tied %$cache && ! (tied %$cache)->can('CLEAR')) {
122b39c5158Smillert      my $funcname = defined($info->{NAME}) ?
123b39c5158Smillert          "function $info->{NAME}" : "anonymous function $func";
124b39c5158Smillert      my $context = {S => 'scalar', L => 'list'}->{$context};
125b39c5158Smillert      croak "Tied cache hash for $context-context $funcname does not support flushing";
126b39c5158Smillert    } else {
127b39c5158Smillert      %$cache = ();
128b39c5158Smillert    }
129b39c5158Smillert  }
130b39c5158Smillert}
131b39c5158Smillert
132*e0680481Safresh1sub _wrap {
133*e0680481Safresh1  my ($name, $orig, $normalizer, $merged, $caches) = @_;
134*e0680481Safresh1  my ($cache_L, $cache_S) = @$caches{qw(LIST SCALAR)};
135*e0680481Safresh1  undef $caches; # keep the pad from keeping the hash alive forever
136*e0680481Safresh1  Scalar::Util::set_prototype(sub {
137*e0680481Safresh1    my $argstr = do {
138*e0680481Safresh1      no warnings 'uninitialized';
139*e0680481Safresh1      defined $normalizer
140*e0680481Safresh1        ? ( wantarray ? ( $normalizer->( @_ ) )[0] : $normalizer->( @_ ) )
141*e0680481Safresh1          . '' # coerce undef to string while the warning is off
142*e0680481Safresh1        : join chr(28), @_;
143*e0680481Safresh1    };
144b39c5158Smillert
145*e0680481Safresh1    if (wantarray) {
146*e0680481Safresh1      _crap_out($name, 'list') unless $cache_L;
147*e0680481Safresh1      exists $cache_L->{$argstr} ? (
148*e0680481Safresh1        @{$cache_L->{$argstr}}
149*e0680481Safresh1      ) : do {
150*e0680481Safresh1        my @q = do { no warnings 'recursion'; &$orig };
151*e0680481Safresh1        $cache_L->{$argstr} = \@q;
152b39c5158Smillert        @q;
153*e0680481Safresh1      };
154b39c5158Smillert    } else {
155*e0680481Safresh1      _crap_out($name, 'scalar') unless $cache_S;
156*e0680481Safresh1      exists $cache_S->{$argstr} ? (
157*e0680481Safresh1        $merged ? $cache_S->{$argstr}[0] : $cache_S->{$argstr}
158*e0680481Safresh1      ) : do {
159*e0680481Safresh1        my $val = do { no warnings 'recursion'; &$orig };
160*e0680481Safresh1        $cache_S->{$argstr} = $merged ? [$val] : $val;
161*e0680481Safresh1        $val;
162*e0680481Safresh1      };
163b39c5158Smillert    }
164*e0680481Safresh1  }, prototype $orig);
165b39c5158Smillert}
166b39c5158Smillert
167b39c5158Smillertsub unmemoize {
168b39c5158Smillert  my $f = shift;
169b39c5158Smillert  my $uppack = caller;
170b39c5158Smillert  my $cref = _make_cref($f, $uppack);
171b39c5158Smillert
172*e0680481Safresh1  unless (exists $memotable{$cref}) {
173b39c5158Smillert    croak "Could not unmemoize function `$f', because it was not memoized to begin with";
174b39c5158Smillert  }
175b39c5158Smillert
176*e0680481Safresh1  my $tabent = $memotable{$cref};
177b39c5158Smillert  unless (defined $tabent) {
178b39c5158Smillert    croak "Could not figure out how to unmemoize function `$f'";
179b39c5158Smillert  }
180b39c5158Smillert  my $name = $tabent->{NAME};
181b39c5158Smillert  if (defined $name) {
182b39c5158Smillert    no strict;
183*e0680481Safresh1    no warnings 'redefine';
184b39c5158Smillert    *{$name} = $tabent->{U}; # Replace with original function
185b39c5158Smillert  }
186*e0680481Safresh1  delete $memotable{$cref};
187b39c5158Smillert
188b39c5158Smillert  $tabent->{U};
189b39c5158Smillert}
190b39c5158Smillert
191b39c5158Smillertsub _make_cref {
192b39c5158Smillert  my $fn = shift;
193b39c5158Smillert  my $uppack = shift;
194b39c5158Smillert  my $cref;
195b39c5158Smillert  my $name;
196b39c5158Smillert
197b39c5158Smillert  if (ref $fn eq 'CODE') {
198b39c5158Smillert    $cref = $fn;
199b39c5158Smillert  } elsif (! ref $fn) {
200b39c5158Smillert    if ($fn =~ /::/) {
201b39c5158Smillert      $name = $fn;
202b39c5158Smillert    } else {
203b39c5158Smillert      $name = $uppack . '::' . $fn;
204b39c5158Smillert    }
205b39c5158Smillert    no strict;
206b39c5158Smillert    if (defined $name and !defined(&$name)) {
207b39c5158Smillert      croak "Cannot operate on nonexistent function `$fn'";
208b39c5158Smillert    }
209b39c5158Smillert#    $cref = \&$name;
210b39c5158Smillert    $cref = *{$name}{CODE};
211b39c5158Smillert  } else {
212b39c5158Smillert    my $parent = (caller(1))[3]; # Function that called _make_cref
213b39c5158Smillert    croak "Usage: argument 1 to `$parent' must be a function name or reference.\n";
214b39c5158Smillert  }
215*e0680481Safresh1  our $DEBUG and warn "${name}($fn) => $cref in _make_cref\n";
216b39c5158Smillert  $cref;
217b39c5158Smillert}
218b39c5158Smillert
219b39c5158Smillertsub _crap_out {
220b39c5158Smillert  my ($funcname, $context) = @_;
221b39c5158Smillert  if (defined $funcname) {
222b39c5158Smillert    croak "Function `$funcname' called in forbidden $context context; faulting";
223b39c5158Smillert  } else {
224b39c5158Smillert    croak "Anonymous function called in forbidden $context context; faulting";
225b39c5158Smillert  }
226b39c5158Smillert}
227b39c5158Smillert
228*e0680481Safresh1# Raise an error if the user tries to specify one of these packages as a
229*e0680481Safresh1# tie for LIST_CACHE
230*e0680481Safresh1my %scalar_only = map {($_ => 1)} qw(DB_File GDBM_File SDBM_File ODBM_File), map +($_, "Memoize::$_"), qw(AnyDBM_File NDBM_File);
231*e0680481Safresh1sub _check_suitable {
232*e0680481Safresh1  my ($context, $package) = @_;
233*e0680481Safresh1  croak "You can't use $package for LIST_CACHE because it can only store scalars"
234*e0680481Safresh1    if $context eq 'LIST' and $scalar_only{$package};
235*e0680481Safresh1}
236*e0680481Safresh1
237b39c5158Smillert1;
238b39c5158Smillert
239*e0680481Safresh1__END__
240b39c5158Smillert
241*e0680481Safresh1=pod
242b39c5158Smillert
243b39c5158Smillert=head1 NAME
244b39c5158Smillert
245b39c5158SmillertMemoize - Make functions faster by trading space for time
246b39c5158Smillert
247b39c5158Smillert=head1 SYNOPSIS
248b39c5158Smillert
249b39c5158Smillert	use Memoize;
250b39c5158Smillert	memoize('slow_function');
251b39c5158Smillert	slow_function(arguments);    # Is faster than it was before
252b39c5158Smillert
253b39c5158Smillert
254b39c5158SmillertThis is normally all you need to know.  However, many options are available:
255b39c5158Smillert
256b39c5158Smillert	memoize(function, options...);
257b39c5158Smillert
258b39c5158SmillertOptions include:
259b39c5158Smillert
260b39c5158Smillert	NORMALIZER => function
261b39c5158Smillert	INSTALL => new_name
262b39c5158Smillert
263b39c5158Smillert	SCALAR_CACHE => 'MEMORY'
264b39c5158Smillert        SCALAR_CACHE => ['HASH', \%cache_hash ]
265b39c5158Smillert	SCALAR_CACHE => 'FAULT'
266b39c5158Smillert	SCALAR_CACHE => 'MERGE'
267b39c5158Smillert
268b39c5158Smillert	LIST_CACHE => 'MEMORY'
269b39c5158Smillert        LIST_CACHE => ['HASH', \%cache_hash ]
270b39c5158Smillert	LIST_CACHE => 'FAULT'
271b39c5158Smillert	LIST_CACHE => 'MERGE'
272b39c5158Smillert
273b39c5158Smillert=head1 DESCRIPTION
274b39c5158Smillert
275*e0680481Safresh1I<Memoizing> a function makes it faster by trading space for time. It
276b39c5158Smillertdoes this by caching the return values of the function in a table.
277b39c5158SmillertIf you call the function again with the same arguments, C<memoize>
278b39c5158Smillertjumps in and gives you the value out of the table, instead of letting
279b39c5158Smillertthe function compute the value all over again.
280b39c5158Smillert
281*e0680481Safresh1=head1 EXAMPLE
282*e0680481Safresh1
283b39c5158SmillertHere is an extreme example.  Consider the Fibonacci sequence, defined
284b39c5158Smillertby the following function:
285b39c5158Smillert
286b39c5158Smillert	# Compute Fibonacci numbers
287b39c5158Smillert	sub fib {
288b39c5158Smillert	  my $n = shift;
289b39c5158Smillert	  return $n if $n < 2;
290b39c5158Smillert	  fib($n-1) + fib($n-2);
291b39c5158Smillert	}
292b39c5158Smillert
293b39c5158SmillertThis function is very slow.  Why?  To compute fib(14), it first wants
294b39c5158Smillertto compute fib(13) and fib(12), and add the results.  But to compute
295b39c5158Smillertfib(13), it first has to compute fib(12) and fib(11), and then it
296b39c5158Smillertcomes back and computes fib(12) all over again even though the answer
297b39c5158Smillertis the same.  And both of the times that it wants to compute fib(12),
298b39c5158Smillertit has to compute fib(11) from scratch, and then it has to do it
299b39c5158Smillertagain each time it wants to compute fib(13).  This function does so
300b39c5158Smillertmuch recomputing of old results that it takes a really long time to
301b39c5158Smillertrun---fib(14) makes 1,200 extra recursive calls to itself, to compute
302b39c5158Smillertand recompute things that it already computed.
303b39c5158Smillert
304b39c5158SmillertThis function is a good candidate for memoization.  If you memoize the
305*e0680481Safresh1C<fib> function above, it will compute fib(14) exactly once, the first
306b39c5158Smillerttime it needs to, and then save the result in a table.  Then if you
307b39c5158Smillertask for fib(14) again, it gives you the result out of the table.
308b39c5158SmillertWhile computing fib(14), instead of computing fib(12) twice, it does
309b39c5158Smillertit once; the second time it needs the value it gets it from the table.
310b39c5158SmillertIt doesn't compute fib(11) four times; it computes it once, getting it
311b39c5158Smillertfrom the table the next three times.  Instead of making 1,200
312*e0680481Safresh1recursive calls to C<fib>, it makes 15. This makes the function about
313b39c5158Smillert150 times faster.
314b39c5158Smillert
315b39c5158SmillertYou could do the memoization yourself, by rewriting the function, like
316b39c5158Smillertthis:
317b39c5158Smillert
318b39c5158Smillert	# Compute Fibonacci numbers, memoized version
319b39c5158Smillert	{ my @fib;
320b39c5158Smillert  	  sub fib {
321b39c5158Smillert	    my $n = shift;
322b39c5158Smillert	    return $fib[$n] if defined $fib[$n];
323b39c5158Smillert	    return $fib[$n] = $n if $n < 2;
324b39c5158Smillert	    $fib[$n] = fib($n-1) + fib($n-2);
325b39c5158Smillert	  }
326b39c5158Smillert        }
327b39c5158Smillert
328b39c5158SmillertOr you could use this module, like this:
329b39c5158Smillert
330b39c5158Smillert	use Memoize;
331b39c5158Smillert	memoize('fib');
332b39c5158Smillert
333b39c5158Smillert	# Rest of the fib function just like the original version.
334b39c5158Smillert
335b39c5158SmillertThis makes it easy to turn memoizing on and off.
336b39c5158Smillert
337b39c5158SmillertHere's an even simpler example: I wrote a simple ray tracer; the
338b39c5158Smillertprogram would look in a certain direction, figure out what it was
339*e0680481Safresh1looking at, and then convert the C<color> value (typically a string
340*e0680481Safresh1like C<red>) of that object to a red, green, and blue pixel value, like
341b39c5158Smillertthis:
342b39c5158Smillert
343b39c5158Smillert    for ($direction = 0; $direction < 300; $direction++) {
344b39c5158Smillert      # Figure out which object is in direction $direction
345b39c5158Smillert      $color = $object->{color};
346b39c5158Smillert      ($r, $g, $b) = @{&ColorToRGB($color)};
347b39c5158Smillert      ...
348b39c5158Smillert    }
349b39c5158Smillert
350b39c5158SmillertSince there are relatively few objects in a picture, there are only a
351b39c5158Smillertfew colors, which get looked up over and over again.  Memoizing
352b39c5158SmillertC<ColorToRGB> sped up the program by several percent.
353b39c5158Smillert
354b39c5158Smillert=head1 DETAILS
355b39c5158Smillert
356b39c5158SmillertThis module exports exactly one function, C<memoize>.  The rest of the
357b39c5158Smillertfunctions in this package are None of Your Business.
358b39c5158Smillert
359b39c5158SmillertYou should say
360b39c5158Smillert
361b39c5158Smillert	memoize(function)
362b39c5158Smillert
363b39c5158Smillertwhere C<function> is the name of the function you want to memoize, or
364b39c5158Smillerta reference to it.  C<memoize> returns a reference to the new,
365b39c5158Smillertmemoized version of the function, or C<undef> on a non-fatal error.
366b39c5158SmillertAt present, there are no non-fatal errors, but there might be some in
367b39c5158Smillertthe future.
368b39c5158Smillert
369b39c5158SmillertIf C<function> was the name of a function, then C<memoize> hides the
370b39c5158Smillertold version and installs the new memoized version under the old name,
371b39c5158Smillertso that C<&function(...)> actually invokes the memoized version.
372b39c5158Smillert
373b39c5158Smillert=head1 OPTIONS
374b39c5158Smillert
375b39c5158SmillertThere are some optional options you can pass to C<memoize> to change
376b39c5158Smillertthe way it behaves a little.  To supply options, invoke C<memoize>
377b39c5158Smillertlike this:
378b39c5158Smillert
379b39c5158Smillert	memoize(function, NORMALIZER => function,
380b39c5158Smillert			  INSTALL => newname,
381b39c5158Smillert                          SCALAR_CACHE => option,
382b39c5158Smillert	                  LIST_CACHE => option
383b39c5158Smillert			 );
384b39c5158Smillert
385b39c5158SmillertEach of these options is optional; you can include some, all, or none
386b39c5158Smillertof them.
387b39c5158Smillert
388b39c5158Smillert=head2 INSTALL
389b39c5158Smillert
390b39c5158SmillertIf you supply a function name with C<INSTALL>, memoize will install
391b39c5158Smillertthe new, memoized version of the function under the name you give.
392b39c5158SmillertFor example,
393b39c5158Smillert
394b39c5158Smillert	memoize('fib', INSTALL => 'fastfib')
395b39c5158Smillert
396b39c5158Smillertinstalls the memoized version of C<fib> as C<fastfib>; without the
397b39c5158SmillertC<INSTALL> option it would have replaced the old C<fib> with the
398b39c5158Smillertmemoized version.
399b39c5158Smillert
400b39c5158SmillertTo prevent C<memoize> from installing the memoized version anywhere, use
401b39c5158SmillertC<INSTALL =E<gt> undef>.
402b39c5158Smillert
403b39c5158Smillert=head2 NORMALIZER
404b39c5158Smillert
405b39c5158SmillertSuppose your function looks like this:
406b39c5158Smillert
407b39c5158Smillert	# Typical call: f('aha!', A => 11, B => 12);
408b39c5158Smillert	sub f {
409b39c5158Smillert	  my $a = shift;
410b39c5158Smillert	  my %hash = @_;
411b39c5158Smillert	  $hash{B} ||= 2;  # B defaults to 2
412b39c5158Smillert	  $hash{C} ||= 7;  # C defaults to 7
413b39c5158Smillert
414b39c5158Smillert	  # Do something with $a, %hash
415b39c5158Smillert	}
416b39c5158Smillert
417b39c5158SmillertNow, the following calls to your function are all completely equivalent:
418b39c5158Smillert
419b39c5158Smillert	f(OUCH);
420b39c5158Smillert	f(OUCH, B => 2);
421b39c5158Smillert	f(OUCH, C => 7);
422b39c5158Smillert	f(OUCH, B => 2, C => 7);
423b39c5158Smillert	f(OUCH, C => 7, B => 2);
424b39c5158Smillert	(etc.)
425b39c5158Smillert
426b39c5158SmillertHowever, unless you tell C<Memoize> that these calls are equivalent,
427b39c5158Smillertit will not know that, and it will compute the values for these
428b39c5158Smillertinvocations of your function separately, and store them separately.
429b39c5158Smillert
430b39c5158SmillertTo prevent this, supply a C<NORMALIZER> function that turns the
431b39c5158Smillertprogram arguments into a string in a way that equivalent arguments
432b39c5158Smillertturn into the same string.  A C<NORMALIZER> function for C<f> above
433b39c5158Smillertmight look like this:
434b39c5158Smillert
435b39c5158Smillert	sub normalize_f {
436b39c5158Smillert	  my $a = shift;
437b39c5158Smillert	  my %hash = @_;
438b39c5158Smillert	  $hash{B} ||= 2;
439b39c5158Smillert	  $hash{C} ||= 7;
440b39c5158Smillert
441b39c5158Smillert	  join(',', $a, map ($_ => $hash{$_}) sort keys %hash);
442b39c5158Smillert	}
443b39c5158Smillert
444b39c5158SmillertEach of the argument lists above comes out of the C<normalize_f>
445b39c5158Smillertfunction looking exactly the same, like this:
446b39c5158Smillert
447b39c5158Smillert	OUCH,B,2,C,7
448b39c5158Smillert
449b39c5158SmillertYou would tell C<Memoize> to use this normalizer this way:
450b39c5158Smillert
451b39c5158Smillert	memoize('f', NORMALIZER => 'normalize_f');
452b39c5158Smillert
453b39c5158SmillertC<memoize> knows that if the normalized version of the arguments is
454b39c5158Smillertthe same for two argument lists, then it can safely look up the value
455b39c5158Smillertthat it computed for one argument list and return it as the result of
456b39c5158Smillertcalling the function with the other argument list, even if the
457b39c5158Smillertargument lists look different.
458b39c5158Smillert
459b39c5158SmillertThe default normalizer just concatenates the arguments with character
460b39c5158Smillert28 in between.  (In ASCII, this is called FS or control-\.)  This
461b39c5158Smillertalways works correctly for functions with only one string argument,
462b39c5158Smillertand also when the arguments never contain character 28.  However, it
463b39c5158Smillertcan confuse certain argument lists:
464b39c5158Smillert
465b39c5158Smillert	normalizer("a\034", "b")
466b39c5158Smillert	normalizer("a", "\034b")
467b39c5158Smillert	normalizer("a\034\034b")
468b39c5158Smillert
469b39c5158Smillertfor example.
470b39c5158Smillert
471b39c5158SmillertSince hash keys are strings, the default normalizer will not
472b39c5158Smillertdistinguish between C<undef> and the empty string.  It also won't work
473b39c5158Smillertwhen the function's arguments are references.  For example, consider a
474b39c5158Smillertfunction C<g> which gets two arguments: A number, and a reference to
475b39c5158Smillertan array of numbers:
476b39c5158Smillert
477b39c5158Smillert	g(13, [1,2,3,4,5,6,7]);
478b39c5158Smillert
479b39c5158SmillertThe default normalizer will turn this into something like
480b39c5158SmillertC<"13\034ARRAY(0x436c1f)">.  That would be all right, except that a
481b39c5158Smillertsubsequent array of numbers might be stored at a different location
482b39c5158Smillerteven though it contains the same data.  If this happens, C<Memoize>
483b39c5158Smillertwill think that the arguments are different, even though they are
484b39c5158Smillertequivalent.  In this case, a normalizer like this is appropriate:
485b39c5158Smillert
486b39c5158Smillert	sub normalize { join ' ', $_[0], @{$_[1]} }
487b39c5158Smillert
488b39c5158SmillertFor the example above, this produces the key "13 1 2 3 4 5 6 7".
489b39c5158Smillert
490b39c5158SmillertAnother use for normalizers is when the function depends on data other
491b39c5158Smillertthan those in its arguments.  Suppose you have a function which
492b39c5158Smillertreturns a value which depends on the current hour of the day:
493b39c5158Smillert
494b39c5158Smillert	sub on_duty {
495b39c5158Smillert          my ($problem_type) = @_;
496b39c5158Smillert	  my $hour = (localtime)[2];
497b39c5158Smillert          open my $fh, "$DIR/$problem_type" or die...;
498b39c5158Smillert          my $line;
499b39c5158Smillert          while ($hour-- > 0)
500b39c5158Smillert            $line = <$fh>;
501b39c5158Smillert          }
502b39c5158Smillert	  return $line;
503b39c5158Smillert	}
504b39c5158Smillert
505b39c5158SmillertAt 10:23, this function generates the 10th line of a data file; at
506b39c5158Smillert3:45 PM it generates the 15th line instead.  By default, C<Memoize>
507b39c5158Smillertwill only see the $problem_type argument.  To fix this, include the
508b39c5158Smillertcurrent hour in the normalizer:
509b39c5158Smillert
510b39c5158Smillert        sub normalize { join ' ', (localtime)[2], @_ }
511b39c5158Smillert
512b39c5158SmillertThe calling context of the function (scalar or list context) is
513b39c5158Smillertpropagated to the normalizer.  This means that if the memoized
514b39c5158Smillertfunction will treat its arguments differently in list context than it
515b39c5158Smillertwould in scalar context, you can have the normalizer function select
516b39c5158Smillertits behavior based on the results of C<wantarray>.  Even if called in
517b39c5158Smillerta list context, a normalizer should still return a single string.
518b39c5158Smillert
519b39c5158Smillert=head2 C<SCALAR_CACHE>, C<LIST_CACHE>
520b39c5158Smillert
521b39c5158SmillertNormally, C<Memoize> caches your function's return values into an
522b39c5158Smillertordinary Perl hash variable.  However, you might like to have the
523b39c5158Smillertvalues cached on the disk, so that they persist from one run of your
524b39c5158Smillertprogram to the next, or you might like to associate some other
525b39c5158Smillertinteresting semantics with the cached values.
526b39c5158Smillert
527b39c5158SmillertThere's a slight complication under the hood of C<Memoize>: There are
528b39c5158Smillertactually I<two> caches, one for scalar values and one for list values.
529b39c5158SmillertWhen your function is called in scalar context, its return value is
530b39c5158Smillertcached in one hash, and when your function is called in list context,
531b39c5158Smillertits value is cached in the other hash.  You can control the caching
532b39c5158Smillertbehavior of both contexts independently with these options.
533b39c5158Smillert
534b39c5158SmillertThe argument to C<LIST_CACHE> or C<SCALAR_CACHE> must either be one of
535b39c5158Smillertthe following four strings:
536b39c5158Smillert
537b39c5158Smillert	MEMORY
538b39c5158Smillert	FAULT
539b39c5158Smillert	MERGE
540b39c5158Smillert        HASH
541b39c5158Smillert
542e9ce3842Safresh1or else it must be a reference to an array whose first element is one of
543b39c5158Smillertthese four strings, such as C<[HASH, arguments...]>.
544b39c5158Smillert
545b39c5158Smillert=over 4
546b39c5158Smillert
547b39c5158Smillert=item C<MEMORY>
548b39c5158Smillert
549b39c5158SmillertC<MEMORY> means that return values from the function will be cached in
550b39c5158Smillertan ordinary Perl hash variable.  The hash variable will not persist
551b39c5158Smillertafter the program exits.  This is the default.
552b39c5158Smillert
553b39c5158Smillert=item C<HASH>
554b39c5158Smillert
555b39c5158SmillertC<HASH> allows you to specify that a particular hash that you supply
556b39c5158Smillertwill be used as the cache.  You can tie this hash beforehand to give
557b39c5158Smillertit any behavior you want.
558b39c5158Smillert
559b39c5158SmillertA tied hash can have any semantics at all.  It is typically tied to an
560b39c5158Smillerton-disk database, so that cached values are stored in the database and
561b39c5158Smillertretrieved from it again when needed, and the disk file typically
562b39c5158Smillertpersists after your program has exited.  See C<perltie> for more
563b39c5158Smillertcomplete details about C<tie>.
564b39c5158Smillert
565b39c5158SmillertA typical example is:
566b39c5158Smillert
567b39c5158Smillert        use DB_File;
568b39c5158Smillert        tie my %cache => 'DB_File', $filename, O_RDWR|O_CREAT, 0666;
569b39c5158Smillert        memoize 'function', SCALAR_CACHE => [HASH => \%cache];
570b39c5158Smillert
571b39c5158SmillertThis has the effect of storing the cache in a C<DB_File> database
572b39c5158Smillertwhose name is in C<$filename>.  The cache will persist after the
573b39c5158Smillertprogram has exited.  Next time the program runs, it will find the
574b39c5158Smillertcache already populated from the previous run of the program.  Or you
575b39c5158Smillertcan forcibly populate the cache by constructing a batch program that
576b39c5158Smillertruns in the background and populates the cache file.  Then when you
577b39c5158Smillertcome to run your real program the memoized function will be fast
578b39c5158Smillertbecause all its results have been precomputed.
579b39c5158Smillert
580e9ce3842Safresh1Another reason to use C<HASH> is to provide your own hash variable.
581e9ce3842Safresh1You can then inspect or modify the contents of the hash to gain finer
582e9ce3842Safresh1control over the cache management.
583e9ce3842Safresh1
584b39c5158Smillert=item C<TIE>
585b39c5158Smillert
586b39c5158SmillertThis option is no longer supported.  It is still documented only to
587b39c5158Smillertaid in the debugging of old programs that use it.  Old programs should
588b39c5158Smillertbe converted to use the C<HASH> option instead.
589b39c5158Smillert
590e9ce3842Safresh1        memoize ... ['TIE', PACKAGE, ARGS...]
591b39c5158Smillert
592b39c5158Smillertis merely a shortcut for
593b39c5158Smillert
594b39c5158Smillert        require PACKAGE;
595e9ce3842Safresh1	{ tie my %cache, PACKAGE, ARGS...;
596b39c5158Smillert          memoize ... [HASH => \%cache];
597e9ce3842Safresh1        }
598b39c5158Smillert
599b39c5158Smillert=item C<FAULT>
600b39c5158Smillert
601b39c5158SmillertC<FAULT> means that you never expect to call the function in scalar
602b39c5158Smillert(or list) context, and that if C<Memoize> detects such a call, it
603b39c5158Smillertshould abort the program.  The error message is one of
604b39c5158Smillert
605b39c5158Smillert	`foo' function called in forbidden list context at line ...
606b39c5158Smillert	`foo' function called in forbidden scalar context at line ...
607b39c5158Smillert
608b39c5158Smillert=item C<MERGE>
609b39c5158Smillert
610e9ce3842Safresh1C<MERGE> normally means that the memoized function does not
611*e0680481Safresh1distinguish between list and scalar context, and that return values in
612e9ce3842Safresh1both contexts should be stored together.  Both C<LIST_CACHE =E<gt>
613e9ce3842Safresh1MERGE> and C<SCALAR_CACHE =E<gt> MERGE> mean the same thing.
614b39c5158Smillert
615b39c5158SmillertConsider this function:
616b39c5158Smillert
617e9ce3842Safresh1	sub complicated {
618e9ce3842Safresh1          # ... time-consuming calculation of $result
619e9ce3842Safresh1          return $result;
620e9ce3842Safresh1        }
621b39c5158Smillert
622e9ce3842Safresh1The C<complicated> function will return the same numeric C<$result>
623e9ce3842Safresh1regardless of whether it is called in list or in scalar context.
624b39c5158Smillert
625e9ce3842Safresh1Normally, the following code will result in two calls to C<complicated>, even
626e9ce3842Safresh1if C<complicated> is memoized:
627b39c5158Smillert
628e9ce3842Safresh1    $x = complicated(142);
629e9ce3842Safresh1    ($y) = complicated(142);
630e9ce3842Safresh1    $z = complicated(142);
631b39c5158Smillert
632e9ce3842Safresh1The first call will cache the result, say 37, in the scalar cache; the
633*e0680481Safresh1second will cache the list C<(37)> in the list cache.  The third call
634e9ce3842Safresh1doesn't call the real C<complicated> function; it gets the value 37
635e9ce3842Safresh1from the scalar cache.
636e9ce3842Safresh1
637e9ce3842Safresh1Obviously, the second call to C<complicated> is a waste of time, and
638e9ce3842Safresh1storing its return value is a waste of space.  Specifying C<LIST_CACHE
639e9ce3842Safresh1=E<gt> MERGE> will make C<memoize> use the same cache for scalar and
640e9ce3842Safresh1list context return values, so that the second call uses the scalar
641e9ce3842Safresh1cache that was populated by the first call.  C<complicated> ends up
642*e0680481Safresh1being called only once, and both subsequent calls return C<37> from the
643e9ce3842Safresh1cache, regardless of the calling context.
644e9ce3842Safresh1
645*e0680481Safresh1=back
646*e0680481Safresh1
647e9ce3842Safresh1=head3 List values in scalar context
648e9ce3842Safresh1
649e9ce3842Safresh1Consider this function:
650e9ce3842Safresh1
651e9ce3842Safresh1    sub iota { return reverse (1..$_[0]) }
652e9ce3842Safresh1
653e9ce3842Safresh1This function normally returns a list.  Suppose you memoize it and
654e9ce3842Safresh1merge the caches:
655e9ce3842Safresh1
656e9ce3842Safresh1    memoize 'iota', SCALAR_CACHE => 'MERGE';
657e9ce3842Safresh1
658e9ce3842Safresh1    @i7 = iota(7);
659e9ce3842Safresh1    $i7 = iota(7);
660e9ce3842Safresh1
661e9ce3842Safresh1Here the first call caches the list (1,2,3,4,5,6,7).  The second call
662e9ce3842Safresh1does not really make sense. C<Memoize> cannot guess what behavior
663e9ce3842Safresh1C<iota> should have in scalar context without actually calling it in
664e9ce3842Safresh1scalar context.  Normally C<Memoize> I<would> call C<iota> in scalar
665e9ce3842Safresh1context and cache the result, but the C<SCALAR_CACHE =E<gt> 'MERGE'>
666e9ce3842Safresh1option says not to do that, but to use the cache list-context value
667e9ce3842Safresh1instead. But it cannot return a list of seven elements in a scalar
668e9ce3842Safresh1context. In this case C<$i7> will receive the B<first element> of the
669e9ce3842Safresh1cached list value, namely 7.
670e9ce3842Safresh1
671e9ce3842Safresh1=head3 Merged disk caches
672b39c5158Smillert
673b39c5158SmillertAnother use for C<MERGE> is when you want both kinds of return values
674b39c5158Smillertstored in the same disk file; this saves you from having to deal with
675b39c5158Smillerttwo disk files instead of one.  You can use a normalizer function to
676b39c5158Smillertkeep the two sets of return values separate.  For example:
677b39c5158Smillert
678*e0680481Safresh1        local $MLDBM::UseDB = 'DB_File';
679*e0680481Safresh1        tie my %cache => 'MLDBM', $filename, ...;
680b39c5158Smillert
681b39c5158Smillert	memoize 'myfunc',
682b39c5158Smillert	  NORMALIZER => 'n',
683b39c5158Smillert	  SCALAR_CACHE => [HASH => \%cache],
684e9ce3842Safresh1	  LIST_CACHE => 'MERGE',
685b39c5158Smillert	;
686b39c5158Smillert
687b39c5158Smillert	sub n {
688b39c5158Smillert	  my $context = wantarray() ? 'L' : 'S';
689b39c5158Smillert	  # ... now compute the hash key from the arguments ...
690b39c5158Smillert	  $hashkey = "$context:$hashkey";
691b39c5158Smillert	}
692b39c5158Smillert
693b39c5158SmillertThis normalizer function will store scalar context return values in
694b39c5158Smillertthe disk file under keys that begin with C<S:>, and list context
695b39c5158Smillertreturn values under keys that begin with C<L:>.
696b39c5158Smillert
697b39c5158Smillert=head1 OTHER FACILITIES
698b39c5158Smillert
699b39c5158Smillert=head2 C<unmemoize>
700b39c5158Smillert
701b39c5158SmillertThere's an C<unmemoize> function that you can import if you want to.
702b39c5158SmillertWhy would you want to?  Here's an example: Suppose you have your cache
703b39c5158Smillerttied to a DBM file, and you want to make sure that the cache is
704b39c5158Smillertwritten out to disk if someone interrupts the program.  If the program
705b39c5158Smillertexits normally, this will happen anyway, but if someone types
706b39c5158Smillertcontrol-C or something then the program will terminate immediately
707b39c5158Smillertwithout synchronizing the database.  So what you can do instead is
708b39c5158Smillert
709b39c5158Smillert    $SIG{INT} = sub { unmemoize 'function' };
710b39c5158Smillert
711b39c5158SmillertC<unmemoize> accepts a reference to, or the name of a previously
712b39c5158Smillertmemoized function, and undoes whatever it did to provide the memoized
713b39c5158Smillertversion in the first place, including making the name refer to the
714b39c5158Smillertunmemoized version if appropriate.  It returns a reference to the
715b39c5158Smillertunmemoized version of the function.
716b39c5158Smillert
717b39c5158SmillertIf you ask it to unmemoize a function that was never memoized, it
718b39c5158Smillertcroaks.
719b39c5158Smillert
720b39c5158Smillert=head2 C<flush_cache>
721b39c5158Smillert
722b39c5158SmillertC<flush_cache(function)> will flush out the caches, discarding I<all>
723b39c5158Smillertthe cached data.  The argument may be a function name or a reference
724b39c5158Smillertto a function.  For finer control over when data is discarded or
725b39c5158Smillertexpired, see the documentation for C<Memoize::Expire>, included in
726b39c5158Smillertthis package.
727b39c5158Smillert
728b39c5158SmillertNote that if the cache is a tied hash, C<flush_cache> will attempt to
729b39c5158Smillertinvoke the C<CLEAR> method on the hash.  If there is no C<CLEAR>
730b39c5158Smillertmethod, this will cause a run-time error.
731b39c5158Smillert
732b39c5158SmillertAn alternative approach to cache flushing is to use the C<HASH> option
733b39c5158Smillert(see above) to request that C<Memoize> use a particular hash variable
734b39c5158Smillertas its cache.  Then you can examine or modify the hash at any time in
735b39c5158Smillertany way you desire.  You may flush the cache by using C<%hash = ()>.
736b39c5158Smillert
737b39c5158Smillert=head1 CAVEATS
738b39c5158Smillert
739b39c5158SmillertMemoization is not a cure-all:
740b39c5158Smillert
741b39c5158Smillert=over 4
742b39c5158Smillert
743b39c5158Smillert=item *
744b39c5158Smillert
745b39c5158SmillertDo not memoize a function whose behavior depends on program
746b39c5158Smillertstate other than its own arguments, such as global variables, the time
747b39c5158Smillertof day, or file input.  These functions will not produce correct
748b39c5158Smillertresults when memoized.  For a particularly easy example:
749b39c5158Smillert
750b39c5158Smillert	sub f {
751b39c5158Smillert	  time;
752b39c5158Smillert	}
753b39c5158Smillert
754b39c5158SmillertThis function takes no arguments, and as far as C<Memoize> is
755b39c5158Smillertconcerned, it always returns the same result.  C<Memoize> is wrong, of
756b39c5158Smillertcourse, and the memoized version of this function will call C<time> once
757b39c5158Smillertto get the current time, and it will return that same time
758b39c5158Smillertevery time you call it after that.
759b39c5158Smillert
760b39c5158Smillert=item *
761b39c5158Smillert
762b39c5158SmillertDo not memoize a function with side effects.
763b39c5158Smillert
764b39c5158Smillert	sub f {
765b39c5158Smillert	  my ($a, $b) = @_;
766b39c5158Smillert          my $s = $a + $b;
767b39c5158Smillert	  print "$a + $b = $s.\n";
768b39c5158Smillert	}
769b39c5158Smillert
770b39c5158SmillertThis function accepts two arguments, adds them, and prints their sum.
771*e0680481Safresh1Its return value is the number of characters it printed, but you
772b39c5158Smillertprobably didn't care about that.  But C<Memoize> doesn't understand
773b39c5158Smillertthat.  If you memoize this function, you will get the result you
774b39c5158Smillertexpect the first time you ask it to print the sum of 2 and 3, but
775b39c5158Smillertsubsequent calls will return 1 (the return value of
776b39c5158SmillertC<print>) without actually printing anything.
777b39c5158Smillert
778b39c5158Smillert=item *
779b39c5158Smillert
780b39c5158SmillertDo not memoize a function that returns a data structure that is
781b39c5158Smillertmodified by its caller.
782b39c5158Smillert
783b39c5158SmillertConsider these functions:  C<getusers> returns a list of users somehow,
784b39c5158Smillertand then C<main> throws away the first user on the list and prints the
785b39c5158Smillertrest:
786b39c5158Smillert
787b39c5158Smillert	sub main {
788b39c5158Smillert	  my $userlist = getusers();
789b39c5158Smillert	  shift @$userlist;
790b39c5158Smillert	  foreach $u (@$userlist) {
791b39c5158Smillert	    print "User $u\n";
792b39c5158Smillert	  }
793b39c5158Smillert	}
794b39c5158Smillert
795b39c5158Smillert	sub getusers {
796b39c5158Smillert	  my @users;
797b39c5158Smillert	  # Do something to get a list of users;
798b39c5158Smillert	  \@users;  # Return reference to list.
799b39c5158Smillert	}
800b39c5158Smillert
801b39c5158SmillertIf you memoize C<getusers> here, it will work right exactly once.  The
802b39c5158Smillertreference to the users list will be stored in the memo table.  C<main>
803b39c5158Smillertwill discard the first element from the referenced list.  The next
804b39c5158Smillerttime you invoke C<main>, C<Memoize> will not call C<getusers>; it will
805b39c5158Smillertjust return the same reference to the same list it got last time.  But
806b39c5158Smillertthis time the list has already had its head removed; C<main> will
807b39c5158Smillerterroneously remove another element from it.  The list will get shorter
808b39c5158Smillertand shorter every time you call C<main>.
809b39c5158Smillert
810b39c5158SmillertSimilarly, this:
811b39c5158Smillert
812b39c5158Smillert	$u1 = getusers();
813b39c5158Smillert	$u2 = getusers();
814b39c5158Smillert	pop @$u1;
815b39c5158Smillert
816b39c5158Smillertwill modify $u2 as well as $u1, because both variables are references
817b39c5158Smillertto the same array.  Had C<getusers> not been memoized, $u1 and $u2
818b39c5158Smillertwould have referred to different arrays.
819b39c5158Smillert
820b39c5158Smillert=item *
821b39c5158Smillert
822b39c5158SmillertDo not memoize a very simple function.
823b39c5158Smillert
824b39c5158SmillertRecently someone mentioned to me that the Memoize module made his
825b39c5158Smillertprogram run slower instead of faster.  It turned out that he was
826b39c5158Smillertmemoizing the following function:
827b39c5158Smillert
828b39c5158Smillert    sub square {
829b39c5158Smillert      $_[0] * $_[0];
830b39c5158Smillert    }
831b39c5158Smillert
832b39c5158SmillertI pointed out that C<Memoize> uses a hash, and that looking up a
833b39c5158Smillertnumber in the hash is necessarily going to take a lot longer than a
834b39c5158Smillertsingle multiplication.  There really is no way to speed up the
835b39c5158SmillertC<square> function.
836b39c5158Smillert
837b39c5158SmillertMemoization is not magical.
838b39c5158Smillert
839b39c5158Smillert=back
840b39c5158Smillert
841b39c5158Smillert=head1 PERSISTENT CACHE SUPPORT
842b39c5158Smillert
843b39c5158SmillertYou can tie the cache tables to any sort of tied hash that you want
844b39c5158Smillertto, as long as it supports C<TIEHASH>, C<FETCH>, C<STORE>, and
845b39c5158SmillertC<EXISTS>.  For example,
846b39c5158Smillert
847b39c5158Smillert        tie my %cache => 'GDBM_File', $filename, O_RDWR|O_CREAT, 0666;
848b39c5158Smillert        memoize 'function', SCALAR_CACHE => [HASH => \%cache];
849b39c5158Smillert
850b39c5158Smillertworks just fine.  For some storage methods, you need a little glue.
851b39c5158Smillert
852b39c5158SmillertC<SDBM_File> doesn't supply an C<EXISTS> method, so included in this
853b39c5158Smillertpackage is a glue module called C<Memoize::SDBM_File> which does
854b39c5158Smillertprovide one.  Use this instead of plain C<SDBM_File> to store your
855b39c5158Smillertcache table on disk in an C<SDBM_File> database:
856b39c5158Smillert
857b39c5158Smillert        tie my %cache => 'Memoize::SDBM_File', $filename, O_RDWR|O_CREAT, 0666;
858b39c5158Smillert        memoize 'function', SCALAR_CACHE => [HASH => \%cache];
859b39c5158Smillert
860b39c5158SmillertC<NDBM_File> has the same problem and the same solution.  (Use
861b39c5158SmillertC<Memoize::NDBM_File instead of plain NDBM_File.>)
862b39c5158Smillert
863b39c5158SmillertC<Storable> isn't a tied hash class at all.  You can use it to store a
864b39c5158Smillerthash to disk and retrieve it again, but you can't modify the hash while
865b39c5158Smillertit's on the disk.  So if you want to store your cache table in a
866b39c5158SmillertC<Storable> database, use C<Memoize::Storable>, which puts a hashlike
867b39c5158Smillertfront-end onto C<Storable>.  The hash table is actually kept in
868b39c5158Smillertmemory, and is loaded from your C<Storable> file at the time you
869b39c5158Smillertmemoize the function, and stored back at the time you unmemoize the
870b39c5158Smillertfunction (or when your program exits):
871b39c5158Smillert
872b39c5158Smillert        tie my %cache => 'Memoize::Storable', $filename;
873b39c5158Smillert	memoize 'function', SCALAR_CACHE => [HASH => \%cache];
874b39c5158Smillert
875b39c5158Smillert        tie my %cache => 'Memoize::Storable', $filename, 'nstore';
876b39c5158Smillert	memoize 'function', SCALAR_CACHE => [HASH => \%cache];
877b39c5158Smillert
878*e0680481Safresh1Include the C<nstore> option to have the C<Storable> database written
879*e0680481Safresh1in I<network order>. (See L<Storable> for more details about this.)
880b39c5158Smillert
881b39c5158SmillertThe C<flush_cache()> function will raise a run-time error unless the
882b39c5158Smillerttied package provides a C<CLEAR> method.
883b39c5158Smillert
884b39c5158Smillert=head1 EXPIRATION SUPPORT
885b39c5158Smillert
886b39c5158SmillertSee Memoize::Expire, which is a plug-in module that adds expiration
887b39c5158Smillertfunctionality to Memoize.  If you don't like the kinds of policies
888b39c5158Smillertthat Memoize::Expire implements, it is easy to write your own plug-in
889b39c5158Smillertmodule to implement whatever policy you desire.  Memoize comes with
890b39c5158Smillertseveral examples.  An expiration manager that implements a LRU policy
891b39c5158Smillertis available on CPAN as Memoize::ExpireLRU.
892b39c5158Smillert
893b39c5158Smillert=head1 BUGS
894b39c5158Smillert
895b39c5158SmillertThe test suite is much better, but always needs improvement.
896b39c5158Smillert
897b39c5158SmillertThere is some problem with the way C<goto &f> works under threaded
898b39c5158SmillertPerl, perhaps because of the lexical scoping of C<@_>.  This is a bug
899b39c5158Smillertin Perl, and until it is resolved, memoized functions will see a
900b39c5158Smillertslightly different C<caller()> and will perform a little more slowly
901b39c5158Smillerton threaded perls than unthreaded perls.
902b39c5158Smillert
903b39c5158SmillertSome versions of C<DB_File> won't let you store data under a key of
904b39c5158Smillertlength 0.  That means that if you have a function C<f> which you
905b39c5158Smillertmemoized and the cache is in a C<DB_File> database, then the value of
906b39c5158SmillertC<f()> (C<f> called with no arguments) will not be memoized.  If this
907b39c5158Smillertis a big problem, you can supply a normalizer function that prepends
908b39c5158SmillertC<"x"> to every key.
909b39c5158Smillert
910*e0680481Safresh1=head1 SEE ALSO
911b39c5158Smillert
912*e0680481Safresh1At L<https://perl.plover.com/MiniMemoize/> there is an article about
913b39c5158Smillertmemoization and about the internals of Memoize that appeared in The
914*e0680481Safresh1Perl Journal, issue #13.
915b39c5158Smillert
916*e0680481Safresh1Mark-Jason Dominus's book I<Higher-Order Perl> (2005, ISBN 1558607013,
917*e0680481Safresh1published
918e9ce3842Safresh1by Morgan Kaufmann) discusses memoization (and many other
919e9ce3842Safresh1topics) in tremendous detail. It is available on-line for free.
920*e0680481Safresh1For more information, visit L<https://hop.perl.plover.com/>.
921b39c5158Smillert
922b39c5158Smillert=head1 THANK YOU
923b39c5158Smillert
924e9ce3842Safresh1Many thanks to Florian Ragwitz for administration and packaging
925e9ce3842Safresh1assistance, to John Tromp for bug reports, to Jonathan Roy for bug reports
926e9ce3842Safresh1and suggestions, to Michael Schwern for other bug reports and patches,
927e9ce3842Safresh1to Mike Cariaso for helping me to figure out the Right Thing to Do
928e9ce3842Safresh1About Expiration, to Joshua Gerth, Joshua Chamas, Jonathan Roy
929e9ce3842Safresh1(again), Mark D. Anderson, and Andrew Johnson for more suggestions
930e9ce3842Safresh1about expiration, to Brent Powers for the Memoize::ExpireLRU module,
931e9ce3842Safresh1to Ariel Scolnicov for delightful messages about the Fibonacci
932e9ce3842Safresh1function, to Dion Almaer for thought-provoking suggestions about the
933e9ce3842Safresh1default normalizer, to Walt Mankowski and Kurt Starsinic for much help
934e9ce3842Safresh1investigating problems under threaded Perl, to Alex Dudkevich for
935e9ce3842Safresh1reporting the bug in prototyped functions and for checking my patch,
936e9ce3842Safresh1to Tony Bass for many helpful suggestions, to Jonathan Roy (again) for
937e9ce3842Safresh1finding a use for C<unmemoize()>, to Philippe Verdret for enlightening
938e9ce3842Safresh1discussion of C<Hook::PrePostCall>, to Nat Torkington for advice I
939e9ce3842Safresh1ignored, to Chris Nandor for portability advice, to Randal Schwartz
940e9ce3842Safresh1for suggesting the 'C<flush_cache> function, and to Jenda Krynicky for
941e9ce3842Safresh1being a light in the world.
942b39c5158Smillert
943b39c5158SmillertSpecial thanks to Jarkko Hietaniemi, the 5.8.0 pumpking, for including
944b39c5158Smillertthis module in the core and for his patient and helpful guidance
945b39c5158Smillertduring the integration process.
946b39c5158Smillert
947*e0680481Safresh1=head1 AUTHOR
948*e0680481Safresh1
949*e0680481Safresh1Mark Jason Dominus
950*e0680481Safresh1
951*e0680481Safresh1=head1 COPYRIGHT AND LICENSE
952*e0680481Safresh1
953*e0680481Safresh1This software is copyright (c) 2012 by Mark Jason Dominus.
954*e0680481Safresh1
955*e0680481Safresh1This is free software; you can redistribute it and/or modify it under
956*e0680481Safresh1the same terms as the Perl 5 programming language system itself.
957*e0680481Safresh1
958b39c5158Smillert=cut
959