diff --git a/codeflash/code_utils/coverage_utils.py b/codeflash/code_utils/coverage_utils.py index 70b0d2e7f..8dd5e6c32 100644 --- a/codeflash/code_utils/coverage_utils.py +++ b/codeflash/code_utils/coverage_utils.py @@ -44,16 +44,24 @@ def build_fully_qualified_name(function_name: str, code_context: CodeOptimizatio def generate_candidates(source_code_path: Path) -> set[str]: """Generate all the possible candidates for coverage data based on the source code path.""" candidates = set() - candidates.add(source_code_path.name) - current_path = source_code_path.parent - - last_added = source_code_path.name - while current_path != current_path.parent: - candidate_path = (Path(current_path.name) / last_added).as_posix() + # Add the filename as a candidate + name = source_code_path.name + candidates.add(name) + + # Precompute parts for efficient candidate path construction + parts = source_code_path.parts + n = len(parts) + + # Walk up the directory structure without creating Path objects or repeatedly converting to posix + last_added = name + # Start from the last parent and move up to the root, exclusive (skip the root itself) + for i in range(n - 2, 0, -1): + # Combine the ith part with the accumulated path (last_added) + candidate_path = f"{parts[i]}/{last_added}" candidates.add(candidate_path) last_added = candidate_path - current_path = current_path.parent + # Add the absolute posix path as a candidate candidates.add(source_code_path.as_posix()) return candidates