Skip to content

perf: cache site.getsitepackages() result#2128

Merged
KRRT7 merged 2 commits intocf-perf-improvementsfrom
cf-perf-cache-site-packages
May 7, 2026
Merged

perf: cache site.getsitepackages() result#2128
KRRT7 merged 2 commits intocf-perf-improvementsfrom
cf-perf-cache-site-packages

Conversation

@KRRT7
Copy link
Copy Markdown
Collaborator

@KRRT7 KRRT7 commented May 7, 2026

Summary

Cache site.getsitepackages() with @lru_cache(maxsize=1) to avoid repeated path resolution on every call to path_belongs_to_site_packages().

Part of #2132 — targeted E2E pipeline performance improvements.

Problem

site.getsitepackages() calls os.path.join + path resolution for every site-packages directory on every invocation. path_belongs_to_site_packages() is called hundreds of times per optimization run. Site-packages paths never change during a process lifetime — resolving them repeatedly is pure waste.

Solution

Extract _get_site_packages_paths() cached helper with @lru_cache(maxsize=1). First call resolves paths, all subsequent calls return the cached tuple.

Changes

  • codeflash/code_utils/code_utils.py: Extract cached helper
  • tests/test_code_utils.py: 4 new tests covering caching behavior, true/false paths, and symlinks

Test plan

  • uv run pytest tests/test_code_utils.py -k site_packages -v
  • Tests use cache_clear() to avoid cross-test contamination

@KRRT7 KRRT7 merged commit 32c627c into cf-perf-improvements May 7, 2026
28 of 30 checks passed
@KRRT7 KRRT7 deleted the cf-perf-cache-site-packages branch May 7, 2026 03:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant