[Baypiggies] identity cache / flywheel design pattern

jim jim at well.com
Sat Aug 2 22:13:07 CEST 2008

   i wonder if you have a newbie nugget here 
regardless of subsequent improvements: would 
you please explain what's going on in the two 
setdefault() statements that makes this work? 
what's being returned to s1 and then to s2? 

On Sat, 2008-08-02 at 12:59 -0700, Shannon -jj Behrens wrote:
> I'm have a program that intrinsically eats up a ton of memory.  I
> often read lines from a file that are the same, so I end up with
> multiple copies of the same data in memory.  I know that strings are
> immutable, so there's no need for me to have two pointers to different
> but equal data when I can have two pointers to the same data.  I
> thought of a simple trick to fix this:
> >>> s1 = "foo"
> >>> s2 = ''.join(['f', 'o', 'o'])
> >>> s1 == s2
> True
> >>> s1 is s2
> False
> >>> identity_cache = {}
> >>> s1 = identity_cache.setdefault(s1, s1)
> >>> s2 = identity_cache.setdefault(s2, s2)
> >>> s1 == 'foo'
> True
> >>> s1 == s2
> True
> >>> s1 is s2
> True
> Of course, in a real library, I'd use lazy references.  Can anyone
> comment on this trick?  Does it already exist in the library?  Is
> there a standard name for this besides the flywheel design pattern?
> Is that the correct name?  I might go looking to see if it's in the
> Cookbook, but I'm not even sure if that's the right name.
> Thanks,
> -jj

More information about the Baypiggies mailing list