fast deep-copying of instances

Robert Brewer fumanchu at amor.org
Wed Jan 28 10:24:49 EST 2004


Michal Vitecek wrote:
>  hello,
> 
>  i have a data cache which sits between a slow database and 
> my server and
>  it's used to cache data retrieved from the database. in 
> order for it to
>  work correctly (race-conditions) i need a fast deep-copying of
>  instances (the cache is used heavily).
> 
>  i tried to use copy module but it's *very* slow, then i tried cPickle
>  to pickle and depickle instances which was faster, but still slow.
>  
>  then i've come across cCopy which is 8x faster than copy, but
>  unfortunately it doesn't work with new style classes because type()'s
>  return is crappy for them. so the question is: does there 
> exist a fast
>  copy module which works with new style classes?

If you don't find something pre-built, you can always build it yourself
into the classes which you are caching, via __copy__ and __deepcopy__
methods.:

    def __copy__(self):
        newUnit = self.__class__()
        newUnit.ID = u''
        newUnit._data.update(self._data)
        newUnit.dirty = False
        newUnit.concrete = False
        return newUnit

>From the Library ref on the copy module:

"In order for a class to define its own copy implementation, it can
define special methods __copy__() and __deepcopy__(). The former is
called to implement the shallow copy operation; no additional arguments
are passed. The latter is called to implement the deep copy operation;
it is passed one argument, the memo dictionary. If the __deepcopy__()
implementation needs to make a deep copy of a component, it should call
the deepcopy() function with the component as first argument and the
memo dictionary as second argument."

HTH!


Robert Brewer
MIS
Amor Ministries
fumanchu at amor.org




More information about the Python-list mailing list