Skip to content

Commit f3c4db3

Browse files
lor1113Lorenzo Curcio
andauthored
Add cache for signifiant speedup on apply_schema() (#97)
* added cache for apply_schema() element matching * fixed flake8 formatting * changed to Union[] syntax for python 3.9 support * final w3c fix for element match cache --------- Co-authored-by: Lorenzo Curcio <Lorenzo.curcio@valuecuberesearch.com>
1 parent 7476db8 commit f3c4db3

1 file changed

Lines changed: 7 additions & 1 deletion

File tree

elementpath/xpath_nodes.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
# @author Davide Brunato <brunato@sissa.it>
99
#
1010
import importlib
11-
from collections import deque
11+
from collections import defaultdict, deque
1212
from collections.abc import Iterator
1313
from urllib.parse import urljoin
1414
from typing import cast, Any, Optional, TYPE_CHECKING, Union
@@ -1250,6 +1250,9 @@ def apply_schema(self, schema: 'AbstractSchemaProxy') -> None:
12501250
delattr(root_node, '_attributes')
12511251

12521252
iterators: deque[Any] = deque()
1253+
element_match_cache: defaultdict[
1254+
int, dict[Union[str, None], Union[XsdElementProtocol, None]]
1255+
] = defaultdict(dict)
12531256
while True:
12541257
for node in children:
12551258
if not isinstance(node, EtreeElementNode):
@@ -1268,12 +1271,15 @@ def apply_schema(self, schema: 'AbstractSchemaProxy') -> None:
12681271
xsd_element = schema.get_element(node.name)
12691272
elif (content := xsd_types[-1].model_group) is None:
12701273
xsd_element = None
1274+
elif node.name in (sub_cache := element_match_cache[id(content)]):
1275+
xsd_element = sub_cache[node.name]
12711276
else:
12721277
for xsd_element in content.iter_elements():
12731278
if xsd_element.is_matching(node.name):
12741279
if xsd_element.name != node.name:
12751280
# a wildcard or a substitute
12761281
xsd_element = schema.get_element(node.name)
1282+
sub_cache[node.name] = xsd_element
12771283
break
12781284
else:
12791285
xsd_element = None

0 commit comments

Comments
 (0)