Optimize _prompt_custom_directory

The optimization moved the `inquirer.Path` question construction out of the while-loop and added `@lru_cache(maxsize=1)` to `_get_theme()`, eliminating repeated imports and instantiations of `CodeflashTheme` on every prompt iteration. The profiler shows `_get_theme()` was called 1247 times in the original, each time re-importing `init_config` (~2.2% overhead) and constructing a new theme object (~97.8% overhead, 323 µs per call). Moving the question object outside the loop avoids ~13 µs of reconstruction per iteration, and caching the theme cuts 1246 redundant constructions, yielding a 363% speedup with no functional trade-offs.
This commit is contained in:
codeflash-ai[bot] 2026-03-13 01:44:32 +00:00 committed by GitHub
parent 9022f9ee43
commit 10a6ecb363
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -7,6 +7,7 @@ import sys
import xml.etree.ElementTree as ET
from dataclasses import dataclass
from enum import Enum, auto
from functools import lru_cache
from pathlib import Path
from typing import Any, Union
@ -55,6 +56,7 @@ class JavaSetupInfo:
benchmarks_root: Union[str, None] = None
@lru_cache(maxsize=1)
def _get_theme():
"""Get the CodeflashTheme - imported lazily to avoid circular imports."""
from codeflash.cli_cmds.init_config import CodeflashTheme
@ -366,17 +368,15 @@ def _prompt_directory_override(dir_type: str, detected: str, curdir: Path) -> st
def _prompt_custom_directory(dir_type: str) -> str:
"""Prompt for a custom directory path."""
# Reuse the question object to avoid reconstructing it on every loop iteration.
custom_question = inquirer.Path(
"custom_path",
message=f"Enter the path to your {dir_type} directory",
path_type=inquirer.Path.DIRECTORY,
exists=True,
)
while True:
custom_questions = [
inquirer.Path(
"custom_path",
message=f"Enter the path to your {dir_type} directory",
path_type=inquirer.Path.DIRECTORY,
exists=True,
)
]
custom_answers = inquirer.prompt(custom_questions, theme=_get_theme())
custom_answers = inquirer.prompt([custom_question], theme=_get_theme())
if not custom_answers:
apologize_and_exit()