Skip to content

Commit d7b67a0

Browse files
authored
Merge pull request #783 from codeflash-ai/codeflash/optimize-pr363-2025-09-29T22.03.01
⚡️ Speed up function `generate_candidates` by 4,183% in PR #363 (`part-1-windows-fixes`)
2 parents f978a40 + d305de8 commit d7b67a0

File tree

1 file changed

+15
-7
lines changed

1 file changed

+15
-7
lines changed

codeflash/code_utils/coverage_utils.py

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -44,16 +44,24 @@ def build_fully_qualified_name(function_name: str, code_context: CodeOptimizatio
4444
def generate_candidates(source_code_path: Path) -> set[str]:
4545
"""Generate all the possible candidates for coverage data based on the source code path."""
4646
candidates = set()
47-
candidates.add(source_code_path.name)
48-
current_path = source_code_path.parent
49-
50-
last_added = source_code_path.name
51-
while current_path != current_path.parent:
52-
candidate_path = (Path(current_path.name) / last_added).as_posix()
47+
# Add the filename as a candidate
48+
name = source_code_path.name
49+
candidates.add(name)
50+
51+
# Precompute parts for efficient candidate path construction
52+
parts = source_code_path.parts
53+
n = len(parts)
54+
55+
# Walk up the directory structure without creating Path objects or repeatedly converting to posix
56+
last_added = name
57+
# Start from the last parent and move up to the root, exclusive (skip the root itself)
58+
for i in range(n - 2, 0, -1):
59+
# Combine the ith part with the accumulated path (last_added)
60+
candidate_path = f"{parts[i]}/{last_added}"
5361
candidates.add(candidate_path)
5462
last_added = candidate_path
55-
current_path = current_path.parent
5663

64+
# Add the absolute posix path as a candidate
5765
candidates.add(source_code_path.as_posix())
5866
return candidates
5967

0 commit comments

Comments
 (0)