Skip to content

Commit

Permalink
Don't toggle profiler state when calling a profiled function recursively
Browse files Browse the repository at this point in the history
In

    @Profile
    def foo():
       if condition:
           foo()

make sure we don't call `profiler.enable()` or `profiler.disable()` on
recursive invocation of `foo`.
  • Loading branch information
elprans committed Feb 13, 2024
1 parent 06cd3d3 commit 9e56758
Showing 1 changed file with 8 additions and 2 deletions.
10 changes: 8 additions & 2 deletions edb/tools/profiling/profiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ def __init__(
self._dir: Union[str, pathlib.Path, None] = dir
self._profiler: Optional[cProfile.Profile] = None
self._dump_file_path: Optional[str] = None
self._profiler_enabled = False

def __call__(self, func: T) -> T:
"""Apply decorator to a function."""
Expand All @@ -118,11 +119,16 @@ def __call__(self, func: T) -> T:
def wrapper(*args, **kwargs):
tracing_singledispatch.profiling_in_progress.set()
self.n_calls += 1
self.profiler.enable()
profiler_was_enabled_here = False
if not self._profiler_enabled:
self.profiler.enable()
self._profiler_enabled = True
profiler_was_enabled_here = True
try:
return func(*args, **kwargs)
finally:
self.profiler.disable()
if profiler_was_enabled_here:
self.profiler.disable()
if self.n_calls % self.save_every_n_calls == 0:
self.dump_stats()
tracing_singledispatch.profiling_in_progress.clear()
Expand Down

0 comments on commit 9e56758

Please sign in to comment.