Skip to content

Commit d3cf9e5

Browse files
committed
perf: cache site.getsitepackages() result to avoid repeated resolution
1 parent 786c94a commit d3cf9e5

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
@@ -421,10 +421,13 @@ def get_run_tmp_file(file_path: Path | str) -> Path:
421421
return _run_tmpdir_path / file_path
422422

423423

424+
@lru_cache(maxsize=1)
425+
def _get_site_packages_paths() -> tuple[Path, ...]:
426+
return tuple(Path(p).resolve() for p in site.getsitepackages())
427+
428+
424429
def path_belongs_to_site_packages(file_path: Path) -> bool:
425-
file_path_resolved = file_path.resolve()
426-
site_packages = [Path(p).resolve() for p in site.getsitepackages()]
427-
return any(file_path_resolved.is_relative_to(site_package_path) for site_package_path in site_packages)
430+
return any(file_path.resolve().is_relative_to(sp) for sp in _get_site_packages_paths())
428431

429432

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

0 commit comments

Comments
 (0)