Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions Lib/test/test_minidom.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import copy
import pickle
import sys
from StringIO import StringIO
from test.test_support import verbose, run_unittest, findfile
import unittest
Expand Down Expand Up @@ -151,6 +152,45 @@ def testAppendChild(self):
self.confirm(dom.documentElement.childNodes[-1].data == "Hello")
dom.unlink()

def testAppendChildNoQuadraticComplexity(self):
impl = getDOMImplementation()

def work(n):
doc = impl.createDocument(None, "some_tag", None)
element = doc.documentElement
total_lines = [0]

# Count the lines executed in minidom as a proxy for work done
def line_counter(frame, event, arg):
total_lines[0] += 1
return line_counter

def tracer(frame, event, arg):
if frame.f_globals.get('__name__') == 'xml.dom.minidom':
return line_counter
return None

sys.settrace(tracer)
try:
for _ in range(n):
child = doc.createElement("child")
element.appendChild(child)
element = child
finally:
sys.settrace(None)
return total_lines[0]

# Doubling N should not ~quadruple the work.
w1 = work(256)
w2 = work(512)
w3 = work(1024)

self.assertGreater(w1, 0)
r1 = float(w2) / w1
r2 = float(w3) / w2
self.assertLess(max(r1, r2), 3.2,
"Possible quadratic behavior: work=%r ratios=%r" % ((w1, w2, w3), (r1, r2)))

def testAppendChildFragment(self):
dom, orig, c1, c2, c3, frag = self._create_fragment_test_nodes()
dom.documentElement.appendChild(frag)
Expand Down
9 changes: 1 addition & 8 deletions Lib/xml/dom/minidom.py
Original file line number Diff line number Diff line change
Expand Up @@ -283,13 +283,6 @@ def _append_child(self, node):
childNodes.append(node)
node.__dict__["parentNode"] = self

def _in_document(node):
# return True iff node is part of a document tree
while node is not None:
if node.nodeType == Node.DOCUMENT_NODE:
return True
node = node.parentNode
return False

def _write_data(writer, data):
"Writes datachars to writer."
Expand Down Expand Up @@ -1468,7 +1461,7 @@ def _clear_id_cache(node):
if node.nodeType == Node.DOCUMENT_NODE:
node._id_cache.clear()
node._id_search_stack = None
elif _in_document(node):
elif node.ownerDocument:
node.ownerDocument._id_cache.clear()
node.ownerDocument._id_search_stack= None

Expand Down