[XML-SIG] Building a DOM tree

Dieter Maurer dieter@handshake.de
Thu, 25 Mar 1999 20:20:35 +0000 (/etc/localtime)


Andrew M. Kuchling writes:
 > Carsten Oberscheid writes:
 > >At 16:22 23.03.99 -0500, Andrew M. Kuchling wrote:
 > >Assuming that each Node object can be a member only of one single DOM tree,
 > >wouldn't it be possible to replace the _parent_relation member of the
 > >document element by one global _parent_relation dictionary on module level? 
 > >
 > >   xml.dom.core._parent_relation == { id(childNode): parentNode, ... }
 > 
 > 	Hmm... hmmm... no, I can't think of any reason that wouldn't
 > work.  Nodes can only have a single parent, and you can't mix nodes
 > from two different document trees (unless you're Fred Drake), so key
 > collisions aren't possible.  That would mean there's a single
 > dictionary with lots of keys, testing Python's dictionary code a bit
 > more, but dictionaries are supposed to handle that sort of thing, so
 > it shouldn't cause any problems.  Shouldn't cause any problems for
 > threading, either.  Hmmm...
Unfortunetely, it would not solve the primary problem: safe
garbage collection of unused DOM nodes.

  Suppose, you remove the last (application) reference to a
  DOM tree. Then, this DOM tree should be garbaged collected.
  It is not, however, because the child "c" of the root
  has an association "id(c) : root" in the global parent_relation
  dictionary.

You still remember "WeakDict"s
(URL:http://www.handshake.de/~dieter/pyprojects/weakdict.html)?
They would remove problems with cycles and parent pointers.
However, in some rare cases, the upper context of a node
might be lost prematurely (because parent and document owner references
are not reference counted, a reference to an internal node
does not protect its upper context).

- Dieter