Skip to content

Commit 357f2ae

Browse files
committed
Improve performance for large arrays of structures
Prevent an expensive `_path` calculation when it is not required.
1 parent e8afd63 commit 357f2ae

1 file changed

Lines changed: 11 additions & 10 deletions

File tree

imas/ids_coordinates.py

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ def __init__(self, coordinate_spec: str) -> None:
8181
logger.debug(
8282
f"Ignoring invalid coordinate specifier {spec}", exc_info=True
8383
)
84-
self.references: "tuple[IDSPath]" = tuple(refs)
84+
self.references: "tuple[IDSPath, ...]" = tuple(refs)
8585
"""A tuple paths that this coordinate refers to.
8686
"""
8787

@@ -357,28 +357,29 @@ def _capture_goto_errors(self, dim, coordinate):
357357
IDSPath.goto().
358358
"""
359359
did_capture = []
360-
path = self._node._path
361360
try:
362361
yield did_capture
363362
except CoordinateLookupError as exc:
364363
raise ValidationError(exc.args[0])
365364
except IndexError as exc:
366365
# Can happen in IDSPath.goto when an invalid index is encountered.
367366
coordinate_refs = coordinate.format_refs(self._node)
367+
path = self._node._path
368368
raise ValidationError(
369369
f"Error while validating element `{path}`: dimension {dim + 1} has an "
370370
f"invalid index for coordinate(s) {coordinate_refs}."
371371
) from exc
372372
except Exception as exc:
373373
# Ignore all other exceptions and log them
374374
if "Unexpected index" in str(exc):
375-
logger.debug(
376-
"Ignored AoS coordinate outside our tree (see IMAS-4675) of "
377-
"element `%s`, dimension %s, coordinate `%s`",
378-
path,
379-
dim,
380-
coordinate.references,
381-
)
375+
if logger.isEnabledFor(logging.DEBUG):
376+
logger.debug(
377+
"Ignored AoS coordinate outside our tree (see IMAS-4675) "
378+
"of element `%s`, dimension %s, coordinate `%s`",
379+
self._node._path,
380+
dim,
381+
coordinate.references,
382+
)
382383
else:
383384
if self._node._version <= "3.38.1":
384385
version_error = (
@@ -395,7 +396,7 @@ def _capture_goto_errors(self, dim, coordinate):
395396
coordinate.references,
396397
dim,
397398
version_error,
398-
exc_info=1,
399+
exc_info=True,
399400
)
400401
# Flag to the caller that an error was suppressed
401402
did_capture.append(1)

0 commit comments

Comments
 (0)