|
1 | 1 | from __future__ import annotations |
2 | 2 |
|
| 3 | +from pathlib import Path |
3 | 4 | from typing import TYPE_CHECKING |
4 | 5 |
|
5 | 6 | from codeflash.cli_cmds.console import console, logger |
|
8 | 9 | from codeflash.tracing.profile_stats import ProfileStats |
9 | 10 |
|
10 | 11 | if TYPE_CHECKING: |
11 | | - from pathlib import Path |
12 | | - |
13 | 12 | from codeflash.discovery.functions_to_optimize import FunctionToOptimize |
14 | 13 |
|
15 | 14 |
|
@@ -82,14 +81,47 @@ def load_function_stats(self) -> None: |
82 | 81 | self._function_stats = {} |
83 | 82 |
|
84 | 83 | def _get_function_stats(self, function_to_optimize: FunctionToOptimize) -> dict | None: |
85 | | - file_path = function_to_optimize.file_path.as_posix() |
86 | | - key1 = f"{file_path}:{function_to_optimize.qualified_name}" |
87 | | - key2 = f"{file_path}:{function_to_optimize.function_name}" |
88 | | - |
89 | | - stats = self._function_stats.get(key1) |
90 | | - if stats is not None: |
91 | | - return stats |
92 | | - return self._function_stats.get(key2) |
| 84 | + # Get all possible path variations |
| 85 | + original_path = function_to_optimize.file_path.as_posix() |
| 86 | + resolved_path = function_to_optimize.file_path.resolve().as_posix() |
| 87 | + |
| 88 | + # Try to get relative path from current working directory |
| 89 | + try: |
| 90 | + cwd_relative = function_to_optimize.file_path.relative_to(Path.cwd()).as_posix() |
| 91 | + except ValueError: |
| 92 | + cwd_relative = original_path |
| 93 | + |
| 94 | + # Try to get relative path from trace file directory |
| 95 | + try: |
| 96 | + trace_dir = self.trace_file_path.parent |
| 97 | + trace_relative = function_to_optimize.file_path.relative_to(trace_dir).as_posix() |
| 98 | + except ValueError: |
| 99 | + trace_relative = original_path |
| 100 | + |
| 101 | + # All possible path variants to try |
| 102 | + path_variants = [original_path, resolved_path, cwd_relative, trace_relative] |
| 103 | + |
| 104 | + for file_path in path_variants: |
| 105 | + key1 = f"{file_path}:{function_to_optimize.qualified_name}" |
| 106 | + key2 = f"{file_path}:{function_to_optimize.function_name}" |
| 107 | + |
| 108 | + stats = self._function_stats.get(key1) |
| 109 | + if stats is not None: |
| 110 | + return stats |
| 111 | + stats = self._function_stats.get(key2) |
| 112 | + if stats is not None: |
| 113 | + return stats |
| 114 | + |
| 115 | + # Try fuzzy matching as last resort - match by function name and filename |
| 116 | + target_filename = function_to_optimize.file_path.name |
| 117 | + for key, stats in self._function_stats.items(): |
| 118 | + if stats.get("function_name") == function_to_optimize.function_name and ( |
| 119 | + key.endswith(f"/{target_filename}") or target_filename in key |
| 120 | + ): |
| 121 | + return stats |
| 122 | + |
| 123 | + logger.debug(f"Could not find stats for function {function_to_optimize.function_name} in {original_path}") |
| 124 | + return None |
93 | 125 |
|
94 | 126 | def get_function_ttx_score(self, function_to_optimize: FunctionToOptimize) -> float: |
95 | 127 | stats = self._get_function_stats(function_to_optimize) |
|
0 commit comments