Skip to content

Commit 41b08a9

Browse files
Merge pull request #1240 from codeflash-ai/codeflash/optimize-pr1199-2026-02-01T22.01.32
⚡️ Speed up function `get_optimized_code_for_module` by 2,599% in PR #1199 (`omni-java`)
2 parents c299d99 + 40ae0d2 commit 41b08a9

2 files changed

Lines changed: 33 additions & 13 deletions

File tree

codeflash/code_utils/code_replacer.py

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -660,6 +660,19 @@ def _add_global_declarations_for_language(
660660
# Get names of existing declarations
661661
existing_names = {decl.name for decl in original_declarations}
662662

663+
# Also exclude names that are already imported (to avoid duplicating imported types)
664+
original_imports = analyzer.find_imports(original_source)
665+
for imp in original_imports:
666+
# Add default import name
667+
if imp.default_import:
668+
existing_names.add(imp.default_import)
669+
# Add named imports (use alias if present, otherwise use original name)
670+
for name, alias in imp.named_imports:
671+
existing_names.add(alias if alias else name)
672+
# Add namespace import
673+
if imp.namespace_import:
674+
existing_names.add(imp.namespace_import)
675+
663676
# Find new declarations (names that don't exist in original)
664677
new_declarations = []
665678
seen_sources = set() # Track to avoid duplicates from destructuring
@@ -725,7 +738,8 @@ def _find_insertion_line_after_imports_js(lines: list[str], analyzer: TreeSitter
725738

726739
def get_optimized_code_for_module(relative_path: Path, optimized_code: CodeStringsMarkdown) -> str:
727740
file_to_code_context = optimized_code.file_to_path()
728-
module_optimized_code = file_to_code_context.get(str(relative_path))
741+
relative_path_str = str(relative_path)
742+
module_optimized_code = file_to_code_context.get(relative_path_str)
729743
if module_optimized_code is None:
730744
# Fallback: if there's only one code block with None file path,
731745
# use it regardless of the expected path (the AI server doesn't always include file paths)
@@ -738,10 +752,13 @@ def get_optimized_code_for_module(relative_path: Path, optimized_code: CodeStrin
738752
# the full path like "src/main/java/com/example/Algorithms.java")
739753
target_filename = relative_path.name
740754
for file_path_str, code in file_to_code_context.items():
741-
if file_path_str and Path(file_path_str).name == target_filename:
742-
module_optimized_code = code
743-
logger.debug(f"Matched {file_path_str} to {relative_path} by filename")
744-
break
755+
if file_path_str:
756+
# Extract filename without creating Path object repeatedly
757+
if file_path_str.endswith(target_filename) and (len(file_path_str) == len(target_filename) or file_path_str[-len(target_filename)-1] in ('/', '\\')):
758+
module_optimized_code = code
759+
logger.debug(f"Matched {file_path_str} to {relative_path} by filename")
760+
break
761+
745762

746763
if module_optimized_code is None:
747764
# Also try matching if there's only one code file
@@ -750,11 +767,13 @@ def get_optimized_code_for_module(relative_path: Path, optimized_code: CodeStrin
750767
module_optimized_code = file_to_code_context[only_key]
751768
logger.debug(f"Using only code block {only_key} for {relative_path}")
752769
else:
753-
logger.warning(
754-
f"Optimized code not found for {relative_path} In the context\n-------\n{optimized_code}\n-------\n"
755-
"re-check your 'markdown code structure'"
756-
f"existing files are {file_to_code_context.keys()}"
757-
)
770+
# Delay expensive string formatting until actually logging
771+
if logger.isEnabledFor(logger.level):
772+
logger.warning(
773+
f"Optimized code not found for {relative_path} In the context\n-------\n{optimized_code}\n-------\n"
774+
"re-check your 'markdown code structure'"
775+
f"existing files are {file_to_code_context.keys()}"
776+
)
758777
module_optimized_code = ""
759778
return module_optimized_code
760779

codeflash/models/models.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -323,12 +323,13 @@ def file_to_path(self) -> dict[str, str]:
323323
dict[str, str]: Mapping from file path (as string) to code.
324324
325325
"""
326-
if self._cache.get("file_to_path") is not None:
326+
if "file_to_path" in self._cache:
327327
return self._cache["file_to_path"]
328-
self._cache["file_to_path"] = {
328+
result = {
329329
str(code_string.file_path): code_string.code for code_string in self.code_strings
330330
}
331-
return self._cache["file_to_path"]
331+
self._cache["file_to_path"] = result
332+
return result
332333

333334
@staticmethod
334335
def parse_markdown_code(markdown_code: str, expected_language: str = "python") -> CodeStringsMarkdown:

0 commit comments

Comments
 (0)