Skip to content

Commit

Permalink
Merge pull request #486 from flyingcircusio/improve-component_init_pe…
Browse files Browse the repository at this point in the history
…rformance

Improve deployment performance when there are many components.
  • Loading branch information
zagy authored Nov 20, 2024
2 parents 74d2579 + f60e79b commit 81db2f8
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 11 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
- Improve deployment performance when there are many components.

Improves the performance of `Component.__init__` which is called very often.
25 changes: 14 additions & 11 deletions src/batou/component.py
Original file line number Diff line number Diff line change
Expand Up @@ -203,19 +203,22 @@ def root(self):
_prepared = False

def __init__(self, namevar=None, **kw):
init_stack = inspect.stack()
init_stack.reverse()
init_breadcrumbs = []
call_site = init_stack[-2]
self._init_file_path = call_site.filename
self._init_line_number = call_site.lineno
for frame in init_stack:
call_site = sys._getframe(2)
self._init_file_path = call_site.f_code.co_filename
self._init_line_number = call_site.f_code.co_firstlineno
for n in range(20): # limit how deep we look
try:
frame = sys._getframe(n)
except ValueError:
# End of stack
break
if (
"self" in frame.frame.f_locals
and isinstance(frame.frame.f_locals["self"], Component)
and frame.frame.f_code.co_name == "configure"
"self" in frame.f_locals
and isinstance(frame.f_locals["self"], Component)
and frame.f_code.co_name == "configure"
):
component = frame.frame.f_locals["self"]
component = frame.f_locals["self"]
try:
init_breadcrumbs.append(component._breadcrumb)
except AttributeError:
Expand All @@ -227,7 +230,7 @@ def __init__(self, namevar=None, **kw):
)
init_breadcrumbs.append(breadcrumb)

self._init_breadcrumbs = init_breadcrumbs
self._init_breadcrumbs = reversed(init_breadcrumbs)

Component._instances.append(self)
self.timer = batou.utils.Timer(self.__class__.__name__)
Expand Down

0 comments on commit 81db2f8

Please sign in to comment.