diff options
author | Jarkko Hietaniemi <jhi@iki.fi> | 2001-09-12 17:41:49 +0000 |
---|---|---|
committer | Jarkko Hietaniemi <jhi@iki.fi> | 2001-09-12 17:41:49 +0000 |
commit | 3d4a255cbdf51bdaf16ab1a302f453e90e07602f (patch) | |
tree | 8f64863dea01c07d7cd32d64f54b6bd51ece572f /lib/Memoize | |
parent | 9bf12eaf439552772b53d65e2dd335097dac1a84 (diff) | |
download | perl-3d4a255cbdf51bdaf16ab1a302f453e90e07602f.tar.gz |
Upgrade to Memoize 0.66.
p4raw-id: //depot/perl@12006
Diffstat (limited to 'lib/Memoize')
-rw-r--r-- | lib/Memoize/AnyDBM_File.pm | 2 | ||||
-rw-r--r-- | lib/Memoize/Expire.pm | 33 | ||||
-rw-r--r-- | lib/Memoize/README | 15 | ||||
-rw-r--r-- | lib/Memoize/TODO | 6 | ||||
-rw-r--r-- | lib/Memoize/t/expire_module_t.t | 32 | ||||
-rwxr-xr-x | lib/Memoize/t/speed.t | 14 |
6 files changed, 73 insertions, 29 deletions
diff --git a/lib/Memoize/AnyDBM_File.pm b/lib/Memoize/AnyDBM_File.pm index 89ef0e13d6..91f960962f 100644 --- a/lib/Memoize/AnyDBM_File.pm +++ b/lib/Memoize/AnyDBM_File.pm @@ -19,6 +19,8 @@ my $verbose = 1; my $mod; for $mod (@ISA) { # (my $truemod = $mod) =~ s/^Memoize:://; +# my $file = "$mod.pm"; +# $file =~ s{::}{/}g; if (eval "require $mod") { print STDERR "AnyDBM_File => Selected $mod.\n" if $Verbose; @ISA = ($mod); # if we leave @ISA alone, warnings abound diff --git a/lib/Memoize/Expire.pm b/lib/Memoize/Expire.pm index 517ce34391..8bd5999077 100644 --- a/lib/Memoize/Expire.pm +++ b/lib/Memoize/Expire.pm @@ -3,7 +3,7 @@ package Memoize::Expire; # require 5.00556; use Carp; $DEBUG = 0; -$VERSION = '0.65'; +$VERSION = '0.66'; # This package will implement expiration by prepending a fixed-length header # to the font of the cached data. The format of the header will be: @@ -18,6 +18,13 @@ sub _header_size () { length(_header_fmt) } # TIE => [Memoize::Expire, LIFETIME => sec, NUM_USES => n, # TIE => [...] ] +BEGIN { + eval {require Time::HiRes}; + unless ($@) { + Time::HiRes->import('time'); + } +} + sub TIEHASH { my ($package, %args) = @_; my %cache; @@ -54,7 +61,7 @@ sub STORE { sub FETCH { $DEBUG and print STDERR " >> Fetch cached value for $_[1]\n"; my ($data, $last_access, $expire_time, $num_uses_left) = _get_item($_[0]{C}{$_[1]}); - $DEBUG and print STDERR " >> (ttl: ", ($expire_time-time), ", nuses: $num_uses_left)\n"; + $DEBUG and print STDERR " >> (ttl: ", ($expire_time-time()), ", nuses: $num_uses_left)\n"; $num_uses_left--; $last_access = time; _set_header(@_, $data, $last_access, $expire_time, $num_uses_left); @@ -113,8 +120,6 @@ sub _get_header { 1; -# Below is the stub of documentation for your module. You better edit it! - =head1 NAME Memoize::Expire - Plug-in module for automatic expiration of memoized values @@ -123,7 +128,7 @@ Memoize::Expire - Plug-in module for automatic expiration of memoized values use Memoize; use Memoize::Expire; - tie my %cache => 'Memoize::Expire', + tie my %cache => 'Memoize::Expire', LIFETIME => $lifetime, # In seconds NUM_USES => $n_uses; @@ -224,6 +229,11 @@ STORE Given a function argument and the corresponding function value, store them into the cache. +=item +CLEAR + +(Optional.) Flush the cache completely. + =back The user who wants the memoization cache to be expired according to @@ -262,7 +272,7 @@ cache item after ten seconds. sub TIEHASH { my ($package, %args) = @_; - my $cache = $args{$HASH} || {}; + my $cache = $args{HASH} || {}; bless $cache => $package; } @@ -299,7 +309,7 @@ entirely absent or was older than ten seconds. You should always support a C<HASH> argument to C<TIEHASH> that ties the underlying cache so that the user can specify that the cache is also persistent or that it has some other interesting semantics. The -example above demonstrates how to do this, as does C<Memozie::Expire>. +example above demonstrates how to do this, as does C<Memoize::Expire>. Another sample module, C<Memoize::Saves>, is included with this package. It implements a policy that allows you to specify that @@ -326,7 +336,7 @@ This module is experimental, and may contain bugs. Please report bugs to the address below. Number-of-uses is stored as a 16-bit unsigned integer, so can't exceed -65535. +65535. Because of clock granularity, expiration times may occur up to one second sooner than you expect. For example, suppose you store a value @@ -334,9 +344,8 @@ with a lifetime of ten seconds, and you store it at 12:00:00.998 on a certain day. Memoize will look at the clock and see 12:00:00. Then 9.01 seconds later, at 12:00:10.008 you try to read it back. Memoize will look at the clock and see 12:00:10 and conclude that the value -has expired. Solution: Build an expiration policy module that uses -Time::HiRes to examine a clock with better granularity. Contributions -are welcome. Send them to: +has expired. This will probably not occur if if you have +C<Time::HiRes> installed. =head1 AUTHOR @@ -356,6 +365,6 @@ http://www.plover.com/~mjd/perl/Memoize/ (for news and updates) I maintain a mailing list on which I occasionally announce new versions of Memoize. The list is for announcements only, not discussion. To join, send an empty message to -mjd-perl-memoize-request@Plover.com. +mjd-perl-memoize-request@Plover.com. =cut diff --git a/lib/Memoize/README b/lib/Memoize/README index 011c4bfed1..b7abb5f4c3 100644 --- a/lib/Memoize/README +++ b/lib/Memoize/README @@ -1,7 +1,7 @@ Name: Memoize What: Transparently speed up functions by caching return values. -Version: 0.65 +Version: 0.66 Author: Mark-Jason Dominus (mjd-perl-memoize+@plover.com) ################################################################ @@ -25,6 +25,19 @@ If not, please send me a report that mentions which tests failed. The address is: mjd-perl-memoize+@plover.com. ################################################################ +What's new since 0.65: + +Test changes only. + + 0.62 was the fist version that would be distributed with Perl. + I got so absorbed in integrating it that I wrote some tests + that used Time::HiRes. I knew this was safe because + Time::HiRes is also distributed with the same versions of + Perl. I totally forgot that some people will get the module + off of CPAN without Perl and they may not have TIme::HiRes. + Sorry! + +################################################################ What's new since 0.62: diff --git a/lib/Memoize/TODO b/lib/Memoize/TODO index fad3262615..59686125cb 100644 --- a/lib/Memoize/TODO +++ b/lib/Memoize/TODO @@ -1,4 +1,4 @@ -# Version 0.05 alpha $Revision: 1.5 $ $Date: 1999/09/17 14:57:55 $ +# Version 0.05 alpha $Revision: 1.6 $ $Date: 2001/06/24 17:11:26 $ =head1 TO DO @@ -336,10 +336,10 @@ same analysis and make the same adjustments to the hash. If the normalizer could make the adjustments and save the changes in @_, you wouldn't have to do it twice. -=item* +=item * 20010623 Add CLEAR methods to tied hash modules. -=item* +=item * 20010623 You get a warning if you try to use DB_File as LIST_CACHE, because it won't store lists. But if you use it as the underlying cache with an expiration manager in the middle, no warning---the diff --git a/lib/Memoize/t/expire_module_t.t b/lib/Memoize/t/expire_module_t.t index 7032f65212..3cc3de13f8 100644 --- a/lib/Memoize/t/expire_module_t.t +++ b/lib/Memoize/t/expire_module_t.t @@ -2,7 +2,15 @@ use lib '..'; use Memoize; -use Time::HiRes 'time'; +BEGIN { + eval {require Time::HiRes}; + if ($@ || $ENV{SLOW}) { +# $SLOW_TESTS = 1; + } else { + 'Time::HiRes'->import('time'); + } +} + my $DEBUG = 0; my $n = 0; @@ -48,7 +56,7 @@ sub now { time; } -tie my %cache => 'Memoize::Expire', LIFETIME => 8; +tie my %cache => 'Memoize::Expire', LIFETIME => 10; memoize 'now', SCALAR_CACHE => [HASH => \%cache ], LIST_CACHE => 'FAULT' @@ -64,42 +72,42 @@ for (1,2,3) { ++$n; print "not " unless close_enough($when{$_}, time()); print "ok $n\n"; - sleep 3 if $_ < 3; + sleep 4 if $_ < 3; $DEBUG and print "# ", time()-$t0, "\n"; } -# values will now expire at T=8, 11, 14 -# it is now T=6 +# values will now expire at T=10, 14, 18 +# it is now T=8 -# T+6 +# T+8 for (1,2,3) { $again{$_} = now($_); # Should be the same as before, because of memoization } -# T+6 +# T+8 foreach (1,2,3) { ++$n; print "not " unless close_enough($when{$_}, $again{$_}); print "ok $n\n"; } -wait_until(9.5); # now(1) expires +wait_until(12); # now(1) expires print "not " unless close_enough(time, $again{1} = now(1)); ++$n; print "ok $n\n"; -# T+9.5 +# T+12 foreach (2,3) { # Should not have expired yet. ++$n; print "not " unless close_enough(scalar(now($_)), $again{$_}); print "ok $n\n"; } -wait_until(12.5); # now(2) expires +wait_until(16); # now(2) expires -# T+12.5 +# T+16 print "not " unless close_enough(time, $again{2} = now(2)); ++$n; print "ok $n\n"; -# T+12.5 +# T+16 foreach (1,3) { # 1 is good again because it was recomputed after it expired ++$n; print "not " unless close_enough(scalar(now($_)), $again{$_}); diff --git a/lib/Memoize/t/speed.t b/lib/Memoize/t/speed.t index ef30a8138d..6d21906573 100755 --- a/lib/Memoize/t/speed.t +++ b/lib/Memoize/t/speed.t @@ -12,15 +12,27 @@ $| = 1; # If we don't say anything, maybe nobody will notice. # print STDERR "\nWarning: I'm testing the speedup. This might take up to thirty seconds.\n "; +my $COARSE_TIME = 1; + +sub times_to_time { my ($u) = times; $u; } +if ($^O eq 'riscos') { + eval {require Time::HiRes; *my_time = \&Time::HiRes::time }; + if ($@) { *my_time = sub { time }; $COARSE_TIME = 1 } +} else { + *my_time = \×_to_time; +} + print "1..6\n"; + + # This next test finds an example that takes a long time to run, then # checks to make sure that the run is actually speeded up by memoization. # In some sense, this is the most essential correctness test in the package. # # We do this by running the fib() function with successfily larger -# arguments until we find one that tales at leasrtt $LONG_RUN seconds +# arguments until we find one that tales at least $LONG_RUN seconds # to execute. Then we memoize fib() and run the same call cagain. If # it doesn't produce the same test in less than one-tenth the time, # something is seriously wrong. |