From dbc96d9024796a9386376f36b81858083a1f1969 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Eduard=20Br=C3=B6cker?= Date: Fri, 21 Jun 2024 13:45:29 +0200 Subject: [PATCH] cache short_names for spead up parsing (#801) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Eduard Bröcker --- src/canmatrix/formats/arxml.py | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/src/canmatrix/formats/arxml.py b/src/canmatrix/formats/arxml.py index 43b8a888..1c92e286 100644 --- a/src/canmatrix/formats/arxml.py +++ b/src/canmatrix/formats/arxml.py @@ -58,12 +58,14 @@ class Earxml: def __init__(self): self.xml_element_cache = dict() # type: typing.Dict[str, _Element] self.path_cache = {} + self.sn_cache = {} def fill_caches(self, start_element=None, ar_path=""): if start_element is None: start_element = self.root self.path_cache = {} if start_element.tag == self.ns + "SHORT-NAME": + self.sn_cache[start_element.getparent()] = start_element.text return start_element.text for sub_element in start_element: text = sub_element.text @@ -155,13 +157,8 @@ def get_short_name_path(self, shortname_path): def get_short_name(self, element): # type: (_Element, str) -> str """Get element short name.""" - if element is None: - return "" - name = element.find('./' + self.ns + 'SHORT-NAME') - if name is not None and name.text is not None: - return name.text - return "" - + return self.sn_cache.get(element, "") + def follow_ref(self, start_element, element_name): ref_element = self.find(element_name, start_element) if ref_element is None: