A blog about skating and cycling, or vice versa

No thanks for the Memoize#

Wed, 18 Oct 2006 17:36:03 +0000

NAME
       Memoize - Make functions faster by trading space for time

SYNOPSIS # This is the documentation for Memoize 1.01 use Memoize; memoize('slowfunction'); slowfunction(arguments); # Is faster than it was before

This is normally all you need to know.

To the extent that I failed to fully consider the implications of the rest of the Memoize manual page (i.e. the bits I wouldn't normally need to know - and in particular the section describing the "default normalizer") the strange and apparently intermittent bug in $WORK-related code I have just spent the last week chasing could be considered my own damn fault. Here's the short summary, though: the wrapper for the memoized function stringizes and concatenates the arguments together and then uses that as a key into a hash table to see if it's been called with those arguments before. This does not work when the arguments are objects.

:; cat test.pl

package Test; sub new { my $o= bless { HELLO => 'goodbye', DATA => "0" x 2048 }, "Test"; return $o; }

package main;

my %seen; for(my $i=0;$i<10000;$i++) { my $obj=Test->new; warn $obj; die "$obj repeated at iteration $i" if($seen{"$obj"}); $seen{"$obj"}=1; }

:; perl test.pl Test=HASH(0x814ed9c) at test.pl line 15. Test=HASH(0x814f7bc) at test.pl line 15. Test=HASH(0x814f5e8) at test.pl line 15. Test=HASH(0x814ed9c) at test.pl line 15. Test=HASH(0x814ed9c) repeated at iteration 3 at test.pl line 16.

Won't be making that mistake again.