Optimize removeDefaultAttributeValues

Avoid looping over DefaultAttribute(s) that are not relevant for a
given node.  This skips a lot of calls to removeDefaultAttributeValue
but more importantly, it avoids "node.nodeName not in attribute.elements"
line in removeDefaultAttributeValue.  As attribute.elements is a list, this
becomes expensive for "larger lists" (or in this case when there are a lot
of attributes).

This seems to remove about 1½-2 minutes of runtime (out of ~8) on the
1_42_polytope_7-cube.svg test case provided in #184.

Signed-off-by: Niels Thykier <niels@thykier.net>
This commit is contained in:
Niels Thykier 2018-04-10 05:53:21 +00:00
parent 58ae54021d
commit 975a84f50e

View file

@ -1910,9 +1910,6 @@ def removeDefaultAttributeValue(node, attribute):
if not node.hasAttribute(attribute.name): if not node.hasAttribute(attribute.name):
return 0 return 0
if (attribute.elements is not None) and (node.nodeName not in attribute.elements):
return 0
# differentiate between text and numeric values # differentiate between text and numeric values
if isinstance(attribute.value, str): if isinstance(attribute.value, str):
if node.getAttribute(attribute.name) == attribute.value: if node.getAttribute(attribute.name) == attribute.value:
@ -1941,9 +1938,17 @@ def removeDefaultAttributeValues(node, options, tainted=set()):
if node.nodeType != Node.ELEMENT_NODE: if node.nodeType != Node.ELEMENT_NODE:
return 0 return 0
# Conditionally remove all default attributes defined in 'default_attributes' (a list of 'DefaultAttribute's) # Remove all default attributes. The remoteDefaultAttributeValue
for attribute in default_attributes: # function deals with "if/when" we are allowed to remove the
# attribute as long as we supply it only with attributes that are
# applicable for this given node. That part is handled by using
# default_attributes_unrestricted and
# default_attributes_restricted_by_tag
for attribute in default_attributes_unrestricted:
num += removeDefaultAttributeValue(node, attribute) num += removeDefaultAttributeValue(node, attribute)
if node.nodeName in default_attributes_restricted_by_tag:
for attribute in default_attributes_restricted_by_tag[node.nodeName]:
num += removeDefaultAttributeValue(node, attribute)
# Summarily get rid of default properties # Summarily get rid of default properties
attributes = [node.attributes.item(i).nodeName for i in range(node.attributes.length)] attributes = [node.attributes.item(i).nodeName for i in range(node.attributes.length)]