Memoizing and WeakValueDictionary

Discussion in 'Python' started by Paul McGuire, Jan 4, 2009.

  1. Paul McGuire

    Paul McGuire Guest

    Just wanted to share some experience I had in doing some memory and
    performance tuning of a graphics script. I've been running some long-
    running scripts on high-resolution images, and added memoizing to
    optimize/minimize object creation (my objects are immutable, so better
    to reuse objects from a cache than constantly create and discard
    instances). This helped my script early on, but as the data files got
    larger and larger, and runs got longer and longer, the object memoize
    cache started to suck up some serious memory, and performance started
    to degrade for a different reason - low memory -> page thrashing ->
    program crawling.

    It occurred to me that in my program, a given object may get used for
    a while, but once its use declines, it doesn't come back for a while.
    At first blush, I thought about using some sort of LRU cache, but
    first I tried the WeakValueDictionary from the wkref module. This
    worked great!

    Now I get fairly optimal reuse of my immutable instances, but my
    object cache doesn't grow without bounds.

    Does this sound like a correct interpretation of this behavior? If
    so, it would seem that WeakValueDictionary would be a good
    recommendation to go along with any memoizing implementations.
     
    Paul McGuire, Jan 4, 2009
    #1
    1. Advertising

  2. Paul McGuire

    Guest

    On Jan 4, 5:55 am, Paul McGuire <> wrote:
    > Just wanted to share some experience I had in doing some memory and
    > performance tuning of a graphics script.  I've been running some long-
    > running scripts on high-resolution images, and added memoizing to
    > optimize/minimize object creation (my objects are immutable, so better
    > to reuse objects from a cache than constantly create and discard
    > instances).  This helped my script early on, but as the data files got
    > larger and larger, and runs got longer and longer, the object memoize
    > cache started to suck up some serious memory, and performance started
    > to degrade for a different reason - low memory -> page thrashing ->
    > program crawling.
    >
    > It occurred to me that in my program, a given object may get used for
    > a while, but once its use declines, it doesn't come back for a while.
    > At first blush, I thought about using some sort of LRU cache, but
    > first I tried the WeakValueDictionary from the wkref module.  This
    > worked great!
    >
    > Now I get fairly optimal reuse of my immutable instances, but my
    > object cache doesn't grow without bounds.
    >
    > Does this sound like a correct interpretation of this behavior?  If
    > so, it would seem that WeakValueDictionary would be a good
    > recommendation to go along with any memoizing implementations.


    It depends on what you use memoizing for. If your memoized function
    return data from a dataset, then yeah, you want to use a weakref
    scheme because when such data is removed from the dataset, you want
    the memory associated to it to be freed. However, if you use memoizing
    on a function that creates the return value, you don't want to use a
    weakref scheme, because it makes the memoizing useless: If the caller
    of the memoized function doesn't keep a reference to the returned
    value, there will be no memoizing taking place for the next call of
    the function with the same arguments.

    To make it short: it depends.
     
    , Jan 4, 2009
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Mark

    Memoizing Generators

    Mark, Jul 8, 2003, in forum: Python
    Replies:
    2
    Views:
    348
  2. Luke
    Replies:
    0
    Views:
    330
  3. Bryan

    WeakValueDictionary

    Bryan, Oct 3, 2004, in forum: Python
    Replies:
    3
    Views:
    969
    Andrew Dalke
    Oct 3, 2004
  4. Daishi  Harada

    Memoizing decorator

    Daishi Harada, Dec 6, 2005, in forum: Python
    Replies:
    6
    Views:
    428
    Daishi Harada
    Dec 8, 2005
  5. weakref and memoizing

    , Jan 20, 2006, in forum: Python
    Replies:
    0
    Views:
    385
Loading...

Share This Page