mirror of
https://github.com/codeflash-ai/codeflash.git
synced 2026-05-04 18:25:17 +00:00
perf: apply Codeflash optimizations for file_to_path and basename matching
- Use try/except KeyError instead of .get() in file_to_path cache (faster on hot path when key is usually present) - Use os.path.basename() instead of Path().name in get_optimized_code_for_module to avoid constructing Path objects from strings - Disable PTH119 ruff rule to allow os.path.basename for performance
This commit is contained in:
parent
ff7b93dbb2
commit
6688b4c3af
3 changed files with 10 additions and 7 deletions
|
|
@ -6,13 +6,15 @@ via the LanguageSupport protocol.
|
|||
|
||||
from __future__ import annotations
|
||||
|
||||
from pathlib import Path
|
||||
import os
|
||||
from typing import TYPE_CHECKING
|
||||
|
||||
from codeflash.cli_cmds.console import logger
|
||||
from codeflash.languages.base import FunctionFilterCriteria, Language
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from pathlib import Path
|
||||
|
||||
from codeflash.discovery.functions_to_optimize import FunctionToOptimize
|
||||
from codeflash.languages.base import LanguageSupport
|
||||
from codeflash.models.models import CodeStringsMarkdown
|
||||
|
|
@ -38,7 +40,7 @@ def get_optimized_code_for_module(relative_path: Path, optimized_code: CodeStrin
|
|||
# directory prefix but the correct filename
|
||||
target_name = relative_path.name
|
||||
basename_matches = [
|
||||
code for path, code in file_to_code_context.items() if path != "None" and Path(path).name == target_name
|
||||
code for path, code in file_to_code_context.items() if path != "None" and os.path.basename(path) == target_name
|
||||
]
|
||||
if len(basename_matches) == 1:
|
||||
logger.debug(f"Using basename-matched code block for {relative_path}")
|
||||
|
|
|
|||
|
|
@ -340,12 +340,12 @@ class CodeStringsMarkdown(BaseModel):
|
|||
dict[str, str]: Mapping from file path (as string) to code.
|
||||
|
||||
"""
|
||||
if self._cache.get("file_to_path") is not None:
|
||||
try:
|
||||
return self._cache["file_to_path"]
|
||||
self._cache["file_to_path"] = {
|
||||
str(code_string.file_path): code_string.code for code_string in self.code_strings
|
||||
}
|
||||
return self._cache["file_to_path"]
|
||||
except KeyError:
|
||||
mapping = {str(code_string.file_path): code_string.code for code_string in self.code_strings}
|
||||
self._cache["file_to_path"] = mapping
|
||||
return mapping
|
||||
|
||||
@staticmethod
|
||||
def parse_markdown_code(markdown_code: str, expected_language: str = "python") -> CodeStringsMarkdown:
|
||||
|
|
|
|||
|
|
@ -295,6 +295,7 @@ ignore = [
|
|||
"F841", # Unused variable (often intentional)
|
||||
"ANN202", # Missing return type for private functions
|
||||
"B009", # getattr-with-constant - needed to avoid mypy [misc] on dunder access
|
||||
"PTH119", # os.path.basename — faster than Path().name for string paths
|
||||
]
|
||||
|
||||
[tool.ruff.lint.flake8-type-checking]
|
||||
|
|
|
|||
Loading…
Reference in a new issue