diff -r 399d49d4acae Lib/xml/dom/minidom.py --- a/Lib/xml/dom/minidom.py Wed Jun 15 17:12:38 2016 -0500 +++ b/Lib/xml/dom/minidom.py Thu Jun 16 16:45:44 2016 +0200 @@ -77,6 +77,13 @@ if self.childNodes: return self.childNodes[-1] + def _index(self, refChild): + if self._index_cache >= 0 and self._index_cache < len(self.childNodes) \ + and self.childNodes[self._index_cache] == refChild: + return self._index_cache + self._index_cache = self.childNodes.index(refChild) + return self._index_cache + def insertBefore(self, newChild, refChild): if newChild.nodeType == self.DOCUMENT_FRAGMENT_NODE: for c in tuple(newChild.childNodes): @@ -92,12 +99,13 @@ self.appendChild(newChild) else: try: - index = self.childNodes.index(refChild) + index = self._index(refChild) except ValueError: raise xml.dom.NotFoundErr() if newChild.nodeType in _nodeTypes_with_children: _clear_id_cache(self) self.childNodes.insert(index, newChild) + self._index_cache += 1 newChild.nextSibling = refChild refChild.previousSibling = newChild if index: @@ -139,7 +147,7 @@ if newChild.parentNode is not None: newChild.parentNode.removeChild(newChild) try: - index = self.childNodes.index(oldChild) + index = self._index(oldChild) except ValueError: raise xml.dom.NotFoundErr() self.childNodes[index] = newChild @@ -330,6 +338,7 @@ def __init__(self): self.childNodes = NodeList() + self._index_cache = -1 class Attr(Node): @@ -647,6 +656,7 @@ self.prefix = prefix self.namespaceURI = namespaceURI self.childNodes = NodeList() + self._index_cache = -1 self._attrs = {} # attributes are double-indexed: self._attrsNS = {} # tagName -> Attribute @@ -1306,6 +1316,7 @@ self.nodeName = name self.notationName = notation self.childNodes = NodeList() + self._index_cache = -1 self._identified_mixin_init(publicId, systemId) def _get_actualEncoding(self): @@ -1499,6 +1510,7 @@ def __init__(self): self.childNodes = NodeList() + self._index_cache = -1 # mapping of (namespaceURI, localName) -> ElementInfo # and tagName -> ElementInfo self._elem_info = {}