[Baypiggies] identity cache / flywheel design pattern

Shannon -jj Behrens jjinux at gmail.com
Sat Aug 2 21:59:08 CEST 2008

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
>>> s1 is s2
>>> identity_cache = {}
>>> s1 = identity_cache.setdefault(s1, s1)
>>> s2 = identity_cache.setdefault(s2, s2)
>>> s1 == 'foo'
>>> s1 == s2
>>> s1 is s2

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.


Lisp programmers know the value of everything, but the cost of nothing...
On my Lisp, '() costs 4 bytes.

More information about the Baypiggies mailing list