Skip to content

Commit a4340b0

Browse files
committed
perf: cache path resolution and validity checks in tracer hot path
Replace per-call Path.is_relative_to() and Path.exists() (stat syscall) with a single cached lookup per unique co_filename. Use os.path.realpath and str.startswith instead of pathlib to avoid object allocation on the hot path. Profile showed these pathlib ops at ~15% self-time.
1 parent d05d103 commit a4340b0

1 file changed

Lines changed: 12 additions & 10 deletions

File tree

codeflash/tracing/tracing_new_process.py

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,7 @@ def __init__(
125125
self.max_function_count = max_function_count
126126
self.config = config
127127
self.project_root = project_root
128+
self.project_root_str = str(project_root) + os.sep if project_root else ""
128129
console.rule(f"Project Root: {self.project_root}", style="bold blue")
129130
self.ignored_functions = {"<listcomp>", "<genexpr>", "<dictcomp>", "<setcomp>", "<lambda>", "<module>"}
130131

@@ -327,19 +328,20 @@ def tracer_logic(self, frame: FrameType, event: str) -> None:
327328
if code.co_name in self.ignored_functions:
328329
return
329330

330-
# Now resolve file path only if we need it
331+
# Resolve file path and check validity (cached)
331332
co_filename = code.co_filename
332333
if co_filename in self.path_cache:
333-
file_name = self.path_cache[co_filename]
334+
file_name, is_valid = self.path_cache[co_filename]
335+
if not is_valid:
336+
return
334337
else:
335-
file_name = Path(co_filename).resolve()
336-
self.path_cache[co_filename] = file_name
337-
# TODO : It currently doesn't log the last return call from the first function
338-
339-
if not file_name.is_relative_to(self.project_root):
340-
return
341-
if not file_name.exists():
342-
return
338+
resolved = os.path.realpath(co_filename)
339+
# startswith is cheaper than Path.is_relative_to, os.path.exists avoids Path construction
340+
is_valid = resolved.startswith(self.project_root_str) and os.path.exists(resolved)
341+
file_name = Path(resolved) if is_valid else None
342+
self.path_cache[co_filename] = (file_name, is_valid)
343+
if not is_valid:
344+
return
343345
if self.functions and code.co_name not in self.functions:
344346
return
345347
class_name = None

0 commit comments

Comments
 (0)