Skip to content

Commit

Permalink
Improve performance of plan and spec loaders
Browse files Browse the repository at this point in the history
Ensure to not read source file for each frame when getting frames
from the stack
  • Loading branch information
Nikokrock committed Jan 9, 2024
1 parent f21f9ae commit 99dfb7e
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 4 deletions.
10 changes: 7 additions & 3 deletions src/e3/anod/loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -277,9 +277,13 @@ def spec(name: str) -> Callable[..., Anod]:
:param name: name of the spec to load
"""
spec_repository: AnodSpecRepository | None = None
for k in inspect.stack()[1:]:
if "__spec_repository" in k[0].f_globals:
spec_repository = k[0].f_globals["__spec_repository"]

# Implementation note: context=0 means that the no source context is
# computed for each frame. This improve drastically the performance
# as it avoids reading the source file for each frame.
for k in inspect.stack(context=0)[1:]:
spec_repository = k[0].f_globals.get("__spec_repository")
if spec_repository is not None:
break

assert spec_repository is not None
Expand Down
7 changes: 6 additions & 1 deletion src/e3/electrolyt/plan.py
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,12 @@ def _add_action(self, name: str, *args: Any, **kwargs: Any) -> None:
plan_line = "unknown filename:unknown lineno"
# Retrieve the plan line
try:
caller_frames = inspect.getouterframes(frame=inspect.currentframe())
# Implementation note: context=0 means that the no source context is
# computed for each frame. This improve drastically the performance
# as it avoids reading the source file for each frame.
caller_frames = inspect.getouterframes(
frame=inspect.currentframe(), context=0
)
caller_frames_in_plan = []
for frame in caller_frames:
frame_filename = frame.filename
Expand Down

0 comments on commit 99dfb7e

Please sign in to comment.