[lxml-dev] Speed-up for moving elements inside the same document
Hi, I decided to dislike the idea of deep-traversing a tree (changeDocumentBelow) whenever its root element is moved to a different parent. So I tried to come up with some cases where this is not necessary. One such case is this: ----------------------- from lxml.etree import Element, SubElement a = Element('{a}test') # create ten children with a child each which has another child map(SubElement, map(SubElement, map(SubElement, [a]*10, ['{ns%s}e'%i for i in range(10)]), ['{x}c1']*10), ['{y}c2']*10) for i in range(100000): el = a[0] a[-1] = el ----------------------- Here, we basically change the order of the elements below a single parent. This case gets about 20% faster on my machine if we skip the traversal in changeDocumentBelow. It is obviously not necessary, since the document reference is not changed anyway. If the tree structure below the moved element is bigger, the gain also increases. I added a third argument to the function that switches recursion on or off. It is set to False by the calling functions if the moved node and its new parent are in the same document. If this is the case, it means that all their children must also be in the same document (by induction). I tried some other cases, but most of them seem to induce infinite loops in the libxml2 calls - I'm not quite sure why. Anyway, this one is a simpler one and works as expected. I would like to apply the attached patch, so, if others are interested, I'd be glad to get some feedback if it changes something in their real-world use cases... Stefan
participants (1)
-
Stefan Behnel