Skip to content

Commit 2ed454b

Browse files
committed
Speedup setting attributes on etree impleemntations
1 parent a723554 commit 2ed454b

File tree

1 file changed

+15
-12
lines changed

1 file changed

+15
-12
lines changed

html5lib/treebuilders/etree.py

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -61,16 +61,17 @@ def _getAttributes(self):
6161
return self._element.attrib
6262

6363
def _setAttributes(self, attributes):
64-
# Delete existing attributes first
65-
# XXX - there may be a better way to do this...
66-
for key in list(self._element.attrib.keys()):
67-
del self._element.attrib[key]
68-
for key, value in attributes.items():
69-
if isinstance(key, tuple):
70-
name = "{%s}%s" % (key[2], key[1])
71-
else:
72-
name = key
73-
self._element.set(name, value)
64+
el_attrib = self._element.attrib
65+
el_attrib.clear()
66+
if attributes:
67+
# calling .items _always_ allocates, and the above truthy check is cheaper than the
68+
# allocation on average
69+
for key, value in attributes.items():
70+
if isinstance(key, tuple):
71+
name = "{%s}%s" % (key[2], key[1])
72+
else:
73+
name = key
74+
el_attrib[name] = value
7475

7576
attributes = property(_getAttributes, _setAttributes)
7677

@@ -129,8 +130,10 @@ def insertText(self, data, insertBefore=None):
129130

130131
def cloneNode(self):
131132
element = type(self)(self.name, self.namespace)
132-
for name, value in self.attributes.items():
133-
element.attributes[name] = value
133+
try:
134+
element._element.attrib = self._element.attrib.copy()
135+
except AttributeError:
136+
element.attributes = self.attributes
134137
return element
135138

136139
def reparentChildren(self, newParent):

0 commit comments

Comments
 (0)