big objects and avoiding deepcopy?

Aaron Brady castironpi at
Sun Oct 26 01:12:54 CEST 2008

On Oct 24, 1:11 pm, Reckoner <recko... at> 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.
> Thanks in advance.

This solution takes a level of indirection.

Each graph has a stack of namespaces mapping names to nodes.

{ 0: nodeA, 1: nodeB, 2: nodeC }
G, { 0: nodeD }
G, { 1: nodeE }
G-copy, { 3: nodeF }

If a key isn't found in the dictionary of a graph, its parent graph is
searched, and so on.  Then G-copy[ 0 ] is nodeD, G-copy[ 1 ] is nodeB,
G-copy2[ 2 ] is nodeC, G-copy-copy[ 0 ] is nodeD.  It might take a
significant change to your implementation, however.

More information about the Python-list mailing list