|
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 logger
|
| 7 | +from codeflash.discovery.functions_to_optimize import FunctionToOptimize |
6 | 8 | from codeflash.tracing.profile_stats import ProfileStats
|
7 | 9 |
|
8 | 10 | if TYPE_CHECKING:
|
@@ -105,19 +107,31 @@ def get_function_ttx_score(self, function_to_optimize: FunctionToOptimize) -> fl
|
105 | 107 | return 0.0
|
106 | 108 |
|
107 | 109 | def rank_functions(self, functions_to_optimize: list[FunctionToOptimize]) -> list[FunctionToOptimize]:
|
108 |
| - # Calculate ttX scores for all functions |
| 110 | + # Load and cache function stats up front |
| 111 | + stats = self.load_function_stats() |
| 112 | + |
| 113 | + # Compute function ttX scores using direct dict lookup |
109 | 114 | function_scores = []
|
| 115 | + append = function_scores.append # Localize for loop speed |
| 116 | + |
110 | 117 | for func in functions_to_optimize:
|
111 |
| - ttx_score = self.get_function_ttx_score(func) |
112 |
| - function_scores.append((func, ttx_score)) |
| 118 | + # Precompute both possible keys for maximum efficiency |
| 119 | + key1 = f"{func.file_path}:{func.qualified_name}" |
| 120 | + tstat = stats.get(key1) |
| 121 | + if tstat is not None: |
| 122 | + ttx_score = tstat["ttx_score"] |
| 123 | + else: |
| 124 | + key2 = f"{func.file_path}:{func.function_name}" |
| 125 | + tstat = stats.get(key2) |
| 126 | + if tstat is not None: |
| 127 | + ttx_score = tstat["ttx_score"] |
| 128 | + else: |
| 129 | + ttx_score = 0.0 |
| 130 | + append((func, ttx_score)) |
113 | 131 |
|
114 | 132 | # Sort by ttX score descending (highest impact first)
|
115 | 133 | function_scores.sort(key=lambda x: x[1], reverse=True)
|
116 | 134 |
|
117 |
| - # logger.info("Function ranking by ttX score:") |
118 |
| - # for i, (func, score) in enumerate(function_scores[:10]): # Top 10 |
119 |
| - # logger.info(f" {i + 1}. {func.qualified_name} (ttX: {score:.0f}ns)") |
120 |
| - |
121 | 135 | ranked_functions = [func for func, _ in function_scores]
|
122 | 136 | logger.info(f"Ranked {len(ranked_functions)} functions by optimization priority")
|
123 | 137 |
|
|
0 commit comments