Skip to content

Commit a80bb5b

Browse files
committed
perf: use string-based path ops in tracer hot path, defer Path construction
Keep file_name as a plain str throughout the per-call path. Path is only constructed once per unique function in the first-time-seen branch where filter_files_optimized and FunctionModules need it.
1 parent a4340b0 commit a80bb5b

1 file changed

Lines changed: 8 additions & 7 deletions

File tree

codeflash/tracing/tracing_new_process.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -338,10 +338,10 @@ def tracer_logic(self, frame: FrameType, event: str) -> None:
338338
resolved = os.path.realpath(co_filename)
339339
# startswith is cheaper than Path.is_relative_to, os.path.exists avoids Path construction
340340
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)
341+
self.path_cache[co_filename] = (resolved, is_valid)
343342
if not is_valid:
344343
return
344+
file_name = resolved
345345
if self.functions and code.co_name not in self.functions:
346346
return
347347
class_name = None
@@ -378,10 +378,11 @@ def tracer_logic(self, frame: FrameType, event: str) -> None:
378378
if function_qualified_name in self.ignored_qualified_functions:
379379
return
380380
if function_qualified_name not in self.function_count:
381-
# seeing this function for the first time
381+
# seeing this function for the first time — Path construction only happens here
382382
self.function_count[function_qualified_name] = 1
383+
file_path = Path(file_name)
383384
file_valid = filter_files_optimized(
384-
file_path=file_name,
385+
file_path=file_path,
385386
tests_root=Path(self.config["tests_root"]),
386387
ignore_paths=[Path(p) for p in self.config["ignore_paths"]],
387388
module_root=Path(self.config["module_root"]),
@@ -393,8 +394,8 @@ def tracer_logic(self, frame: FrameType, event: str) -> None:
393394
self.function_modules.append(
394395
FunctionModules(
395396
function_name=code.co_name,
396-
file_name=file_name,
397-
module_name=module_name_from_file_path(file_name, project_root_path=self.project_root),
397+
file_name=file_path,
398+
module_name=module_name_from_file_path(file_path, project_root_path=self.project_root),
398399
class_name=class_name,
399400
line_no=code.co_firstlineno,
400401
)
@@ -438,7 +439,7 @@ def tracer_logic(self, frame: FrameType, event: str) -> None:
438439
event,
439440
code.co_name,
440441
class_name,
441-
str(file_name),
442+
file_name,
442443
frame.f_lineno,
443444
frame.f_back.__hash__(),
444445
t_ns,

0 commit comments

Comments
 (0)