diff --git a/codeflash/languages/code_replacer.py b/codeflash/languages/code_replacer.py index ac35dd998..b4505dfa0 100644 --- a/codeflash/languages/code_replacer.py +++ b/codeflash/languages/code_replacer.py @@ -28,11 +28,13 @@ def get_optimized_code_for_module(relative_path: Path, optimized_code: CodeStrin module_optimized_code = file_to_code_context["None"] logger.debug(f"Using code block with None file_path for {relative_path}") else: - logger.warning( - f"Optimized code not found for {relative_path} In the context\n-------\n{optimized_code}\n-------\n" - "re-check your 'markdown code structure'" - f"existing files are {file_to_code_context.keys()}" - ) + # Avoid expensive string formatting when logging is not enabled + if logger.isEnabledFor(logger.level): + logger.warning( + f"Optimized code not found for {relative_path} In the context\n-------\n{optimized_code}\n-------\n" + "re-check your 'markdown code structure'" + f"existing files are {file_to_code_context.keys()}" + ) module_optimized_code = "" return module_optimized_code diff --git a/codeflash/models/models.py b/codeflash/models/models.py index 697601403..acc15133a 100644 --- a/codeflash/models/models.py +++ b/codeflash/models/models.py @@ -331,12 +331,12 @@ def file_to_path(self) -> dict[str, str]: dict[str, str]: Mapping from file path (as string) to code. """ - if self._cache.get("file_to_path") is not None: - 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"] + cached = self._cache.get("file_to_path") + if cached is not None: + return cached + result = {str(code_string.file_path): code_string.code for code_string in self.code_strings} + self._cache["file_to_path"] = result + return result @staticmethod def parse_markdown_code(markdown_code: str, expected_language: str = "python") -> CodeStringsMarkdown: