From 9e567587f2ed171b5c38b614210dcba501012e20 Mon Sep 17 00:00:00 2001 From: Elvis Pranskevichus Date: Tue, 13 Feb 2024 10:49:29 -0800 Subject: [PATCH] Don't toggle profiler state when calling a profiled function recursively In @profile def foo(): if condition: foo() make sure we don't call `profiler.enable()` or `profiler.disable()` on recursive invocation of `foo`. --- edb/tools/profiling/profiler.py | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/edb/tools/profiling/profiler.py b/edb/tools/profiling/profiler.py index 4c9ca9301df..4d0e41b982c 100644 --- a/edb/tools/profiling/profiler.py +++ b/edb/tools/profiling/profiler.py @@ -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.""" @@ -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()