diff --git a/codeflash/code_utils/coverage_utils.py b/codeflash/code_utils/coverage_utils.py index 456685d4..65bc69d4 100644 --- a/codeflash/code_utils/coverage_utils.py +++ b/codeflash/code_utils/coverage_utils.py @@ -41,13 +41,23 @@ def build_fully_qualified_name(function_name: str, code_context: CodeOptimizatio def generate_candidates(source_code_path: Path) -> list[str]: """Generate all the possible candidates for coverage data based on the source code path.""" - candidates = [source_code_path.name] + candidates = [] + name = source_code_path.name current_path = source_code_path.parent - while current_path != current_path.parent: - candidate_path = (Path(current_path.name) / candidates[-1]).as_posix() - candidates.append(candidate_path) - current_path = current_path.parent + # Start with the base candidate + candidate = name + candidates.append(candidate) + + # Pre-fetch parent for comparison to avoid repeated attribute access + parent = current_path.parent + + while current_path != parent: + # Build candidate path using string concat for speed + candidate = f"{current_path.name}/{candidate}" + candidates.append(candidate) + current_path = parent + parent = current_path.parent return candidates