big objects and avoiding deepcopy?
Robert Kern
robert.kern at gmail.com
Sat Oct 25 16:19:41 EDT 2008
Reckoner wrote:
> I am writing an algorithm that takes objects (i.e. graphs with
> thousands of nodes) into a "hypothetical" state. I need to keep a
> history of these hypothetical objects depending on what happens to
> them later. Note that these hypothetical objects are intimately
> operated on, changed, and made otherwise significantly different from
> the objects they were copied from.
>
> I've been using deepcopy to push the objects into the hypothetical
> state where I operate on them heavily. This is pretty slow since the
> objects are very large.
>
> Is there another way to do this without resorting to deepcopy?
>
> by the way, the algorithm works fine. It's just this part of it that I
> am trying to change.
This is similar to implementing "Undo" functionality in applications. One
solution is to define every operation you can do on the data structure as a pair
of functions, one which does the "forward" operation on the data structure and
one which does the "backward" operation which will return the modified data
structure back to its original state. Each time you do a forward operation,
append the pair of functions to a list (along with any auxiliary data that you
need). Once you have finished with the hypothetical operations, you can work
your way backwards through the list and using the "backwards" operations.
This works fairly well if you have a single data structure that you are managing
this way and a limited set of operations to track. If you have multiple
interacting objects and a large set of operations, things can become cumbersome.
--
Robert Kern
"I have come to believe that the whole world is an enigma, a harmless enigma
that is made terrible by our own mad attempt to interpret it as though it had
an underlying truth."
-- Umberto Eco
More information about the Python-list
mailing list