Skip to content

Commit 67ee734

Browse files
committed
perf: remove redundant project_root_path.resolve() from hot path
Resolve project root paths once at construction time (TestConfig.__post_init__, FunctionOptimizer.__init__, filter_functions entry) instead of on every call to module_name_from_file_path — eliminating ~776 redundant filesystem syscalls.
1 parent b19e1bd commit 67ee734

4 files changed

Lines changed: 9 additions & 4 deletions

File tree

codeflash/code_utils/code_utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -340,7 +340,7 @@ def get_qualified_name(module_name: str, full_qualified_name: str) -> str:
340340

341341
def module_name_from_file_path(file_path: Path, project_root_path: Path, *, traverse_up: bool = False) -> str:
342342
try:
343-
relative_path = file_path.resolve().relative_to(project_root_path.resolve())
343+
relative_path = file_path.resolve().relative_to(project_root_path)
344344
return relative_path.with_suffix("").as_posix().replace("/", ".")
345345
except ValueError:
346346
if traverse_up:

codeflash/discovery/functions_to_optimize.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -836,6 +836,7 @@ def filter_functions(
836836
*,
837837
disable_logs: bool = False,
838838
) -> tuple[dict[Path, list[FunctionToOptimize]], int]:
839+
resolved_project_root = project_root.resolve()
839840
filtered_modified_functions: dict[str, list[FunctionToOptimize]] = {}
840841
blocklist_funcs = get_blocklisted_functions()
841842
logger.debug(f"Blocklisted functions: {blocklist_funcs}")
@@ -912,7 +913,7 @@ def is_test_file(file_path_normalized: str) -> bool:
912913
lang_support = get_language_support(Path(file_path))
913914
if lang_support.language == Language.PYTHON:
914915
try:
915-
ast.parse(f"import {module_name_from_file_path(Path(file_path), project_root)}")
916+
ast.parse(f"import {module_name_from_file_path(Path(file_path), resolved_project_root)}")
916917
except SyntaxError:
917918
malformed_paths_count += 1
918919
continue

codeflash/optimization/function_optimizer.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -443,7 +443,7 @@ def __init__(
443443
args: Namespace | None = None,
444444
replay_tests_dir: Path | None = None,
445445
) -> None:
446-
self.project_root = test_cfg.project_root_path
446+
self.project_root = test_cfg.project_root_path.resolve()
447447
self.test_cfg = test_cfg
448448
self.aiservice_client = aiservice_client if aiservice_client else AiServiceClient()
449449
self.function_to_optimize = function_to_optimize
@@ -1451,7 +1451,7 @@ def reformat_code_and_helpers(
14511451
optimized_code = ""
14521452
if optimized_context is not None:
14531453
file_to_code_context = optimized_context.file_to_path()
1454-
optimized_code = file_to_code_context.get(str(path.relative_to(self.project_root)), "")
1454+
optimized_code = file_to_code_context.get(str(path.resolve().relative_to(self.project_root)), "")
14551455

14561456
new_code = format_code(
14571457
self.args.formatter_cmds, path, optimized_code=optimized_code, check_diff=True, exit_on_failure=False

codeflash/verification/verification_utils.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,10 @@ class TestConfig:
158158
_language: Optional[str] = None # Language identifier for multi-language support
159159
js_project_root: Optional[Path] = None # JavaScript project root (directory containing package.json)
160160

161+
def __post_init__(self) -> None:
162+
self.project_root_path = self.project_root_path.resolve()
163+
self.tests_project_rootdir = self.tests_project_rootdir.resolve()
164+
161165
@property
162166
def test_framework(self) -> str:
163167
"""Returns the appropriate test framework based on language.

0 commit comments

Comments
 (0)