Skip to content

Commit 4316780

Browse files
committed
perf: cache site.getsitepackages() result to avoid repeated resolution
1 parent 0a2ec48 commit 4316780

1 file changed

Lines changed: 6 additions & 3 deletions

File tree

codeflash/code_utils/code_utils.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -427,10 +427,13 @@ def get_run_tmp_file(file_path: Path | str) -> Path:
427427
return get_run_tmp_file.tmpdir_path / file_path
428428

429429

430+
@lru_cache(maxsize=1)
431+
def _get_site_packages_paths() -> tuple[Path, ...]:
432+
return tuple(Path(p).resolve() for p in site.getsitepackages())
433+
434+
430435
def path_belongs_to_site_packages(file_path: Path) -> bool:
431-
file_path_resolved = file_path.resolve()
432-
site_packages = [Path(p).resolve() for p in site.getsitepackages()]
433-
return any(file_path_resolved.is_relative_to(site_package_path) for site_package_path in site_packages)
436+
return any(file_path.resolve().is_relative_to(sp) for sp in _get_site_packages_paths())
434437

435438

436439
def is_class_defined_in_file(class_name: str, file_path: Path) -> bool:

0 commit comments

Comments
 (0)