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:
parent
5dc1b7a820
commit
1650f91ea4
1 changed files with 10 additions and 5 deletions
|
|
@ -1900,9 +1900,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:
|
||||||
|
|
@ -1931,8 +1928,16 @@ 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)
|
||||||
|
if node.nodeName in default_attributes_restricted_by_tag:
|
||||||
|
for attribute in default_attributes_restricted_by_tag[node.nodeName]:
|
||||||
num += removeDefaultAttributeValue(node, attribute)
|
num += removeDefaultAttributeValue(node, attribute)
|
||||||
|
|
||||||
# Summarily get rid of default properties
|
# Summarily get rid of default properties
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue