big objects and avoiding deepcopy?

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


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

G:
{ 0: nodeA, 1: nodeB, 2: nodeC }
G-copy:
G, { 0: nodeD }
G-copy2:
G, { 1: nodeE }
G-copy-copy:
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